Search code examples
c++opencvsobel

sobel edge detection for error(opencv error: assertion failed (dims<=2&&data&&(unsigned)i0)


#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include <cmath>
using namespace cv;
using namespace std;

int main()
{
    Mat image = imread("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);

    if (!image.data)
    {
        cout << "we can not open an image!!" << endl;
        return -1;
    }

    const int sobel_width = 3;
    const int sobel_height = 3;

    int sobel_y[sobel_width][sobel_height] = {
        {-1,-2,-1},
        {0, 0, 0},
        {1, 2, 1}
    };

    int sobel_x[sobel_width][sobel_height] = {
        { -1, 0, 1 },
        { -2, 0, 2 },
        { -1, 0, 1 }
    };
    Mat grayimage(image.size(), CV_8UC1);

    Mat final(image.size(), CV_8UC1);

    int SUM;
    int verticleimagebound = (sobel_height - 1) / 2;
    int horizantalimagebound = (sobel_width - 1) / 2;

    for (int j = 0+verticleimagebound; j < image.rows - verticleimagebound; j++)
    {
        for (int i = 0+horizantalimagebound; i < image.cols - horizantalimagebound; i++)
        {
            int sum_x=0, sum_y = 0;
            for (int sj = 0; sj < 3; sj++)
            {
                for (int si = 0; si < 3; si++)
                {
                    int pixel1 = grayimage.at<uchar>(sj + j - verticleimagebound / 2, si + i - horizantalimagebound/2)*sobel_x[sj][si];
                    sum_x += pixel1;
                    int pixel2 = grayimage.at<uchar>(sj + j - verticleimagebound / 2, si + i - horizantalimagebound/2)*sobel_y[sj][si];
                    sum_y += pixel2;
                }
            }
            SUM = abs((int)sum_x) + abs((int)sum_y);
            if (SUM > 255)
            {
                SUM = 255;
            }
            else if (SUM < 0)
            {
                SUM = 0;
            }
            final.at<uchar>(j, i) = 255 - (uchar)(SUM);
        }
    }

    namedWindow("orginal", 1);
    imshow("orginal", image);

    namedWindow("sobel", 1);
    imshow("sobel", final);

    waitKey(0);
    return 0;

}

Now I am trying to make a code for sobel edge detection. But the error block to open an image and sobel edge image. When I debug my code, the window says like this:

OpenCv Error: Assertion failed (
    dims <= 2 
    && data 
    && (unsigned)i0 < (unsigned)size.p[0] 
    && (unsigned)(i1*DataType<_Tp>::channedls) < (unsigned)(size.p[1]*channels()) 
    && ((((sizeof(size_t)<<28)|0x8442211)>>((DataType<_Tp>::depth) & ((1 << 3)-1))*4) & 15) == elemmSize1()) 
  in cv:: Mat::at, file c:\opencv\build\include\opencv2\core\mat.hpp, line 538

So I cannot move to next step.


Solution

  • There are a few coding errors in your sample. one is your are getting out of bounds of Mat data in for loop with the statement sj + j - verticleimagebound / 2, si + i - horizantalimagebound/2.This should be sj + j - verticleimagebound, si + i - horizantalimagebound.

    other is you are accessing pixels from grayimage in which no data present.you should clone the input data of image into grayimage.