# Block Overlapped Histogram Equalization

I want to do a block overlapped histogram equalization with a 3x3 kernel with fast algorithm. For example, given a 9x9 image (with rows from 0 to 8 ; and columns from 0 to 8). The location for each pixel will be given as: (rows, columns) i.e (height, width). First the pixel at location (1,1) will be computed using a 3x3 kernel to obtain the first kernel histogram. Then move to the second pixel at location (1,2), here I would like to delete pixel at location (0,0), (1,0), (2,0) and add pixel at location (0,3), (1,3), (2,3) to the previous kernel histogram while maintaining pixels at locations (0,1), (1,1) (2,1) and (0,2) (1,2) (2,2). The window will slides from left to right until the second last column at (1,7). In the OutputImage, I can only compute the kernel histogram with column=1 (i.e x==cr_w/2) while other kernel histograms cannot be computed. Is there something wrong at “histogram[image.at<uchar>(y+k,x-r-1)]--;” and “histogram[image.at<uchar>(y+k,x+r)]++;” in the code I wrote below? Please advise.

After finish sliding the first row at (1,7), I wand to continue with the second row at location (2,7) by deleting pixels at first row and adding pixels at fourth row. Then I would like to slide from right to left. How do I need to modify my code so that it can run till the last pixel at (7,7)?

Code:

```
int nl = image.rows; // image height
int nc = image.cols; // image width
unsigned int TransferFunction[256];
cv::Mat OutputImage(nc, nl, CV_8U,cv::Scalar(255));
int cr_w;
int cr_h;
cout<<"\nPlease enter contextual region width = ";
cin>> cr_w;
cout<<"\nPlease enter contextual region height = ";
cin>> cr_h;
for (int y=cr_h/2; y<nl-cr_h/2; y++)
{
for (int x=cr_w/2; x<nc-cr_w/2; x++)
{
// update histogram;
for(int i=0; i<256; i++)
histogram[i] = 0;
int top = y-cr_h/2;
int bottom = y+cr_h/2;
int left = x-cr_w/2;
int right = x+cr_w/2;
int r = cr_h/2; //window radius
if (x == cr_w/2)
{
for (int j=top; j<=bottom; j++)
for (int i=left; i<=right; i++)
{
histogram[image.at<uchar>(j,i)]++;
}
}
else
{
for(int k=-cr_h/2; k<=ch_w/2; k++)
{
histogram[image.at<uchar>(y+k,x-r-1)]--;
histogram[image.at<uchar>(y+k,x+r)]++;
}
}
HE(histogram, 0, 255, TransferFunction); //histogram equalization function
OutputImage.at<uchar>(y,x) = TransferFunction[int(image.at<uchar>(y,x))];
}
}
```