void process_image(PNG_Canvas_BW& image) { int width = image.get_width(); int height = image.get_height(); double kernel[5][5]; double sum = 0; for (int x=0; x< 5; x++) { for (int y = 0; y < 5; y++) { kernel[x][y] = exp((-(x*x)+(y*y))/(2*sigma*sigma)); sum += kernel[x][y]; } } /* for (int x = 0; x<5; x++){ for(int y = 0; y < 5; y++){ printf(" %f ",kernel[x][y]); } printf("\n"); } printf("%f\n",sum); */ int K = 2; double s = 1.0/sum; //Make a new image canvas for the output to avoid conflicts PNG_Canvas_BW outputImage(width,height); for (int y = K; y < height-K-1; y++) { for (int x = K; x < width-K-1; x++) { int sum = 0; for (int j = -K; j <= K; j++) { for (int i = -K; i <= K; i++) { int p = image.get_pixel(x+i,y+j); int c = kernel[j+K][i+K]; sum += c * p; } } int q = round(s*sum); if (q < 0) q = 0; if (q > 255) q = 255; outputImage.set_pixel(x,y,q); } } image = outputImage; }
void invert_image(PNG_Canvas_BW& image) { int width = image.get_width(); int height = image.get_height(); //Make a new image canvas for the output to avoid conflicts PNG_Canvas_BW outputImage(width, height); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { outputImage[x][y] = 255 - image[x][y]; } } //Copy the result back into the provided image image = outputImage; }
int main(int argc, char** argv) { if (argc < 3) { cerr << "Usage: " << argv[0] << " <input file> <output file>" << endl; return 0; } string input_filename = argv[1]; string output_filename = argv[2]; PNG_Canvas_BW canvas; if (!canvas.load_image(input_filename)) { cerr << "Unable to load " << input_filename << ". Exiting..." << endl; return 0; } invert_image(canvas); canvas.save_image(output_filename); }
void process_image(PNG_Canvas_BW& image){ int width = image.get_width(); int height = image.get_height(); //image dilatate with and then erode with H //Make a new image canvas for the output to avoid conflicts int new_w = width_in; int new_h = height_in; PNG_Canvas_BW imagenew(new_w,new_h); float wnw = ((double) width)/((double)new_w); float hnh = ((double) height)/((double)new_h); //printf("%f",wnw); for (int x = 0; x < new_w; x++){ for (int y = 0; y < new_h; y++){ int x0 = floor(x * wnw); int y0 = floor(y*hnh); int x1 = ceil(x*wnw); int y1 = ceil(y*hnh); int xs = x - x0; int ys = y - y0; int dx = 1 - xs; int dy = 1 - ys; int p0 = (image.get_pixel(x0,y0)*dx)+(image.get_pixel(x1,y0)*xs); int p1 = (image.get_pixel(x0,y1)*dx)+(image.get_pixel(x1,y1)*xs); int result = (p0 * dy) + (p1 *ys); if (result > 255) result = 128; if (result < 0) result = 128; imagenew.set_pixel(x,y,result); } } //Copy the result back into the provided image image = imagenew; }
int main(int argc, char** argv) { if (argc < 3) { cerr << "Usage: " << argv[0] << " <input file> <output file>" << endl; return 0; } string input_filename = argv[1]; string output_filename = argv[2]; if (argc == 4) { sscanf(argv[3],"%lf",&sigma); } PNG_Canvas_BW canvas; if (!canvas.load_image(input_filename)) { cerr << "Unable to load " << input_filename << ". Exiting..." << endl; return 0; } process_image(canvas); canvas.save_image(output_filename); }
void process_image(PNG_Canvas_BW& image){ int width = image.get_width(); int height = image.get_height(); int h[256] = {0}; for (int x =0; x < width; x++){ for (int y = 0; y < height; y++){ h[image.get_pixel(x,y)] = h[image.get_pixel(x,y)]+1; } } int H[256] = {0}; for (int x =0; x < width; x++){ for (int y = 0; y < height; y++){ H[image[x][y]] = H[image[x][y]]+1; } } H[0] = h[0]; for (int a = 1; a < 256; a++){ H[a] = H[a-1] + H[a]; } //gaussian distribution double normal[256] = {0}; int sigma = 50; int m = 128; float sigmasqr2pi = sigma * sqrt(2*M_PI); float twosigma2 = 2 * sigma * sigma; for (int x = 0; x< 256; x++){ normal[x] = exp((-pow((x-m),2))/twosigma2)/(sigmasqr2pi); } //inversed bell curve double gx[256] = {0}; for (int b = 0; b < 256; b ++){ int sigma = 50; int m = 128; float sigmasqr2pi = sigma * sqrt(2*M_PI); float twosigma2 = 2 * sigma * sigma; float gm = exp((-pow((m-m),2))/twosigma2)/(sigmasqr2pi); gx[b] = gm - normal[b]; //printf("%f\n",gx[b]); } for (int b = 1; b < 256; b++){ gx[b] = gx[b-1]+gx[b]; } //for (int i = 0; i < 256; i++){ // printf("%f\n",gx[i]); //} int F[256] = {0}; int n = width * height; int r = n/1; int i = 0; int c = 0; int j = 0; while (i < 256){ if (c <= r*gx[j]){ //printf("%f\n", r*normal[i]); c += h[i]; F[i] = j; i = i + 1; } else { j = j + 1; } } //Make a new image canvas for the output to avoid conflicts PNG_Canvas_BW outputImage(width,height); for (int y = 0; y < height; y++){ for (int x = 0; x < width; x++){ outputImage[x][y] = F[image[x][y]]; } } image = outputImage; }