I am performing a simple masking operation with a 5x5 window. Following the accepted solution from this post Problem with Operation on Border pixels of an image (second solution), I have created a image img_clamp
which can act as a buffer with 4 extra rows and 4 extra columns. Please find the sample code below.
int main(int argc, char** argv)
{
Mat input = imread("C:/Users/20181217/Desktop/images/imgs/den_check.png");
//input.rows = 256, input.cols =512
Mat output = (input.rows,input.cols,input.type()); //row and col = 256 and 512
//number of additional rows and columns you woluld ike on each side
int top, left, right, bottom;
top = 2;
left = 2;
right = 2;
bottom = 2;
//define new image with additional borders
Mat img_clamp(input.rows + 4, input.cols + 4, CV_8UC3);
//if you want to replicate the border of the image
copyMakeBorder(input, img_clamp, top, left, right, bottom, BORDER_REPLICATE);
//img_clamp row and col size : 260 and 516
//Now you can access the image without having to worry about the borders as shown below
//start iterationg from the 2nd row till 258th row (this leaves 1,2,259 and 260th rows for the out of bounds access by the 5x5 window)
for(int i=2;i<input.rows-2;i++)
{
//start iteration from the 2nd col till 514th col (this leaves 1,2,515 and 516th cols for the out of bounds access by the 5x5 window)
for(int j=2;i<input.cols-2;i++)
{
temp_red = img_clamp.at<Vec3b>[0](i-2,j-2) + img_clamp.at<Vec3b>[0](i-2,j+2) + img_clamp.at<Vec3b>[0](i+2,j-2) + img_clamp.at<Vec3b>[0](i+2,j+2);
temp_green = img_clamp.at<Vec3b>[1](i-2,j-2) + img_clamp.at<Vec3b>[1](i-2,j+2) + img_clamp.at<Vec3b>[1](i+2,j-2) + img_clamp.at<Vec3b>[1](i+2,j+2);
temp_blue = img_clamp.at<Vec3b>[2](i-2,j-2) + img_clamp.at<Vec3b>[2](i-2,j+2) + img_clamp.at<Vec3b>[2](i+2,j-2) + img_clamp.at<Vec3b>[2](i+2,j+2);
...
//store values in the output image which has the same the size as input image (i.e 256 and 512 (rows and cols))
output.at<Vec3b>[0](i-2,j-2) =temp_red
output.at<Vec3b>[1](i-2,j-2) =temp_green
output.at<Vec3b>[2](i-2,j-2) =temp_blue
}
}
//Code for checking the output is matching with the golden data(ideal image)<---- the ideal image is from a different language halide, My goal is to replicate the same logic in c++
Mat diff = abs(ideal-output);
Mat diff = abs(small_img - ideal);
//cout << diff;
int r, g, b, t, n,r_b,g_b,b_b;
r = 0; //faulty red
g = 0;//faulty green
b = 0;//faulty blue
n = 393216; //total number of pixels
//_b indicates the border pixels
r_b = 0;
b_b = 0;
g_b = 0;
t = 1;//threshold for the difference
// printing out the difference between final image and ideal image (if any)
for (int i = 0; i < input.cols; i++)//512 col size x
{
for (int j = 0; j < input.rows; j++)//256 row size y
{
if ((int)diff.at<Vec3b>(j, i)[0] > t)
{
if (i == 508 || i == 509 || i == 510 || i == 511 || j == 252 || j == 253 || j == 254 || j == 255) //border pixels(right most and bottom most of the image)
r_b++;//increment if its one of the border pixels
//printing the pixel position which has the wrong value
cout << "problem at (" << j << "," << i << ") of red :" << (int)output.at<Vec3b>(j, i)[0] << ", expected value:" << (int)ideal.at<Vec3b>(j, i)[0] << endl;
r++;//increment if the red pixel is faulty and not matching with the ideal image
n--;
}
if ((int)diff.at<Vec3b>(j, i)[1] > t)
{
if (i == 508 || i == 509 || i == 510 || i == 511 || j == 252 || j == 253 || j == 254 || j == 255)
g_b++;
g++;//increment if the green pixel is faulty and not matching with the ideal image
n--;
}
if ((int)diff.at<Vec3b>(j, i)[2] > t)
{
if (i == 508 || i == 509 || i == 510 || i == 511 || j == 252 || j == 253 || j == 254 || j == 255)
b_b++;
b++;//increment if the blue pixel is faulty and not matching with the ideal image
n--;
}
}
}
cout << endl << endl << "for a threshold of difference greater than " << t << endl << "total faulty pixels are(R,G,B) :(" << r << " " << g << " " << b << ")"<<endl;
cout << " The border pixels present in the faulty pixels are :(" << r_b << " " << g_b << " " << b_b << endl << endl;
cout<<" correct pixels that match with the ideal image = " << n;
return 0;
}
I would like to store the output as the regular image**(not padded)**.
Everything is going according to the logic except for the last four rows and four columns. The output from the console is
I know that its going wrong at (i-2,j-2)
while storing but I am not able to find a way around it.
Any help/suggestions on how to work around it will be highly appreciated.
Thanks in advance
See Question&Answers more detail:
os