Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Task 1 of the CPS125 project: Toronto Metropolitan University CPS125 Project - Winter 2023 Page 1 of 8 CPS125 Project - Winter 2023 Overall Instructions
Task 1 of the CPS125 project:
Toronto Metropolitan University CPS125 Project - Winter 2023 Page 1 of 8 CPS125 Project - Winter 2023 Overall Instructions This is a group project with a maximum group size of 3. File named "cps125_project.c" is given. You must complete the requirements of this project by implementing the functions described in this document. Your project should produce a series of bitmap images (file extension BMP). Once you complete the project, submit only the file "cps125_project.c" and nothing else. Project background In today's world, everyone has some experience with image filters ranging from photo editing software to social media applications. These filters are created using computer vision algorithms. In this project you practice applying the computer science concepts that you have learned in the course by implementing a few basic algorithms in C. This document will guide you through the core computer vision concepts and you will be required to implement them. 3-dimensional Array and Images In the lectures, the concept of 1-dimensional (1D) and 2-dimensional (2D) arrays is covered. We will extend that concept to 3-dimensional (3D) arrays, which is especially useful when dealing with images. This concept can be further extended to N-dimensional arrays, used in mathematics, physics, and machine learning applications. However, we will only focus on 3D arrays in this project. It is easy to think of an image as a 2D array since we can correlate each pixel location with a row and column where the number of columns and rows are the width and height of an image respectively. This is also referred to as the resolution or size of an image. In fact, each pixel does not have just one value but three values. Each value is the intensity of each channel of three colours: blue, green, and red. This intensity level is a number between 0 and 255. row We can imagine an image to be a cuboid where 2 each pixel location still has a row and column 0 1 but the layers is divided such that layer O is for the blue pixels, layer 1 is for the green pixel and 392 . 482 .576 .376 layer 2 is for the red pixel. Figure 1 is a diagram that illustrates this concept by showing O .263 . 169 . 305 . 376 . 451 a 3x3x3 cube representing a tiny image of 3 . 478 . 63 pixels by 3 pixels and their blue, green and red .44 263 channels. column 1 . 580 . 79 . 373 .60 375 . 478 .561 To understand how to load an image into a 3D 2 . 443 . 569 .674 array we have to go back and visit the idea of N 2D array. A 2D array in reality is just a ID array where the actual memory location of a particular row and column can be calculated 0 with the following formula: channel index = row x width + column Figure I We can extend this formula to access the 3 colour channels where:Toronto Metropolitan University CPS125 Project - Winter 2023 Page 2 of 8 blue Pixel = index + 0 greenPixel = index + 1 redPixel = index + 2 Let us apply this to the simple 3x3 pixels image example. In this example, the height and width of our image are both equal to 3. The size of the array that we need is 27 (3 x 3 x 3 = 27). We must declare an array and use the function malloc() (or calloc function) to allocate space for 27 unsigned integers. Suppose that we want the image to be all black with the pixel at 3" row, 2" column to be fully yellow. We can use a for loop to set all the pixel values to be zero. A fully yellow colour is where blue = 0; red = 255; and green = 255. We can then use the formula above to find which memory location to set. index = num_of channel x (row x width + column) index = 3 x (2 x 3 + 1) = 21 greenPixel = 21 + 1 redPixel = 21 + 2 Let us now look at the code snippet that will accomplish this. You learned about int type in your lecture, we will use the type of byte, which is an unsigned integer of size 1 (byte). This means that a variable of type byte holds a number between 0 and 255. int width = 3, height = 3, row = 2, column = 1; int index, greenChannel, redChannel; byte *imgColour = calloc (width*height*3, sizeof(byte) ) ; index = (2*width+1) *3; greenChannel = index + 1; redChannel = index + 2; imgColour [greenChannel ] = 255; imgColour [ redChannel ] = 255; The code snippet above we declared an array named imageColour. The calloc() function was used to allocate space and initialize all the values to 0. We then calculate the index of the array to correspond with the pixel we want set to be yellow. Then used that to calculate the index of the green channel and red channel. We then use those indices to assign the value to the appropriate pixel channel. The next section will provide you with detail information on the tasks you must complete for this project.Toronto Metropolitan University CPS125 Project - Winter 2023 Page 3 of 8 Project Tasks Description In the file "cps125_project.c" given to you, there is code that will read in and write out to bitmap files. The code will read in a bitmap file and place the pixel data in an array in the same format described above. Your tasks will be to create filters and apply them to the image array. The rest of the code will output your filtered images back to bitmap files in the same folder. Do not change any of the core code except in the commented areas telling you to change it or add to it. Task 1 - Grayscale image conversion (10 marks) The first task in most computer vision algorithms is to convert the colour images into a grayscale image. A grayscale image is an image where each pixel has a shade of grey where the value lies between 0 and 255. To convert a colour image to a grayscale image we must average the three colour channels of the colour image to get the grey value. Since we crush our three colour channels into one channel, we only need a 2D array to hold our grayscale image. This simplicity will help us with other tasks later. For example, if we know a pixel from the colour image to have a blue value of 100, green value of 50, and red value of 180, then the grey value is 1 10. 100 + 50 + 180 = 110 In the "cps125_project.c" find the main function. Note that a function named ReadImage() is called. This function will read a bitmap file and load all the pixel information into the array colourlig, and the value of width and height of the image and assign them to the variable width and height respectively. The size of the colouring array is width x height x 3. Look for the comment that starts with, /* Task 1 - Allocate memory for greyIng in main here After the comment, write the code that allocate space for the variable greying. The variable greying should be a 2D array of type byte with the size of width x height of the image. After allocating memory, go to the function colourToGrey(). You must write the code of the body of this function. The function takes in the input arguments colour, height, width, calculates the grey value of each pixel and then assigning that grey value to the argument grey. Note that colour will be a 3D array and grey will be a 2D array. Once Task 1 is completed find the macro TASKI located just above the main function. Set it to be 1 instead of 0 and then compile and run your program. This will produce a grayscale image in the same folder and is named "flowers_grey.bmp". You can then open that image to see if you implemented your function correctly. If your implementation of colourToGrey() is correct, the program will tell you that you completed it successfully. If not, you can look at the output image and compare it to the "ground truth" image name "flowers_grey_GT.bmp" That is what the image is supposed to look like. If you find Task 1 to be too difficult, perhaps due to the idea of 3D array, you can choose to skip it. There is a macro called SKIP_TASK1. Set it to be I and it will skip it but still allow you to work on the other tasks which deal with only 2D arrays. To do this, you must make sure you have the file "flowers_grey_GT.bmp" in the same folder.\fProject References, Tips and Hints 1) Do NOT change any code in the Built-in Functions sections of the file "cps125_project.c" but you are welcome to look at the code there to see if any of it is helpful. 2) The functions Test Taskl, Test Task2, Test Task4, Test Task4 would be useful to look at to see how your code is called. 3) Look at those test functions to try to write your own test functions. Do not test your code on the image right away but make up small arrays like the examples that you see in this document and test your code with those first. 4) Write small bits of code and test each part's functionality thoroughly before moving on. 5) For a better understanding of how the overall process of this project, there are some excellent videos by the YouTube channel Computerphile and some links to explanations of convolutions: https://youtube/C zFhWdM4ic https://youtube/uihBwtPIBxM . https://setosa.io/ev/image-kernels/Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started