Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

c++ - problem with storing an image after padding

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 console output

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...