填充后存储图像的问题 [英] problem with storing an image after padding

查看:67
本文介绍了填充后存储图像的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用5x5窗口执行简单的遮罩操作.遵循本文中公认的解决方案

我知道在存储时(i-2,j-2)出现了问题,但是我找不到解决方法.

任何有关如何解决它的帮助/建议都将受到高度赞赏.

预先感谢

解决方案

问题在于 for循环的边界代替这个

  for(int i = 2; i< input.rows-2; i ++)//size = 256{//从第2列开始直到第514列开始迭代(这留下了1,2,515和516列用于5x5窗口的越界访问)for(int j = 2; i< input.cols-2; i ++)//大小= 512 

应该是

  for(int i = 2; i< img_clamp.rows-2; i ++)//size = 260{//从第2列开始直到第514列开始迭代(这留下了1,2,515和516列用于5x5窗口的越界访问)for(int j = 2; i< img_clamp.cols-2; i ++)//size = 516 

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

解决方案

The problem was with bounds for the for loop instead of 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

It should have been

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

这篇关于填充后存储图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆