/* getPuzzle.c * * finds the puzzle board on the image and outputs the puzzle image * * Input: one GrayImage * * * by Victor A. Dan, Dan Knights, Laura Matefy **** for CX 336 Project */ static char usage[] = "usage:\n %s inputPuzzleBigImage\n"; #include #include /* for INT_MAX */ #include #include #include "mcimg.h" /* comment out for speed later! */ #define IMAGE_CHECKALL #define thresh 60 #define err 7 /*the boundaries of the initial image may contain some black pixles - to avoid them we should start with the scan lines a bit further*/ #define threshH 200 #define mean 10 int findX1(GrayImage im){ int x =err, y=0, i; int siz=0, X[1000]={0}, Sum=0; int width = imGetWidth(im); int height = imGetHeight(im); if(height/2>mean) { for(y=height/2-mean; ythresh && x < width-1){ x++; } X[siz]=x; siz++; } for(i=0; i<2*mean; i++) Sum=Sum+X[i]; //fprintf(stderr, "%d, %d\n", Sum/mean, siz); return Sum/(2*mean); } else return 0; } int findY1(GrayImage im){ int x =err, y=0, i; int siz=0, Y[1000]={0}, Sum=0; int width = imGetWidth(im); int height = imGetHeight(im); if(width/2>mean) { for(x=width/2-mean; xthresh && ymean) { for(y=height/2-mean; ythresh && x>=0){ x--; } X1[siz]=x; siz++; } for(i=0; i<2*mean; i++) Sum=Sum+X1[i]; //fprintf(stderr, "%d, %d\n", Sum/mean, siz); return Sum/(2*mean); } else return 0; } int length(int X1, int X2){ return ABS(X2-X1); } int main(int argc, char **argv) { char *im1name; GrayImage im1, out; int width, X1, Y1, X2, i, j, argnum=1, width1, height1; if (argc!= 2) { fprintf(stderr, usage, argv[0]); exit(1); } im1name = argv[argnum++]; im1 = loadGrayImage(im1name); width1 = imGetWidth(im1); height1 = imGetHeight(im1); X1=findX1(im1); Y1=findY1(im1); X2=findX2(im1); width = length(X1, X2); if(width <=0) exit(0); else { out = newGrayImage(width, width); for(j=0; j