【问题标题】:sobel edge detection for error(opencv error: assertion failed (dims<=2&&data&&(unsigned)i0)错误的 sobel 边缘检测(opencv 错误:断言失败(dims<=2&&data&&(unsigned)i0)
【发布时间】:2016-01-27 14:22:01
【问题描述】:
#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;

}

现在我正在尝试编写用于 sobel 边缘检测的代码。但是错误块打开图像和sobel边缘图像。当我调试代码时,窗口显示如下:

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

所以我无法进行下一步。

【问题讨论】:

  • 您可能会发现this 很有帮助

标签: c++ opencv sobel


【解决方案1】:

您的示例中有一些编码错误。 一个是您的声明超出了for loop 中的 Mat 数据范围 sj + j - verticleimagebound / 2, si + i - horizantalimagebound/2。这应该是sj + j - verticleimagebound, si + i - horizantalimagebound

其他是您正在访问来自grayimage 的像素,其中没有数据存在。您应该将image 的输入数据克隆到grayimage

【讨论】:

    猜你喜欢
    • 2019-05-04
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    相关资源
    最近更新 更多