int paint_histogram(int width, int height, unsigned char* image, char* filename) { unsigned char *histoimg = calloc(1, width*height); double histo[256]={}; int i=0, j=0; for(i=0; i<256; i++) histo[i]=0; for(i=0; i<width*height; i++) histo[image[i]]++; for(i=0; i<256; i++) { histo[i]/=65536; histo[i]*=255*40; } for(i=0; i<256; i++) { for(j=0; j<histo[i]; j++) { if(j>255) break; histoimg[i*width+j]=238; } } write_pgm_image(filename, width, height, histoimg); free(histoimg); histoimg=NULL; return 0; }
void save_pgm_image(char *path, unsigned char *image, int nx, int ny) { char dataOut[MAXLEN], header[MAXLEN]; int sizeOutput, nHead; FILE *outfile; write_pgm_image((FILE *) NULL, path, image, nx, ny); }
/* The main host program controlling and representing the whole application */ int main(int argc, char* argv[]) { int image[HEIGHT][WIDTH]; unsigned char output[HEIGHT][WIDTH]; // Initialize with some values init_image(WIDTH, HEIGHT, image); // Draw 70 horizontal lines and map operation on 8 PEs: #pragma omp parallel for num_threads(8) for(int proc = 0; proc < 70; proc++) // Each iteration is on a different PE in parallel: #pragma smecy map(PE, proc & 7) \ arg(2, in, [1][LINE_SIZE]) \ arg(3, out, [1][LINE_SIZE]) // Invert an horizontal line: invert_vector(LINE_SIZE, &image[HEIGHT - 20 - proc][WIDTH/2 + 2*proc], &image[HEIGHT - 20 - proc][WIDTH/2 + 2*proc]); /* Here we guess we have 5 hardware accelerators and we launch operations on them: */ #pragma omp parallel for num_threads(5) for(int proc = 0; proc < 5; proc++) { /* This is need to express the fact that our accelerator only accept continuous data but we want apply them on non contiguous data in the array */ int input_line[LINE_SIZE]; int output_line[LINE_SIZE]; /* We need to remap data in the good shape. The compiler should use the remapping information to generate DMA transfer for example and remove input_line array */ SMECY_remap_int2D_to_int1D(HEIGHT, WIDTH, HEIGHT/3, 30 + 20*proc, LINE_SIZE, 1, image, LINE_SIZE, input_line); // Each iteration is on a different PE in parallel: #pragma smecy map(PE, proc) arg(2, in, [LINE_SIZE]) arg(3, out, [LINE_SIZE]) invert_vector(LINE_SIZE, input_line, output_line); SMECY_remap_int1D_to_int2D(LINE_SIZE, output_line, HEIGHT, WIDTH, HEIGHT/3, 30 + 20*proc, LINE_SIZE, 1, image); } // Convert int image to char image: normalize_to_char(WIDTH, HEIGHT, image, output); write_pgm_image("remapping_example-output.pgm", WIDTH, HEIGHT, output); return EXIT_SUCCESS; }
/* The main host program controlling and representing the whole application */ int main(int argc, char* argv[]) { int image[HEIGHT][WIDTH]; unsigned char output[HEIGHT][WIDTH]; // Initialize with some values init(WIDTH, HEIGHT, image); for(in) { function_A(in_A, out_A); function_B(out_A, out_B); function_C(out_B, out_C); #pragma omp parallel sections { // On one processor // We rewrite a small part of image: #pragma smecy map(PE, 0) arg(3, inout, [HEIGHT][WIDTH] \ /[HEIGHT/3:HEIGHT/3 + HEIGHT/2 - 1] \ [WIDTH/8:WIDTH/8 + HEIGHT/2 - 1]) square_symmetry(WIDTH, HEIGHT, image, HEIGHT/2, WIDTH/8, HEIGHT/3); // On another processor #pragma omp section // Here let the compiler to guess the array size #pragma smecy map(PE, 1) arg(3, inout, /[HEIGHT/4:HEIGHT/4 + HEIGHT/2 - 1] \ [3*WIDTH/8:3*WIDTH/8 + HEIGHT/2 - 1]) square_symmetry(WIDTH, HEIGHT, image, HEIGHT/2, 3*WIDTH/4, HEIGHT/4); // On another processor #pragma omp section // Here let the compiler to guess the array size #pragma smecy map(PE, 1) arg(3, inout, /[2*HEIGHT/5:2*HEIGHT/5 + HEIGHT/2 - 1] \ [WIDTH/2:WIDTH/2 + HEIGHT/2 - 1]) square_symmetry(WIDTH, HEIGHT, image, HEIGHT/2, WIDTH/2, 2*HEIGHT/5); } // Here there is a synchronization because of the parallel part end // Since there normalize_to_char(WIDTH, HEIGHT, image, output); write_pgm_image("output.pgm", WIDTH, HEIGHT, output); return EXIT_SUCCESS; }
void write_image( Image_<PixelType, ResourceType>const& output_image, boost::filesystem::path const& pathname ){ // select pgm or ppm if( pathname.extension().native() == ".ppm" ){ // write the image write_ppm_image( output_image.getResource(), pathname ); } else if( pathname.extension().native() == ".pgm" ){ // output must be in PixelGray_u8 write_pgm_image( output_image.getResource(), pathname ); } else{ throw GEO::GeneralException(std::string("Unknown extension (")+pathname.extension().native()+")", std::string(__FILE__), __LINE__); } }
int main(int argc, char *argv[]) { FILE *fp; int w, h, i; if ((fp = fopen(argv[1], "r")) == NULL && argc == 1) { printf("ERROR: %s can't open %s!", argv[0], argv[1]); } else { if (read_pgm_hdr(fp, &w, &h) != -1) { struct image img, img_gauss, img_out; //img_scratch, img_scratch2, printf("*** PGM file recognized, reading data into image struct ***\n"); img.width = w; img.height = h; unsigned char *img_data = malloc(w * h * sizeof(char)); for (i = 0; i < w * h; i++) { img_data[i] = fgetc(fp); } img.pixel_data = img_data; img_out.width = img_gauss.width = w; img_out.height = img_gauss.height = h; unsigned char *img_gauss_data = malloc(w * h * sizeof(char)); img_gauss.pixel_data = img_gauss_data; unsigned char *img_out_data = malloc(w * h * sizeof(char)); img_out.pixel_data = img_out_data; printf("*** image struct initialized ***\n"); printf("*** performing gaussian noise reduction ***\n"); gaussian_noise_reduce(&img, &img_gauss); //printf("*** performing morphological closing ***\n"); //morph_close(&img, &img_scratch, &img_scratch2, &img_gauss); canny_edge_detect(&img_gauss, &img_out); write_pgm_image(&img_out); free(img_data); free(img_gauss_data); free(img_out_data); } else { printf("ERROR: %s is not a PGM file!", argv[1]); } } return(1); }
int canny(int j) { int w, h, i,u; unsigned char yo[522240]; u = 0; if(u==1) { readpicture(yo,j); img.pixel_data = yo; } else { //img.pixel_data = CurrentFrame.framebits; } w = 960; h = 544; img.width = w; img.height = h; img_out.width = 960; img_out.height = 544; unsigned char *img_gauss_data = malloc(w * h * sizeof(char)); img_gauss.pixel_data = img_gauss_data; gaussian_noise_reduce(&img, &img_gauss); //printf("*** performing morphological closing ***\n"); //morph_close(&img, &img_scratch, &img_scratch2, &img_gauss); canny_edge_detect(&img_gauss, &img); write_pgm_image(&img,j); free(img_gauss_data); return(1); }
main(int argc, char *argv[]) { char *infilename = NULL; /* Name of the input image */ char *dirfilename = NULL; /* Name of the output gradient direction image */ char outfilename[128]; /* Name of the output "edge" image */ char composedfname[128]; /* Name of the output "direction" image */ unsigned char *image; /* The input image */ unsigned char *edge; /* The output edge image */ int rows, cols; /* The dimensions of the image. */ float sigma, /* Standard deviation of the gaussian kernel. */ tlow, /* Fraction of the high threshold in hysteresis. */ thigh; /* High hysteresis threshold control. The actual threshold is the (100 * thigh) percentage point in the histogram of the magnitude of the gradient image that passes non-maximal suppression. */ /**************************************************************************** * Get the command line arguments. ****************************************************************************/ if(argc < 5){ fprintf(stderr,"\n<USAGE> %s image sigma tlow thigh [writedirim]\n",argv[0]); fprintf(stderr,"\n image: An image to process. Must be in "); fprintf(stderr,"PGM format.\n"); fprintf(stderr," sigma: Standard deviation of the gaussian"); fprintf(stderr," blur kernel.\n"); fprintf(stderr," tlow: Fraction (0.0-1.0) of the high "); fprintf(stderr,"edge strength threshold.\n"); fprintf(stderr," thigh: Fraction (0.0-1.0) of the distribution"); fprintf(stderr," of non-zero edge\n strengths for "); fprintf(stderr,"hysteresis. The fraction is used to compute\n"); fprintf(stderr," the high edge strength threshold.\n"); fprintf(stderr," writedirim: Optional argument to output "); fprintf(stderr,"a floating point"); fprintf(stderr," direction image.\n\n"); exit(1); } infilename = argv[1]; sigma = atof(argv[2]); tlow = atof(argv[3]); thigh = atof(argv[4]); if(argc == 6) dirfilename = infilename; else dirfilename = NULL; /**************************************************************************** * Read in the image. This read function allocates memory for the image. ****************************************************************************/ if(VERBOSE) printf("Reading the image %s.\n", infilename); if(read_pgm_image(infilename, &image, &rows, &cols) == 0){ fprintf(stderr, "Error reading the input image, %s.\n", infilename); exit(1); } /**************************************************************************** * Perform the edge detection. All of the work takes place here. ****************************************************************************/ if(VERBOSE) printf("Starting Canny edge detection.\n"); if(dirfilename != NULL){ sprintf(composedfname, "%s_s_%3.2f_l_%3.2f_h_%3.2f.fim", infilename, sigma, tlow, thigh); dirfilename = composedfname; } canny(image, rows, cols, sigma, tlow, thigh, &edge, dirfilename); /**************************************************************************** * Write out the edge image to a file. ****************************************************************************/ sprintf(outfilename, "%s_s_%3.2f_l_%3.2f_h_%3.2f.pgm", infilename, sigma, tlow, thigh); if(VERBOSE) printf("Writing the edge iname in the file %s.\n", outfilename); if(write_pgm_image(outfilename, edge, rows, cols, "", 255) == 0){ fprintf(stderr, "Error writing the edge image, %s.\n", outfilename); exit(1); } }
int main(int argc, char *argv[]) { char *infilename = NULL; /* Name of the input image */ char *dirfilename = NULL; /* Name of the output gradient direction image */ char outfilename[128]; /* Name of the output "edge" image */ char composedfname[128]; /* Name of the output "direction" image */ unsigned char *image; /* The input image */ unsigned char *edge; /* The output edge image */ int rows, cols; /* The dimensions of the image. */ float sigma=2.5, /* Standard deviation of the gaussian kernel. */ tlow=0.5, /* Fraction of the high threshold in hysteresis. */ thigh=0.5; /* High hysteresis threshold control. The actual threshold is the (100 * thigh) percentage point in the histogram of the magnitude of the gradient image that passes non-maximal suppression. */ /**************************************************************************** * Get the command line arguments. ****************************************************************************/ if(argc < 2) { fprintf(stderr,"\n<USAGE> %s image sigma tlow thigh [writedirim]\n",argv[0]); fprintf(stderr,"\n image: An image to process. Must be in "); fprintf(stderr,"PGM format.\n"); exit(1); } infilename = argv[1]; Timer totalTime; initTimer(&totalTime, "Total Time"); /**************************************************************************** * Read in the image. This read function allocates memory for the image. ****************************************************************************/ if(VERBOSE) printf("Reading the image %s.\n", infilename); if(read_pgm_image(infilename, &image, &rows, &cols) == 0) { fprintf(stderr, "Error reading the input image, %s.\n", infilename); exit(1); } /**************************************************************************** * Perform the edge detection. All of the work takes place here. ****************************************************************************/ if(VERBOSE) printf("Starting Canny edge detection.\n"); if(dirfilename != NULL) { sprintf(composedfname, "%s_s_%3.2f_l_%3.2f_h_%3.2f.fim", infilename, sigma, tlow, thigh); dirfilename = composedfname; } // MCPROF_START(); canny(image, rows, cols, sigma, tlow, thigh, &edge, dirfilename); // MCPROF_STOP(); /**************************************************************************** * Write out the edge image to a file. ****************************************************************************/ sprintf(outfilename, "%s_s_%3.2f_l_%3.2f_h_%3.2f.pgm", infilename, sigma, tlow, thigh); if(VERBOSE) printf("Writing the edge iname in the file %s.\n", outfilename); if(write_pgm_image(outfilename, edge, rows, cols, "", 255) == 0) { fprintf(stderr, "Error writing the edge image, %s.\n", outfilename); exit(1); } free(image); free(edge); return 0; }
int main(int argc, char** argv) { FILE *file = NULL; // image data array unsigned char Imagedata[Size*Size] = {}; char fname[1024]={}; if(argv[1] != NULL && strlen(argv[1])>0) strcpy(fname, argv[1]); else { fprintf(stderr, "please specify filename of raw input image.\n"); exit(-1); } if (!(file=fopen(fname,"rb"))) { fprintf(stderr, "Cannot open file!\n"); exit(1); } fread(Imagedata, sizeof(unsigned char), Size*Size, file); fclose(file); /* save the original image for comparision */ write_pgm_image("dip_hw1_p1_I.pgm", Size, Size, Imagedata); unsigned char imageD[Size*Size]={}; /* copy image data */ memcpy(imageD, Imagedata, sizeof(Imagedata)); /* decrease brightness (D) */ decrease_brightness(Size, Size, imageD); write_pgm_image("dip_hw1_p1_D.pgm", Size, Size, imageD); unsigned char imageH[Size*Size]={}; /* copy image data */ memcpy(imageH, imageD, sizeof(imageD)); /* histogram_equalizer (H) */ histogram_equalizer(Size, Size, imageH); write_pgm_image("dip_hw1_p1_H.pgm", Size, Size, imageH); /* here loop create local histogram equalizer with different window size */ /* change w_size to whatever 0< number <256 */ /* int w_size=50; unsigned char imageL[Size*Size]={}; char file_name[1024]={}; for( int w_size=10; w_size<250; w_size+=10) { memset(imageL, 0, sizeof(imageL)); for(int i=0; i<Size; i++) { for(int j=0; j<Size; j++) { // just ignore the boarder & corner local_histogram_equalizer(w_size, Size, Size, i, j, imageD, imageL); } } sprintf(file_name, "solved_c_%d.pgm", w_size); write_pgm_image(file_name, Size, Size, imageL); } */ int i=0, j=0, w_size=10; unsigned char imageL[Size*Size]={}; for(i=0; i<Size; i++) for(j=0; j<Size; j++) local_histogram_equalizer(w_size, Size, Size, i, j, imageD, imageL); write_pgm_image("dip_hw1_p1_L_10.pgm", Size, Size, imageL); paint_histogram(Size, Size, imageL, "dip_hw1_p1_histogram_L_10.pgm"); memset(imageL, 0, Size*Size); for(i=0; i<Size; i++) for(j=0; j<Size; j++) local_histogram_equalizer(30, Size, Size, i, j, imageD, imageL); write_pgm_image("dip_hw1_p1_L_30.pgm", Size, Size, imageL); paint_histogram(Size, Size, imageL, "dip_hw1_p1_histogram_L_30.pgm"); memset(imageL, 0, Size*Size); for(i=0; i<Size; i++) for(j=0; j<Size; j++) local_histogram_equalizer(120, Size, Size, i, j, imageD, imageL); write_pgm_image("dip_hw1_p1_L_120.pgm", Size, Size, imageL); paint_histogram(Size, Size, imageL, "dip_hw1_p1_histogram_L_120.pgm"); memset(imageL, 0, Size*Size); for(i=0; i<Size; i++) for(j=0; j<Size; j++) local_histogram_equalizer(180, Size, Size, i, j, imageD, imageL); write_pgm_image("dip_hw1_p1_L_180.pgm", Size, Size, imageL); paint_histogram(Size, Size, imageL, "dip_hw1_p1_histogram_L_180.pgm"); paint_histogram(Size, Size, Imagedata, "dip_hw1_p1_histogram_I.pgm"); paint_histogram(Size, Size, imageD, "dip_hw1_p1_histogram_D.pgm"); paint_histogram(Size, Size, imageH, "dip_hw1_p1_histogram_H.pgm"); unsigned char imageLog[Size*Size] = {}; unsigned char imageILog[Size*Size] = {}; unsigned char imagePlaw[Size*Size] = {}; memcpy(imageLog, imageD, sizeof(imageD)); memcpy(imageILog, imageD, sizeof(imageD)); memcpy(imagePlaw, imageD, sizeof(imageD)); /* log_c is the constant for log transform */ const int log_c = 35; log_transform(log_c, Size, Size, imageLog); write_pgm_image("dip_hw1_p1_D_log.pgm", Size, Size, imageLog); paint_histogram(Size, Size, imageLog, "dip_hw1_p1_histogram_D_log.pgm"); const int log_d = 240; inverse_log_transform(log_d, Size, Size, imageILog); write_pgm_image("dip_hw1_p1_D_ilog.pgm", Size, Size, imageILog); paint_histogram(Size, Size, imageILog, "dip_hw1_p1_histogram_D_ilog.pgm"); double pow = 30; double gamma = 0.5; power_law_transform(pow, gamma, Size, Size, imagePlaw); write_pgm_image("dip_hw1_p1_D_pow.pgm", Size, Size, imagePlaw); paint_histogram(Size, Size, imagePlaw, "dip_hw1_p1_histogram_D_pow.pgm"); pow = 1; gamma = 1.5; memcpy(imagePlaw, imageD, sizeof(imageD)); power_law_transform(pow, gamma, Size, Size, imagePlaw); write_pgm_image("dip_hw1_p1_D_pow1.pgm", Size, Size, imagePlaw); paint_histogram(Size, Size, imagePlaw, "dip_hw1_p1_histogram_D_pow1.pgm"); unsigned char imageOtsu[Size*Size]={}; int thre = otsu_method(Size, Size, Imagedata); fprintf(stderr, " thresold = %d \n", thre); memcpy(imageOtsu, Imagedata, sizeof(imageOtsu)); convert_to_black_n_white(thre, Size, Size, imageOtsu); write_pgm_image("dip_hw1_p1_I_otsu.pgm", Size, Size, imageOtsu); memcpy(imageOtsu, Imagedata, sizeof(imageOtsu)); convert_to_black_n_white(64, Size, Size, imageOtsu); write_pgm_image("dip_hw1_p1_I_64.pgm", Size, Size, imageOtsu); memcpy(imageOtsu, Imagedata, sizeof(imageOtsu)); convert_to_black_n_white(128, Size, Size, imageOtsu); write_pgm_image("dip_hw1_p1_I_128.pgm", Size, Size, imageOtsu); memcpy(imageOtsu, Imagedata, sizeof(imageOtsu)); convert_to_black_n_white(192, Size, Size, imageOtsu); write_pgm_image("dip_hw1_p1_I_192.pgm", Size, Size, imageOtsu); exit(0); return 0; }