// unoptimized to debug easier std::vector < bool** > find_all(gy_image_object _img){ std::vector < bool** > output; int W = _img.width(), H = _img.height(); pass = new int*[H]; img = new int*[H]; for(size_t i=0;i<H;i++){ pass[i] = new int[W]; img[i] = new int[W]; } for(int i=0;i<H;i++) for(int j=0;j<W;j++){ pass[i][j] = false; img[i][j] = _img.get_pixel(j, i); } for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ if(not pass[i][j] and img[i][j] <= THRESHOLD){ count_pixel = sum_x = sum_y = 0; fill(W,H,j,i); if(count_pixel < MINIMAL_PIXEL) continue; sum_x = (int)floor((double)sum_x/(double)count_pixel); sum_y = (int)floor((double)sum_y/(double)count_pixel); bool **A = get_all(W,H,sum_x,sum_y,LEFT,TOP,RIGHT,BOTTOM); output.push_back(A); } } } end: return output; }
void preprocessing_niblack(gy_image_object &img, int window_size) { int w = img.width(),h = img.height(),i,j,x1,x2,y1,y2,pix,sum,sd; int *QS; long long int *PWQS,pwsum; QS = (int*) malloc (w * h * sizeof(int)); PWQS = (long long int*) malloc (w * h * sizeof(long long int)); for(i = 0; i < w; i++) { for(j = 0; j < h; j++) { QS[i * h + j] = img.get_pixel(i,j); PWQS[i * h + j] = (long long int)img.get_pixel(i,j); PWQS[i * h + j] *= PWQS[i * h + j]; } } for(i = 0; i < w; i++) { for(j = 1; j < h; j++) { QS[i * h + j] += QS[i * h + j - 1]; PWQS[i * h + j] += PWQS[i * h + j - 1]; } } for(i = 1; i < w; i++) { for(j = 0; j < h; j++) { QS[i * h + j] += QS[(i - 1) * h + j]; PWQS[i * h + j] += PWQS[(i - 1) * h + j]; } } for(i = 0; i < w; i++) { for(j = 0; j < h; j++) { x1 = i - window_size; x2 = i + window_size; y1 = j - window_size; y2 = j + window_size; if(x1 < 0) { x1 = -1; } if(y1 < 0) { y1 = -1; } if(x2 >= w) { x2 = w - 1; } if(y2 >= h) { y2 = h - 1; } pix = (x2 - x1) * (y2 - y1); sum = QS[x2 * h + y2]; pwsum = PWQS[x2 * h + y2]; if(x1 >= 0) { sum -= QS[x1 * h + y2]; pwsum -= PWQS[x1 * h + y2]; } if(y1 >= 0) { sum -= QS[x2 * h + y1]; pwsum -= PWQS[x2 * h + y1]; } if(x1 >= 0 && y1 >= 0) { sum += QS[x1 * h + y1]; pwsum += PWQS[x1 * h + y1]; } sd = (int)sqrt(((long long int)pwsum - (2 * (long long int)sum * (long long int)sum / (long long int)pix)) / (long long int)pix + ((long long int)sum * (long long int)sum / (long long int)pix / (long long int)pix)); (img.get_pixel(i,j) < sum / pix - (int)((double)1.3 * (double)sd))? img.set_pixel(i,j,0): img.set_pixel(i,j,255); } } }
//run median filter of img with window size window_size void preprocessing_median_filter(gy_image_object &img, int window_size) { int w = img.width(),h = img.height(),i,j,*QS,sum; QS = (int*) malloc (w * h * sizeof(int)); for(i = 0; i < w; i++) { for(j = 0; j < h; j++) { QS[i * h + j] = (img.get_pixel(i,j) == 0)? 1: 0; } } for(i = 0; i < w; i++) { for(j = 1; j < h; j++) { QS[i * h + j] += QS[i * h + j - 1]; } } for(i = 1; i < w; i++) { for(j = 0; j < h; j++) { QS[i * h + j] += QS[(i - 1) * h + j]; } } window_size--; img.resize(w - window_size,h - window_size); for(i = 0; i < w - window_size; i++) { for(j = 0; j < h - window_size; j++) { sum = QS[(i + window_size) * h + j + window_size]; //printf("sum = QS[%d][%d] = %d\n",i + window_size,j + window_size,QS[(i + window_size) * h + j + window_size]); if(j > 0) { sum -= QS[(i + window_size) * h + j - 1]; //printf("sum -= QS[%d][%d]\n",i + window_size,j - 1); } if(i > 0) { sum -= QS[(i - 1) * h + j + window_size]; //printf("sum -= QS[%d][%d]\n",i - 1,j + window_size); } if(i > 0 && j > 0) { sum += QS[(i - 1) * h + j - 1]; //printf("sum += QS[%d][%d]\n",i - 1,j - 1); } //printf("<%d,%d> = %d\n",i,j,sum); img.set_pixel(i,j,(sum < (window_size + 1) * (window_size + 1) >> 1)? 255: 0); } } free(QS); }