I wrote code to invert, threshold, change luminosity, rotate, reflect, frame, box blur, Gaussian blur, sharpen, and edge detect an image.
I tried to write code to change the contrast but I couldn't manage to do it in a reliable manner.
Here's the explanation of the code.
Resource to explain convolving/kernels
The first section of the code is all my teacher's work, except where I define the arrays, put a loop on the theImageArrayDup to duplicate the array, and call my imageProcessing code. Aside from that the rest of it is my code.
Here's Default Irv (my professor's kitty), for comparison
Convolution UI
Convolution is essentially the idea of taking one pixel, and manipulating it based on the surrounding pixels. You use a matrix to manipulate the pixel based on its surrounding pixels. Because accounting for the edge is too much work, I simply let my loops start at 1 instead of 0 to ignore the outermost pixels.
Box Blur
It's hard to notice on the small image but it's there. If you do this in Photoshop with a 1px box blur, you'll get the exact same effect.
Gaussian Blur
Again, hard to notice on the small image but it's there. Oddly enough doing this in Photoshop with a 1px Gaussian blur does not give the same effect.
Edge Detection
Works a little differently than that in Photoshop. But still picks up on edges fairly well. Does not threshold despite looking like it did.
Sharpen
This one got a little screwy and ended up being washed out and otherwise looking like the edge detection algorithm.
Framing UI
Framing
Invert UI
This one is literally 3 lines of code, if you're feeling lazy.
Invert
Luminosity UI
Lighten
Darken
Transform UI
Reflect Horizontal
Reflect Vertical
Rotate 90 Degrees Clockwise
Rotate 180 Degrees Clockwise
Rotate 270 Degrees Clockwise
Threshold UI
I tried to make this flexible but couldn't manage to do it with flexible if statements so I just hard coded these cases, as after 6 levels, it doesn't make much of a difference anyway.
Threshold Lv 2
Threshold Lv 3
Threshold Lv 4
Threshold Lv 5
Threshold Lv 6
// Read a PGM-format file into an array // PGM file format is documented here: // http://en.wikipedia.org/wiki/Netpbm_format // // Here's a short example: // // P2 // # a comment line // 24 7 // 3 // 0 0 0 0 1 1 1 0 0 0 2 2 2 2 0 0 0 0 3 3 3 3 3 0 // ... 6 more rows of pixel data // // The P2 means a gray-scale image // Comment lines start with # and can be ignored // 24 means 24 wide // 7 means 7 high // 3 means the maximum pixel value is 3 in this example #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> //typedef unsigned short int word ; //typedef unsigned char byte ; // Maximum size image we will process (larger ones // get truncated to the upper left corner), or may "wrap" // in interesting ways... #define MAX_HEIGHT 1025 #define MAX_WIDTH 1025 // allows writing multiple pixels per line to the // output PGM file. Looks like many programs that // use the PGM format just keep this at one. It // should not be more than 70 in any event. #define MAX_PIXELS_PER_LINE 25 // Useful macro def #define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) ) // This is the array that will contain the image // within your program. //*** ???????????????????????????????? // You will need an identical array to use to hold // the output of your image transformation //*** ???????????????????????????????? // ME DEFINING THEIMAGEARRAY int theImageArray[1025][MAX_WIDTH]; int theImageArrayDup[1025][MAX_WIDTH]; // Internal function to read the header information // stored at the beginning of a PGM format file. // // Used by read_pgm_file_into_array to see set the // height and width of the image FILE* read_pgm_file_info ( int* height, // output int* width, // output int* maxPixel, // output char* pgmFname // input ) { static FILE* fp = 0L ; char pgmFormatFlag[3] = { '\0' } ; char trash[80] = { '\0' } ; memset ( ( void * ) trash, '\0', 80 ) ; fp = fopen ( pgmFname, "r" ) ; if ( fp ) { // Check the signature fscanf ( fp, "%2c\n", pgmFormatFlag ) ; //printf ( "%s\n", pgmFormatFlag ) ; if ( ! strcmp ( pgmFormatFlag, "P2" ) ) { // Skip the comment line //fscanf ( fp, "\n" ) ; fgets ( trash, 70, fp ) ; //printf ( "%s", trash ) ; // Read the width fscanf ( fp, "%i", width ) ; //printf ( "%i,", *width ) ; // Read the height fscanf ( fp, "%i", height ) ; //printf ( "%i\n", *height ) ; // Read the maximum pixel value fscanf ( fp, "%i", maxPixel ) ; } //fclose ( fp ) ; } return fp ; } // Write out an array as a PGM file (ascii). // writes imageArray to the file named by pgmOutFileName. // commentLine, height, width, and maxPixel must be // set and passed to this function so they can // be written correctly in the PGM file header and // so this function will know how many rows (image height) // and how many columns (image width) to write. void write_pgm_file_from_array ( char* pgmOutFileName, // input int imageArray[][MAX_WIDTH], // input char* commentLine, // input int height, // input int width, // input int maxPixel // input ) { int row = 0 ; int col = 0 ; FILE* fp = fopen ( pgmOutFileName, "w" ) ; if ( fp ) { // Generate the header info fprintf ( fp, "P2\n" ) ; fprintf ( fp, "%s\n", commentLine ) ; fprintf ( fp, "%u %u\n", width, height ) ; fprintf ( fp, "%u\n", maxPixel ) ; // Now write out the data, ensuring that at most 70 // values appear on each line, even if width is > 70 for ( row = 0 ; row < height ; row ++ ) { for ( col = 0 ; col < width ; col ++ ) { fprintf ( fp, "%u", imageArray[row][col] ) ; // break up long rows into multiple lines as needed if ( MAX_PIXELS_PER_LINE > 1 ) { fprintf ( fp, " " ) ; } if ( ( col % MAX_PIXELS_PER_LINE ) == 0 ) { fprintf ( fp, "\n" ) ; } } // Watch out of special case of width == 70 if ( col % MAX_PIXELS_PER_LINE ) { fprintf ( fp, "\n" ) ; } } fclose ( fp ) ; } return ; } // Read file named by pgmInFileName argument into // the array imageArray. This function respects the // MAX_HEIGHT and MAX_WIDTH values, so if you declare // your target array using these bounds, even reading // a large image file should not blow up your program. // (but you will only get the upper left-hand corner // of the image). Checks that the file read is a PGM // (gray scale, ascii) file. // // Sets height, width, and maxPixel according to the // header read. void read_pgm_file_into_array ( int imageArray[][MAX_WIDTH], // output int* height, // output int* width, // output int* maxPixel, // output char* pgmInFileName // input ) { int row = 0 ; int col = 0 ; FILE* fp = read_pgm_file_info ( height, width, maxPixel, pgmInFileName ) ; char trash = ' '; char yesThreshold = ' '; if ( fp ) { printf ( "reading height=%d, width=%d\n", *height, *width ) ; for ( row = 0 ; row < MIN( MAX_HEIGHT - 1, *height ) ; row ++ ) { for ( col = 0 ; col < MIN( MAX_WIDTH -1, *width ) ; col ++ ) { fscanf ( fp, "%i", &imageArray[row][col]) ; } } fclose ( fp ) ; } return ; } int main( void ) { int height = 0 ; int width = 0 ; int maxPixel = 0 ; //char* pgmInFileName = "lynx_central.pgm" ; //char* pgmOutFileName = "lynx_central_out.pgm" ; char* pgmInFileName = "irv.pgm" ; char* pgmOutFileName = "irv_out.pgm" ; // Read input file into array, then write it back out, // just to prove this all works. Input file and output // file should look just the same. (sizes on disk might // vary slightly) read_pgm_file_into_array (theImageArray, &height, &width, &maxPixel, pgmInFileName ) ; //**** //**** Your code to transform the image goes here ... for(int i = 0; i < 1025; i++){ for(int j = 0; j < MAX_WIDTH; j++){ theImageArrayDup[i][j] = theImageArray[i][j]; } } imageProcessing(theImageArray, theImageArrayDup,height,width); //Transfer the current values in theImageArray back to disk write_pgm_file_from_array ( pgmOutFileName, theImageArray, "# JR test file", height, width, maxPixel ) ; printf ( "Copying %s to %s, height=%u, width=%u, maxPixel=%d\n", pgmInFileName, pgmOutFileName, height, width, maxPixel ) ; //system("pause"); return 0 ; }
#define MAX_HEIGHT 1025 #define MAX_WIDTH 1025 #include <stdio.h> #include <math.h> #include <string.h> void convolve(int theImageArray[1025][1025], int theImageArrayDup[1025][1025], int height, int width){ int boxBlur[3][3] = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } }; int gaussian[3][3] = { {1, 2, 1}, {2, 4, 2}, {1, 2, 1} }; int edgeDetect[3][3] = { {-1, -1, -1}, {-1, 5, -1}, {-1, -1, -1} }; int sharpen[3][3] = { {0, -1, 0}, {-1, 5, -1}, {0, -1, 0} }; char convolution[30]; printf("Would you like to run box blur, Gaussian blur, edge detection, or sharpen?\n(Box, Gaussian, Edge, Sharpen) \n"); scanf("%s", convolution); lowerletter(convolution); if(strcmp(convolution, "box") == 0){ for (int i = 1; i < MAX_HEIGHT; i++) { for (int j = 1; j < MAX_WIDTH; j++) { conMatrixMult(theImageArray, theImageArrayDup, boxBlur, i, j, 9, 0); } } } if(strcmp(convolution, "gaussian") == 0){ for (int i = 1; i < MAX_HEIGHT; i++) { for (int j = 1; j < MAX_WIDTH; j++) { conMatrixMult(theImageArray, theImageArrayDup, gaussian, i, j, 16, 0); } } } if(strcmp(convolution, "edge") == 0){ for (int i = 1; i < MAX_HEIGHT; i++) { for (int j = 1; j < MAX_WIDTH; j++) { conMatrixMult(theImageArray, theImageArrayDup, edgeDetect, i, j, 128, 0); } } } if(strcmp(convolution, "sharpen") == 0){ for (int i = 1; i < MAX_HEIGHT; i++) { for (int j = 1; j < MAX_WIDTH; j++) { conMatrixMult(theImageArray, theImageArrayDup, sharpen, i, j, 8, -50); /*if((2*theImageArray[i][j])<= 255){ theImageArray[i][j] = (2*theImageArray[i][j]); } else{ theImageArray[i][j] = 255; }*/ theImageArray[i][j] = 2*(theImageArray[i][j] -50) + 100; if (theImageArray[i][j] > 255) { theImageArray[i][j] = 255; } } } printf("Image is washed out, sorry. Couldn't figure out a way around that. It is sharpened though."); } } void conMatrixMult(int theImageArray[1025][1025], int theImageArrayDup[1025][1025], int myMatrix[3][3], int i, int j, int divisor, int subtractor){ theImageArray[i][j] = (theImageArray[i+1][j+1]*myMatrix[0][0] + theImageArray[i][j+1]*myMatrix[0][1]+ theImageArray[i-1][j+1]*myMatrix[0][2]+theImageArray[i+1][j]*myMatrix[1][0]+theImageArray[i][j]*myMatrix[1][1]+theImageArray[i-1][j]*myMatrix[1][2]+theImageArray[i+1][j-1]*myMatrix[2][0]+theImageArray[i][j+1]*myMatrix[2][1]+theImageArray[i-1][j-1]*myMatrix[2][2])/divisor - subtractor; } void invert(int theImageArray[1025][1025]){ for (int i = 1; i < MAX_HEIGHT; i++) { for (int j = 1; j < MAX_WIDTH; j++) { theImageArray[i][j] = 255 - theImageArray[i][j]; } } } void threshold(int theImageArray[1025][1025]) { int n; printf("Enter the number of levels you wish to threshold by. \n(Integer between 2-6 inclusive). \n"); scanf("%d", &n); for (int i = 0; i < MAX_HEIGHT; i++) { for (int j = 0; j < MAX_WIDTH; j++) { switch (n){ case 1: printf("Invalid response. Please start over. /n"); break; case 2: if (theImageArray[i][j] > (n-1)*255/n) { theImageArray[i][j] = 255; } else { theImageArray[i][j] = 0; } break; case 3: if (theImageArray[i][j] > (n-1)*255/n) { theImageArray[i][j] = 255; } else if (theImageArray[i][j] > (n-2)*(255/n)){ theImageArray[i][j] = (n-2)*(255/n); } else { theImageArray[i][j] = 0; } break; case 4: if (theImageArray[i][j] > (n-1)*255/n) { theImageArray[i][j] = 255; } else if (theImageArray[i][j] > (n-2)*(255/n)){ theImageArray[i][j] = (n-2)*(255/n); } else if (theImageArray[i][j] > (n-3)*(255/n)){ theImageArray[i][j] = (n-3)*(255/n); } else { theImageArray[i][j] = 0; } break; case 5: if (theImageArray[i][j] > (n-1)*255/n) { theImageArray[i][j] = 255; } else if (theImageArray[i][j] > (n-2)*(255/n)){ theImageArray[i][j] = (n-2)*(255/n); } else if (theImageArray[i][j] > (n-3)*(255/n)){ theImageArray[i][j] = (n-3)*(255/n); } else if (theImageArray[i][j] > (n-4)*(255/n)){ theImageArray[i][j] = (n-4)*(255/n); } else { theImageArray[i][j] = 0; } break; case 6: if (theImageArray[i][j] > (n-1)*255/n) { theImageArray[i][j] = 255; } else if (theImageArray[i][j] > (n-2)*(255/n)){ theImageArray[i][j] = (n-2)*(255/n); } else if (theImageArray[i][j] > (n-3)*(255/n)){ theImageArray[i][j] = (n-3)*(255/n); } else if (theImageArray[i][j] > (n-4)*(255/n)){ theImageArray[i][j] = (n-4)*(255/n); } else if (theImageArray[i][j] > (n-5)*(255/n)){ theImageArray[i][j] = (n-5)*(255/n); } else { theImageArray[i][j] = 0; } break; default: printf("Invalid response. Please start over. /n"); break; } } } } void luminosity(int theImageArray[1025][1025]) { char lum[10]; double perc; printf("Do you want to lighten or darken the image? (Lighten, Darken) \n"); scanf("%s", lum); lowerletter(lum); printf("Enter a percentage between 0 and 100 (without the %%) of how much you want\nthe luminosity to change by. \n"); scanf("%lf", &perc); perc = 0.01* perc; if(strcmp(lum, "lighten") == 0){ perc = 1.0 + perc; for (int i = 0; i < MAX_HEIGHT; i++) { for (int j = 0; j < MAX_WIDTH; j++) { if(perc*theImageArray[i][j]<= 255){ theImageArray[i][j] = perc*theImageArray[i][j]; } else{ theImageArray[i][j] = 255; } } } } if(strcmp(lum, "darken") == 0){ perc = 1.0 - perc; for (int i = 0; i < MAX_HEIGHT; i++) { for (int j = 0; j < MAX_WIDTH; j++) { theImageArray[i][j] = perc*theImageArray[i][j]; } } } } void frame(int theImageArray[1025][1025], int width, int height){ int startPosX; int endPosX; int startPosY; int endPosY; int lum; printf("Enter leftmost X position (0-%d): ", width); scanf("%d", &startPosX); printf("\nEnter rightmost X position (0-%d): ", width); scanf("%d", &endPosX); printf("\nEnter topmost Y position (0-%d): ", height); scanf("%d", &startPosY); printf("\nEnter bottommost Y position (0-%d): ", height); scanf("%d", &endPosY); printf("\n"); printf("Enter frame luminosity (0-255): "); scanf("%d", &lum); printf("\n"); for(int i = 0; i < MAX_WIDTH; i++){ for(int j = 0; j < startPosY; j++){ theImageArray[i][j] = lum; } } for(int i = 0; i < startPosX; i++){ for(int j = startPosY; j < MAX_HEIGHT; j++){ theImageArray[i][j] = lum; } } for(int i = startPosX; i < endPosX; i++){ for(int j = endPosY; j < MAX_HEIGHT; j++){ theImageArray[i][j] = lum; } } for(int i = endPosX; i < MAX_WIDTH; i++){ for(int j = startPosY; j < MAX_HEIGHT; j++){ theImageArray[i][j] = lum; } } } void transformation(int theImageArray[1025][1025], int theImageArrayDup[1025][1025], int width, int height){ char tfm[10]; int rotation; printf("Do you want to rotate the image or reflect the image? (Rotate, Reflect) \n"); scanf("%s", tfm); lowerletter(tfm); if (strcmp(tfm, "reflect") == 0) { printf("Do you want the image to be reflected vertically or horizontally?\n(Vertical, Horizontal)\n"); char direction[15]; scanf("%s", direction); lowerletter(direction); if(strcmp(direction, "vertical") == 0){ for(int i = 0; i < width; i++){ for(int j = 0; j < height; j++){ theImageArrayDup[i][j] = theImageArray[width-i][j]; } } for(int i = 0; i < width; i++){ for(int j = 0; j < height; j++){ theImageArray[i][j] = theImageArrayDup[i][j]; } } } if(strcmp(direction,"horizontal") == 0){ for(int i = 0; i < width; i++){ for(int j = 0; j < height; j++){ theImageArrayDup[i][j] = theImageArray[i][height-j]; } } for(int i = 0; i < width; i++){ for(int j = 0; j < height; j++){ theImageArray[i][j] = theImageArrayDup[i][j]; } } } } if(strcmp(tfm, "rotate") == 0) { printf("Do you want the image rotated 90, 180, or 270 degrees clockwise? "); scanf("%d", &rotation); if (rotation == 90){ for(int i = 0; i < MAX_WIDTH; i++){ for(int j = 0; j < MAX_HEIGHT; j++){ theImageArrayDup[i][j] = theImageArray[j][i]; } } for(int i = 0; i < MAX_WIDTH; i++){ for(int j = 0; j < MAX_HEIGHT; j++){ theImageArray[i][j] = theImageArrayDup[i][j]; } } } if(rotation == 180) { for(int i = 0; i < width; i++){ for(int j = 0; j < height; j++){ theImageArrayDup[i][j] = theImageArray[width-i][height-j]; } } for(int i = 0; i < width; i++){ for(int j = 0; j < height; j++){ theImageArray[i][j] = theImageArrayDup[i][j]; } } } if (rotation == 270){ for(int i = 0; i < width; i++){ for(int j = 0; j < height; j++){ theImageArrayDup[i][j] = theImageArray[height-j][i]; } } for(int i = 0; i < width; i++){ for(int j = 0; j < height; j++){ theImageArray[i][j] = theImageArrayDup[i][j]; } } } } }
#include <ctype.h>> #include <stdio.h> #include <stdlib.h> void imageProcessing(int theImageArray[1025][1025], int theImageArrayDup[1025][1025], int height, int width) { char inputFunc[30]; printf("Enter the function you wish to apply to your image. (Threshold, Luminosity,\nFraming, Transform, Convolve, Invert) \n"); scanf("%s", inputFunc); lowerletter(inputFunc); if(strcmp(inputFunc,"invert") == 0) { invert(theImageArray); } if(strcmp(inputFunc,"convolve") == 0) { convolve(theImageArray, height, width); } if(strcmp(inputFunc, "threshold") == 0){ threshold(theImageArray); } if(strcmp(inputFunc, "luminosity") == 0){ luminosity(theImageArray); } if(strcmp(inputFunc, "framing") == 0){ frame(theImageArray, height, width); } if(strcmp(inputFunc,"transform") == 0) { transformation(theImageArray, theImageArrayDup, height, width); } printf("\nDone\n"); } void lowerletter(char myString[30]){ for(int i = 0; i < strlen(myString); i++){ myString[i] = tolower(myString[i]); } }
It's awesome.can you share pgm and PDF in my I'd and it convert to ppt
ReplyDelete