/* findcrosshair.cpp */ static char usage[] = "usage: %s in.png [out.png]\n"; #include #include #include "imageLib.h" // multiply by 100 and add random integer in [-49..49] inline int randoff(int n) { return 100 * n + 49 - (rand() % 99); } int inbounds(int x, int y, int w, int h) { return x >= 0 && x < w && y >= 0 && y < h; } void enhanceContrast(CByteImage im) { CShape sh = im.Shape(); int width = sh.width, height = sh.height, x, y; int max = 0; for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { max = __max(max, im.Pixel(x, y, 0)); } } // map max intensity to 220 float scale = 220.0 / max; for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { im.Pixel(x, y, 0) = (int)(scale * im.Pixel(x, y, 0)); } } } int main(int argc, char **argv) { int verbose = 0; if (argc < 2) { fprintf(stderr, usage, argv[0]); exit(1); } int optind = 1; char *imname = argv[optind++]; char *outname = argc > 2? argv[optind++] : NULL; try { CByteImage im; ReadImageVerb(im, imname, verbose); im = ConvertToGray(im); CShape sh = im.Shape(); int width = sh.width, height = sh.height, x, y; sh.nBands = 2; CIntImage cnt(sh); cnt.ClearPixels(); for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { cnt.Pixel(x, 0, 0) += im.Pixel(x, y, 0); cnt.Pixel(0, y, 1) += im.Pixel(x, y, 0); } } for (x = 4; x < width-4; x++) cnt.Pixel(x, 1, 0) = 7*cnt.Pixel(x, 0, 0) - cnt.Pixel(x-2, 0, 0) - cnt.Pixel(x-3, 0, 0) - cnt.Pixel(x-4, 0, 0) - cnt.Pixel(x+2, 0, 0) - cnt.Pixel(x+3, 0, 0) - cnt.Pixel(x+4, 0, 0); for (y = 4; y < height-4; y++) cnt.Pixel(1, y, 1) = 7*cnt.Pixel(0, y, 1) - cnt.Pixel(0, y-2, 1) - cnt.Pixel(0, y-3, 1) - cnt.Pixel(0, y-4, 1) - cnt.Pixel(0, y+2, 1) - cnt.Pixel(0, y+3, 1) - cnt.Pixel(0, y+4, 1); int maxx = 0, maxy = 0; for (x = 5; x < width-5; x++) maxx = __max(maxx, cnt.Pixel(x, 1, 0)); for (y = 5; y < height-5; y++) maxy = __max(maxy, cnt.Pixel(1, y, 1)); //im.ClearPixels(); float bestval = -1e20; int bestx = 0, besty = 0; for (y = 5; y < height-5; y++) { for (x = 5; x < width-5; x++) { float val = (float(cnt.Pixel(x, 1, 0) / (float)maxx) * float(cnt.Pixel(1, y, 1) / (float)maxy)); //im.Pixel(x, y, 0) = __max(0, __min(255, int(255 * val))); if (val > bestval) { bestval = val; bestx = x; besty = y; } } } enhanceContrast(im); // mark max with "X" for (int k = -8; k <= 8; k++) { if (inbounds(bestx+k, besty+k, width, height)) im.Pixel(bestx+k, besty+k, 0) = 255; if (inbounds(bestx+k, besty-k, width, height)) im.Pixel(bestx+k, besty-k, 0) = 255; } if (outname != NULL) WriteImageVerb(im, outname, verbose); //printf("max at %d, %d\n", bestx, besty); //printf("bestval = %f\n", bestval); printf("%d %d\n", bestx, besty); } catch (CError &err) { fprintf(stderr, err.message); fprintf(stderr, "\n"); exit(1); } return 0; }