Assertion Failed during debug in the ‘for’ loop, for the if condition statement

I want to equate the 0 pixel value to the pixel location having 0 pixel value in Mask image to the same location in the grayimg12 image, which is a gray image. When I put the for loop in try-catch block it is giving me error and assertion failed, without using try-catch the error is “Unhandled Exception at 0x755b0f22 and cv:: Exception at memory location 0x004af338.. I am using opencv 3.0.0 beta version and Visual Studio 2010.

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d.hpp>

#include <iostream>
#include <sstream>
using namespace cv;
using namespace std;

int main()
{

// Reading Mask and Creating New Image 
    Mat grayimg, grayimg12, input, Mask; int keyboard; 
    input = imread("peter.jpg");
    cvtColor(input, grayimg, COLOR_BGR2GRAY);
    grayimg.copyTo(grayimg12, grayimg);

    namedWindow("Gray Converted Frame");
    imshow("Gray Converted Frame", grayimg);
    int r = input.rows; int c = input.cols; 

    Mask = grayimg > 100;

    namedWindow("Binary Image");
    imshow("Binary Image", Mask);

    try
    {
    for (int i=1;i<=r;i++)
    {
        for (int j=1;j<=c; j++)
        {
            if (Mask.at<uchar>(i,j) == 0)
            {
                grayimg12.at<uchar>(i,j) = 0;

            }
            else
                grayimg12.at<uchar>(i,j) = grayimg.at<uchar>(i,j);

        }
    }
    }
    catch(Exception)
    {
        cout<<"Hi..";
    }

    namedWindow("Gray Output Image");
    imshow("Gray Output Image", grayimg12);

    keyboard = waitKey( 10000 ); 
    return 0;
}


Source: c++

Leave a Reply