Question: I need to fill in the C code where it says // ------------------- // YOUR CODE GOES HERE // ------------------- . Purpose Learn about

I need to fill in the C code where it says "

 // ------------------- // YOUR CODE GOES HERE // -------------------" 

.

Purpose

Learn about histograms

Use arrays to store and manipulate data

Apply filters to images

Mainly, you are to implement two functions and then use them in the main function before and after the image gets blurred. The first is fillHistogram which will populate your histogram array with how many pixels have luminances of each level (0 through 9), and the second is printHistogram which will print the histogram out to the screen in the proper format.

You will need the following formula to calculate our luminance level:

Luminance Level = 2.126 / 255 * R + 7.152 / 255 * G + 0.722 / 255 * B

Round this number down to find the luminance level that you should increment in your histogram. As a special case, you should treat a luminance level of anything higher than 9 as a 9 to make sure it all fits within our histogram (e.g. if R, G, and B are all 255). The format for printing your histogram is as follows:

Level, Count,

0, 16619,

1, 28832,

2, 29331,

3, 10855,

4, 13485,

5, 36156,

6, 41254,

7, 3730,

8, 3091,

9, 3147,

Generate histograms for each of the following images from before and after the blur:

electricity.png

tesla.png

coover.png

// Defines and includes needed for using PNGs #define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image.h" #include "stb_image_write.h" // Defines for determining maximum and minimum numbers #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b)) // Maximum dimensions of the image static const int MAX_HEIGHT = 500; static const int MAX_WIDTH = 500; // Function Prototypes void fillHistogram(const uint8_t image[MAX_HEIGHT][MAX_WIDTH][3], int height, int width, int hist[10]); void printHistogram(const int hist[10]); void readPNG(const char* filename, uint8_t image[MAX_HEIGHT][MAX_WIDTH][3], int* height, int* width); void writePNG(const char* filename, const uint8_t image[MAX_HEIGHT][MAX_WIDTH][3], int height, int width); void boxBlur(const uint8_t input[MAX_HEIGHT][MAX_WIDTH][3], int height, int width, int boxSize, uint8_t output[MAX_HEIGHT][MAX_WIDTH][3]); int main() { // Image Array (Height by Width by Color Depth) uint8_t image[MAX_HEIGHT][MAX_WIDTH][3]; uint8_t blurredImage[MAX_HEIGHT][MAX_WIDTH][3]; // Actual image height and image width (smaller than or equal to the max) int imageHeight, imageWidth; // Read in an image readPNG("image.png", image, &imageHeight, &imageWidth); // ------------------- // YOUR CODE GOES HERE // ------------------- // Apply box blur boxBlur(image, imageHeight, imageWidth, 15, blurredImage); // ------------------- // YOUR CODE GOES HERE // ------------------- // If you're curious to see what the blurred image looks like, uncomment this next line //writePNG("blurredImage.png", blurredImage, imageHeight, imageWidth); return 0; } /** * Function: fillHistogram * Description: Fills a histogram according to the contents in image * image: A 3D array that holds an image * hist: The histogram that will be filled */ void fillHistogram(const uint8_t image[MAX_HEIGHT][MAX_WIDTH][3], int height, int width, int hist[10]) { // ------------------- // YOUR CODE GOES HERE // ------------------- } /** * Function: printHistogram * Description: Prints out a filled histogram to the screen * hist: The histogram that is to be printed */ void printHistogram(const int hist[10]) { // ------------------- // YOUR CODE GOES HERE // ------------------- } /** * Function: readPNG * Description: Reads in a PNG file and stores it in a 3D array * filename: A string that holds the name of the file to be read in (e.g. "~/Downloads/image.png") * height: A pointer to the height of the image (this will update the value to the proper width after it runs) * width: A pointer to the width of the image (this will update the value to the proper width after it runs) * image: A 3D array that will hold all of the values of the image after running */ void readPNG(const char* filename, uint8_t image[MAX_HEIGHT][MAX_WIDTH][3], int* height, int* width) { int bytesPerPixel; // Read the image into rgb_image (also width, height, and bytesPerPixel now have the correct values) uint8_t* rgb_image = stbi_load(filename, width, height, &bytesPerPixel, 3); if (*width > 500 || *height > 500) { printf("The image was too large. The width and height must be less than or equal to 500. "); exit(-1); } // Go through each pixel and RGB value int i, j, k; for (i = 0; i < *height; i++) { for (j = 0; j < *width; j++) { for (k = 0; k < 3; k++) { // Store the 1D array value into our 3D array image[i][j][k] = rgb_image[i * (*width) * 3 + j * 3 + k]; } } } // Free up the memory allocated by stbi_load free(rgb_image); } /** * Function: writePNG * Description: Writes a 3D array to a PNG file * filename: A string that holds the name of the file to be written to (e.g. "~/Downloads/image2.png") * height: The height of the image * width: The width of the image * image: A 3D array that holds the values to be written */ void writePNG(const char* filename, const uint8_t image[MAX_HEIGHT][MAX_WIDTH][3], int height, int width) { // Allocate memory for writing the image uint8_t* rgb_image = (uint8_t*) malloc(sizeof(uint8_t) * width * height * 3); // Go through each pixel and RGB value int i, j, k; for (i = 0; i < height; i++) { for (j = 0; j < width; j++) { for (k = 0; k < 3; k++) { // Store the 3D array value into their 1D array rgb_image[i * width * 3 + j * 3 + k] = image[i][j][k]; } } } // Write the image to the file stbi_write_png(filename, width, height, 3, rgb_image, width*3); // Free up the memory allocated by malloc free(rgb_image); } /** * Function: boxBlur * Description: Sets each pixel in an image to the average of the pixels in a box around it * input: A 3D array that holds the values to be averaged * height: The height of the image * width: The width of the image * boxSize: The width of the box around a pixel to average * output: A 3D array that holds the averaged values */ void boxBlur(const uint8_t input[MAX_HEIGHT][MAX_WIDTH][3], int height, int width, int boxSize, uint8_t output[MAX_HEIGHT][MAX_WIDTH][3]) { int i, j, k, x, y; // For each pixel in the input for (i = 0; i < height; i++) { for (j = 0; j < width; j++) { // Determine X and Y min/max bounds for averaging int xmin = MAX(0, j - boxSize / 2); int xmax = MIN(width, j + boxSize / 2); int ymin = MAX(0, i - boxSize / 2); int ymax = MIN(height, i + boxSize / 2); // For each color channel for (k = 0; k < 3; k++) { // Start the sum of the box at 0 int sum = 0; // Add up each pixel in the box for (y = ymin; y < ymax; y++) { for (x = xmin; x < xmax; x++) { sum += input[y][x][k]; } } // Assign the average of the box to the proper place in output output[i][j][k] = sum / ((ymax - ymin) * (xmax - xmin)); } } } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!