Opencv断言错误

时间:2018-05-29 14:22:24

标签: c++ opencv

我使用opencv 3.2ver,在我的代码中,它有一些错误 这是我的错误代码:

  

“CV_DbgAssert((unsigned)(i1 * DataType< _Tp> :: channels)<   (unsigned)(size.p [1] * channels()));“

binary_Mat[0] = Mat_Sum(image, i, j - 3);会导致错误。

请让我知道问题是什么

int Mat_Sum(Mat& image, int i, int j)

{
int sum = image.at<uchar>(i-1, j-1) + image.at<uchar>(i-1, j) + image.at<uchar>(i-1, j+1) +
    image.at<uchar>(i, j-1) + image.at<uchar>(i, j) + image.at<uchar>(i, j+1) +
    image.at<uchar>(i+1, j-1) + image.at<uchar>(i+1, j) + image.at<uchar>(i+1, j+1);
return sum / 9;
}

int main()

{
    Mat image0 = imread("rena.jpg");
    Mat image;
    cvtColor(image0, image, CV_BGR2GRAY);
    const int width = image.rows;
    const int height = image.cols;

    Mat result_img = image.clone();

    int binary_Mat[8];
    for (int i = 3; i < height - 3; i++)
    {
        for (int j = 3; j < width - 3; j++)
        {
            result_img.at<uchar>(i, j) = 0;
            binary_Mat[0] = Mat_Sum(image, i, j - 3);
            binary_Mat[1] = Mat_Sum(image, i + 3, j - 3);
            binary_Mat[2] = Mat_Sum(image, i + 3, j);
            binary_Mat[3] = Mat_Sum(image, i + 3, j + 3);
            binary_Mat[4] = Mat_Sum(image, i, j + 3);
            binary_Mat[5] = Mat_Sum(image, i - 3, j + 3);
            binary_Mat[6] = Mat_Sum(image, i - 3, j);
            binary_Mat[7] = Mat_Sum(image, i - 3, j - 3);

            int criterion = Mat_Sum(image, i, j);
            for (int k = 0; k < 8; k++) {
                if (binary_Mat[k] > criterion)
                    binary_Mat[k] = 1;
                else
                    binary_Mat[k] = 0;
            }

            int decimal = 0;
            for (int k = 0; k < 8; k++)
                decimal += binary_Mat[k] * pow(2, 8 - k);

            result_img.at<uchar>(i, j) = decimal;
        }
    }
    return 0;
}

2 个答案:

答案 0 :(得分:2)

你有高度和宽度的混淆 - 改变:

const int width = image.rows;
const int height = image.cols;

为:

const int height = image.rows;
const int width = image.cols;

<小时/> 您似乎还试图访问image越界,例如

        binary_Mat[0] = Mat_Sum(image, i, j - 3);

将尝试访问Mat_Sum内的负索引列索引。一个可能的解决办法是改变:

for (int i = 3; i < height - 3; i++)
{
    for (int j = 3; j < width - 3; j++)
    {

为:

for (int i = 4; i < height - 4; i++)
{
    for (int j = 4; j < width - 4; j++)
    {

答案 1 :(得分:0)

看起来你已经互换了行和列: 宽度= image.rows 高度= image.cols

因此,当你打电话给你的功能时,你会通过&#34; i&#34;和&#34; j&#34;这实际上是互换的 因此你的natrix值超出了界限

尝试更改为: 宽度= image.cols 身高= image.rows 这应该工作