填充后存储图像的问题
Posted
技术标签:
【中文标题】填充后存储图像的问题【英文标题】:problem with storing an image after padding 【发布时间】:2020-08-12 22:00:41 【问题描述】:我正在使用 5x5 窗口执行简单的屏蔽操作。按照这篇文章Problem with Operation on Border pixels of an image(第二个解决方案)中接受的解决方案,我创建了一个图像img_clamp
,它可以用作具有4个额外行和4个额外列的缓冲区。请在下面找到示例代码。
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-2,j-2)
出错了,但我无法找到解决方法。
任何关于如何解决它的帮助/建议都将受到高度赞赏。
提前致谢
【问题讨论】:
您添加了 4 行/列,但在创建img_clamp
时只添加了 2。
@1201ProgramAlarm,上两下两下共4行
但是在img_clamp
的构造中,你只是将2
添加到输入Mat的行数和列数中。
@1201ProgramAlarm,感谢您指出这一点,我从 3x3 掩码过滤器中提取代码并对其进行了编辑,所以我错过了那部分。我已经更新了
@CrisLuengo 我已经更新了信息,当我试图详细解释时,我发现哪里出了问题。这是 for 循环中的一个小错误,我使用“input.rows”而不是“img_clamp.rows”导致最后 4 行和列的问题,现在已解决。感谢您的建议:-)
【参考方案1】:
问题在于for loop
的边界
而不是这个
for(int i=2;i<input.rows-2;i++)//size =256
//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++)//size =512
应该是的
for(int i=2;i<img_clamp.rows-2;i++)//size = 260
//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<img_clamp.cols-2;i++)//size = 516
【讨论】:
以上是关于填充后存储图像的问题的主要内容,如果未能解决你的问题,请参考以下文章
为 iOS 应用程序存储预填充数据(图像 + 文本)的建议 [重复]