c++ - 填充后存储图像的问题
问题描述
我正在使用 5x5 窗口执行简单的屏蔽操作。按照这篇文章对图像边界像素的操作问题(第二个解决方案)中接受的解决方案,我创建了一个图像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)
存储时出错了,但我无法找到解决方法。
任何有关如何解决它的帮助/建议将不胜感激。
提前致谢
解决方案
问题在于边界for loop
而不是 this
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
推荐阅读
- autodesk-forge - 停止自定义图像旋转
- python - 如何从数组中返回具有最大特定值的元素?
- html - Angular Material mat-chips 未设置为可移除
- css - 如何在 React 中自定义简单的 HTML 范围滑块
- git-commit - 如何使用 git 加载项目的倒数第二个提交版本?
- parameters - 替换修饰符中的参数扩展
- php - 从两个 mysql 表中获取数据并用 php 显示显示顺序不正确
- elasticsearch - 从 Xpath 创建嵌套字段并检查现有文档
- django - ORM Django 和 DB postgreSQL 用于 B-tree 索引的方式是什么?
- javascript - React.js TypeError:解构不可迭代实例的无效尝试