实施SIFT算法时出现断言错误:opencv [英] Assertion Error while implementing SIFT algorithm :opencv

查看:103
本文介绍了实施SIFT算法时出现断言错误:opencv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用opencv 2.4.8和studio 2013,n我遇到了运行时错误.我的主要代码是..

#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include"SIFT.h"
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace cv;
using namespace std;
int main()
{
    cout << "hello";
    Mat image = imread("abc.jpg",0);
    cout << image.channels() << endl;
    SIFT controller(image);
    controller.DoSIFT();    
    waitKey(100000);

}

我的头文件代码如下

#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>

#include<iostream>

using namespace cv;
using namespace std;

class SIFT{

private:
    Mat image_orig;
    Mat Scale_spaces[4][6];
    Mat LOG_img[4][4];
    Mat Extrema[4][2];
    Mat image_temp;
    Mat middle, up, down;


    void BuildScaleSpace()
    {

        cout << "in build space" <<endl;
        int i, j;
            //image should be in grey scale
        cout << endl<<image_temp.rows << image_temp.cols << endl;
            GaussianBlur(image_temp, image_temp, cv::Size(5, 5), 0.5);
            resize(image_temp, image_temp, Size(image_temp.cols * 2, image_temp.rows * 2), 0, 0, 1);
            cout << image_temp.rows << image_temp.cols << endl;
            Scale_spaces[0][0] =image_temp;
            cout << endl << image_temp.rows << image_temp.cols << endl;
            GaussianBlur(Scale_spaces[0][0], Scale_spaces[0][0], cv::Size(5, 5), 1);
            double int_sigma = sqrt(2) / 2;
            double sigma = int_sigma;;
            double mid;
            double power = 1.0;
            for (i = 0; i < 4; i++)
            {

                if (i>0)
                {
                    sigma = mid;
                    Scale_spaces[i][0] = image_temp;
                    resize(image_temp, Scale_spaces[i][0], Size(image_temp.cols / power, image_temp.rows / power), 0, 0, 1);
                    cout << endl << "after resizing" << Scale_spaces[i][0].rows << Scale_spaces[i][0].cols << endl;
                }

                for (j = 1; j <= 5; j++)
                {
                    if (j == 2)
                        mid = sigma;
            //      printf(" %f  ", sigma);
                            GaussianBlur(Scale_spaces[i][j-1], Scale_spaces[i][j], cv::Size(5, 5), sigma);
                    sigma = sigma*sqrt(2);

                }
        //      printf("\n");
                power = power * 2;
            }




    }

    void ShowScaleSpace()
    {
        cout << "\n show scale\n\n";
        namedWindow("image", CV_WINDOW_AUTOSIZE);
        int i, j;
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j <= 5; j++)
            {
                cout << i<<"   "<<j<<endl;
                cout << Scale_spaces[i][j].rows<<"  " << Scale_spaces[i][j].cols << endl;
                imshow("image", Scale_spaces[i][j]);
                cvWaitKey(700);
            }
        }

    }

    void FindLOG()
    {
        cout << endl << "IN LOG  " << endl;
        int i;
        int j;
        for (i = 0; i < 4; i++)
        {
            for (j = 1; j < 5; j++)
            {
                addWeighted(Scale_spaces[i][j], 1, Scale_spaces[i][j + 1], -1,0, LOG_img[i][j - 1]);
            }
        }

    }

    void FindMaxMin()
    {

        cout << endl << "IN findmaxmin  " << endl;
        int i;
        int j;
        int xiter, yiter;
        for (i = 0; i < 4; i++)
        {
            for (j = 1; j < 3; j++)
            {

                std::cout << LOG_img[i][j].channels();
                middle = LOG_img[i][j].clone();
                up = LOG_img[i][j - 1].clone();
                down = LOG_img[i][j + 1].clone();

                for (yiter = 1; yiter < middle.rows-1; yiter++)
                {
                    for (xiter = 1; xiter < middle.cols-1; xiter++)
                    {
                        cout << i << "  " << j << "  " << yiter << "  " << xiter<< "  "<<endl;
                        double currentPixel = middle.at<uchar>(yiter, xiter);
                        cout << "1 ";
                        // Check for a maximum
                        if (cout << "helllo 2hhukjhgkj"&&currentPixel> middle.at<uchar>(yiter, xiter + 1) &&
                            currentPixel> middle.at<uchar>(yiter, xiter - 1) &&
                            currentPixel > middle.at<uchar>(yiter + 1, xiter) &&
                            currentPixel > middle.at<uchar>(yiter + 1, xiter - 1) &&
                            currentPixel > middle.at<uchar>(yiter, xiter + 1) &&
                            currentPixel > middle.at<uchar>(yiter - 1, xiter) &&
                            currentPixel > middle.at<uchar>(yiter - 1, xiter + 1) &&
                            currentPixel > middle.at<uchar>(yiter - 1, xiter - 1) &&

                            currentPixel > up.at<uchar>(yiter, xiter) &&
                            currentPixel > up.at<uchar>(yiter, xiter + 1) &&
                            currentPixel > up.at<uchar>(yiter, xiter - 1) &&
                            currentPixel > up.at<uchar>(yiter + 1, xiter) &&
                            currentPixel > up.at<uchar>(yiter + 1, xiter - 1) &&
                            currentPixel > up.at<uchar>(yiter, xiter + 1) &&
                            currentPixel > up.at<uchar>(yiter - 1, xiter) &&
                            currentPixel > up.at<uchar>(yiter - 1, xiter + 1) &&
                            currentPixel > up.at<uchar>(yiter - 1, xiter - 1) &&

                            currentPixel > down.at<uchar>(yiter, xiter) &&
                            currentPixel > down.at<uchar>(yiter, xiter + 1) &&
                            currentPixel > down.at<uchar>(yiter, xiter - 1) &&
                            currentPixel > down.at<uchar>(yiter + 1, xiter) &&
                            currentPixel > down.at<uchar>(yiter + 1, xiter - 1) &&
                            currentPixel > down.at<uchar>(yiter, xiter + 1) &&
                            currentPixel > down.at<uchar>(yiter - 1, xiter) &&
                            currentPixel > down.at<uchar>(yiter - 1, xiter + 1) &&
                            currentPixel > down.at<uchar>(yiter - 1, xiter - 1)
                            )
                        {
                            cout << "inside, updating extrema  ";
                            Extrema[i][j - 1].at<uchar>(xiter, yiter) = 255;
                        }

                        else if (currentPixel< middle.at<uchar>(yiter, xiter + 1) &&
                            currentPixel< middle.at<uchar>(yiter, xiter - 1) &&
                            currentPixel < middle.at<uchar>(yiter + 1, xiter) &&
                            currentPixel < middle.at<uchar>(yiter + 1, xiter - 1) &&
                            currentPixel < middle.at<uchar>(yiter, xiter + 1) &&
                            currentPixel < middle.at<uchar>(yiter - 1, xiter) &&
                            currentPixel < middle.at<uchar>(yiter - 1, xiter + 1) &&
                            currentPixel < middle.at<uchar>(yiter - 1, xiter - 1) &&

                            currentPixel < up.at<uchar>(yiter, xiter) &&
                            currentPixel < up.at<uchar>(yiter, xiter + 1) &&
                            currentPixel < up.at<uchar>(yiter, xiter - 1) &&
                            currentPixel < up.at<uchar>(yiter + 1, xiter) &&
                            currentPixel < up.at<uchar>(yiter + 1, xiter - 1) &&
                            currentPixel < up.at<uchar>(yiter, xiter + 1) &&
                            currentPixel < up.at<uchar>(yiter - 1, xiter) &&
                            currentPixel < up.at<uchar>(yiter - 1, xiter + 1) &&
                            currentPixel < up.at<uchar>(yiter - 1, xiter - 1) &&

                            currentPixel < down.at<uchar>(yiter, xiter) &&
                            currentPixel < down.at<uchar>(yiter, xiter + 1) &&
                            currentPixel < down.at<uchar>(yiter, xiter - 1) &&
                            currentPixel < down.at<uchar>(yiter + 1, xiter) &&
                            currentPixel < down.at<uchar>(yiter + 1, xiter - 1) &&
                            currentPixel < down.at<uchar>(yiter, xiter + 1) &&
                            currentPixel < down.at<uchar>(yiter - 1, xiter) &&
                            currentPixel < down.at<uchar>(yiter - 1, xiter + 1) &&
                            currentPixel < down.at<uchar>(yiter - 1, xiter - 1)
                            )
                        {
                            Extrema[i][j - 1].at<uchar>(xiter, yiter) = 255;
                        }

                        else
                            Extrema[i][j - 1].at<uchar>(xiter, yiter) = 0;
                    }




                }
            }
        }
    }



public:
    SIFT(Mat Image)
    {
        image_orig = Image;
        image_temp = Image;

    }

    void DoSIFT()
    {

        BuildScaleSpace();
        //ShowScaleSpace();
        FindLOG();
        FindMaxMin();


    }



};

构建成功,但是在运行时显示以下错误.

该错误可能出在FindMaxMin()函数和此行中

  currentPixel> middle.at<uchar>(yiter, xiter + 1)

但是我无法纠正它.

解决方案

对不起,@ user2396315,我没有经常访问这里.

问题是您没有使用正确的大小和类型的Mat :: create()来初始化"Extrema". 您只声明了"Mat Extrema [4] [2];"

那么,如果还有……

Extrema[i][j - 1].at<uchar>(xiter, yiter) = ??? ;

将访问一个空的Mat"Extrema [i] [j-1]".

您可以放

if( Extrema[i][j - 1].empty() == true )
   cerr << "Fatal error" << endl ;

之前,您会看到.

在使用Mat之前,请始终通过Mat :: empty()检查Mat. 这就是我在此上的检查尺寸"的意思.发布.我不是要检查数组的大小.

请记住,断言失败"通常意味着您已经传递了空的Mat或大小不正确的Mat.

I am using opencv 2.4.8 and studio 2013, n i am getting a run time error.my main code is this..

#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include"SIFT.h"
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace cv;
using namespace std;
int main()
{
    cout << "hello";
    Mat image = imread("abc.jpg",0);
    cout << image.channels() << endl;
    SIFT controller(image);
    controller.DoSIFT();    
    waitKey(100000);

}

and my header file code is as below

#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>

#include<iostream>

using namespace cv;
using namespace std;

class SIFT{

private:
    Mat image_orig;
    Mat Scale_spaces[4][6];
    Mat LOG_img[4][4];
    Mat Extrema[4][2];
    Mat image_temp;
    Mat middle, up, down;


    void BuildScaleSpace()
    {

        cout << "in build space" <<endl;
        int i, j;
            //image should be in grey scale
        cout << endl<<image_temp.rows << image_temp.cols << endl;
            GaussianBlur(image_temp, image_temp, cv::Size(5, 5), 0.5);
            resize(image_temp, image_temp, Size(image_temp.cols * 2, image_temp.rows * 2), 0, 0, 1);
            cout << image_temp.rows << image_temp.cols << endl;
            Scale_spaces[0][0] =image_temp;
            cout << endl << image_temp.rows << image_temp.cols << endl;
            GaussianBlur(Scale_spaces[0][0], Scale_spaces[0][0], cv::Size(5, 5), 1);
            double int_sigma = sqrt(2) / 2;
            double sigma = int_sigma;;
            double mid;
            double power = 1.0;
            for (i = 0; i < 4; i++)
            {

                if (i>0)
                {
                    sigma = mid;
                    Scale_spaces[i][0] = image_temp;
                    resize(image_temp, Scale_spaces[i][0], Size(image_temp.cols / power, image_temp.rows / power), 0, 0, 1);
                    cout << endl << "after resizing" << Scale_spaces[i][0].rows << Scale_spaces[i][0].cols << endl;
                }

                for (j = 1; j <= 5; j++)
                {
                    if (j == 2)
                        mid = sigma;
            //      printf(" %f  ", sigma);
                            GaussianBlur(Scale_spaces[i][j-1], Scale_spaces[i][j], cv::Size(5, 5), sigma);
                    sigma = sigma*sqrt(2);

                }
        //      printf("\n");
                power = power * 2;
            }




    }

    void ShowScaleSpace()
    {
        cout << "\n show scale\n\n";
        namedWindow("image", CV_WINDOW_AUTOSIZE);
        int i, j;
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j <= 5; j++)
            {
                cout << i<<"   "<<j<<endl;
                cout << Scale_spaces[i][j].rows<<"  " << Scale_spaces[i][j].cols << endl;
                imshow("image", Scale_spaces[i][j]);
                cvWaitKey(700);
            }
        }

    }

    void FindLOG()
    {
        cout << endl << "IN LOG  " << endl;
        int i;
        int j;
        for (i = 0; i < 4; i++)
        {
            for (j = 1; j < 5; j++)
            {
                addWeighted(Scale_spaces[i][j], 1, Scale_spaces[i][j + 1], -1,0, LOG_img[i][j - 1]);
            }
        }

    }

    void FindMaxMin()
    {

        cout << endl << "IN findmaxmin  " << endl;
        int i;
        int j;
        int xiter, yiter;
        for (i = 0; i < 4; i++)
        {
            for (j = 1; j < 3; j++)
            {

                std::cout << LOG_img[i][j].channels();
                middle = LOG_img[i][j].clone();
                up = LOG_img[i][j - 1].clone();
                down = LOG_img[i][j + 1].clone();

                for (yiter = 1; yiter < middle.rows-1; yiter++)
                {
                    for (xiter = 1; xiter < middle.cols-1; xiter++)
                    {
                        cout << i << "  " << j << "  " << yiter << "  " << xiter<< "  "<<endl;
                        double currentPixel = middle.at<uchar>(yiter, xiter);
                        cout << "1 ";
                        // Check for a maximum
                        if (cout << "helllo 2hhukjhgkj"&&currentPixel> middle.at<uchar>(yiter, xiter + 1) &&
                            currentPixel> middle.at<uchar>(yiter, xiter - 1) &&
                            currentPixel > middle.at<uchar>(yiter + 1, xiter) &&
                            currentPixel > middle.at<uchar>(yiter + 1, xiter - 1) &&
                            currentPixel > middle.at<uchar>(yiter, xiter + 1) &&
                            currentPixel > middle.at<uchar>(yiter - 1, xiter) &&
                            currentPixel > middle.at<uchar>(yiter - 1, xiter + 1) &&
                            currentPixel > middle.at<uchar>(yiter - 1, xiter - 1) &&

                            currentPixel > up.at<uchar>(yiter, xiter) &&
                            currentPixel > up.at<uchar>(yiter, xiter + 1) &&
                            currentPixel > up.at<uchar>(yiter, xiter - 1) &&
                            currentPixel > up.at<uchar>(yiter + 1, xiter) &&
                            currentPixel > up.at<uchar>(yiter + 1, xiter - 1) &&
                            currentPixel > up.at<uchar>(yiter, xiter + 1) &&
                            currentPixel > up.at<uchar>(yiter - 1, xiter) &&
                            currentPixel > up.at<uchar>(yiter - 1, xiter + 1) &&
                            currentPixel > up.at<uchar>(yiter - 1, xiter - 1) &&

                            currentPixel > down.at<uchar>(yiter, xiter) &&
                            currentPixel > down.at<uchar>(yiter, xiter + 1) &&
                            currentPixel > down.at<uchar>(yiter, xiter - 1) &&
                            currentPixel > down.at<uchar>(yiter + 1, xiter) &&
                            currentPixel > down.at<uchar>(yiter + 1, xiter - 1) &&
                            currentPixel > down.at<uchar>(yiter, xiter + 1) &&
                            currentPixel > down.at<uchar>(yiter - 1, xiter) &&
                            currentPixel > down.at<uchar>(yiter - 1, xiter + 1) &&
                            currentPixel > down.at<uchar>(yiter - 1, xiter - 1)
                            )
                        {
                            cout << "inside, updating extrema  ";
                            Extrema[i][j - 1].at<uchar>(xiter, yiter) = 255;
                        }

                        else if (currentPixel< middle.at<uchar>(yiter, xiter + 1) &&
                            currentPixel< middle.at<uchar>(yiter, xiter - 1) &&
                            currentPixel < middle.at<uchar>(yiter + 1, xiter) &&
                            currentPixel < middle.at<uchar>(yiter + 1, xiter - 1) &&
                            currentPixel < middle.at<uchar>(yiter, xiter + 1) &&
                            currentPixel < middle.at<uchar>(yiter - 1, xiter) &&
                            currentPixel < middle.at<uchar>(yiter - 1, xiter + 1) &&
                            currentPixel < middle.at<uchar>(yiter - 1, xiter - 1) &&

                            currentPixel < up.at<uchar>(yiter, xiter) &&
                            currentPixel < up.at<uchar>(yiter, xiter + 1) &&
                            currentPixel < up.at<uchar>(yiter, xiter - 1) &&
                            currentPixel < up.at<uchar>(yiter + 1, xiter) &&
                            currentPixel < up.at<uchar>(yiter + 1, xiter - 1) &&
                            currentPixel < up.at<uchar>(yiter, xiter + 1) &&
                            currentPixel < up.at<uchar>(yiter - 1, xiter) &&
                            currentPixel < up.at<uchar>(yiter - 1, xiter + 1) &&
                            currentPixel < up.at<uchar>(yiter - 1, xiter - 1) &&

                            currentPixel < down.at<uchar>(yiter, xiter) &&
                            currentPixel < down.at<uchar>(yiter, xiter + 1) &&
                            currentPixel < down.at<uchar>(yiter, xiter - 1) &&
                            currentPixel < down.at<uchar>(yiter + 1, xiter) &&
                            currentPixel < down.at<uchar>(yiter + 1, xiter - 1) &&
                            currentPixel < down.at<uchar>(yiter, xiter + 1) &&
                            currentPixel < down.at<uchar>(yiter - 1, xiter) &&
                            currentPixel < down.at<uchar>(yiter - 1, xiter + 1) &&
                            currentPixel < down.at<uchar>(yiter - 1, xiter - 1)
                            )
                        {
                            Extrema[i][j - 1].at<uchar>(xiter, yiter) = 255;
                        }

                        else
                            Extrema[i][j - 1].at<uchar>(xiter, yiter) = 0;
                    }




                }
            }
        }
    }



public:
    SIFT(Mat Image)
    {
        image_orig = Image;
        image_temp = Image;

    }

    void DoSIFT()
    {

        BuildScaleSpace();
        //ShowScaleSpace();
        FindLOG();
        FindMaxMin();


    }



};

the build is successful but, at the run time it is showing the following error.

the error might be in the FindMaxMin() function and in this line

  currentPixel> middle.at<uchar>(yiter, xiter + 1)

But I am not able to rectify it.

解决方案

Sorry, @user2396315, I didn't visit here constantly.

The problem is You didn't initial "Extrema", with Mat::create() with correct size and type. You only declared it "Mat Extrema[4][2];"

So after the if else...

Extrema[i][j - 1].at<uchar>(xiter, yiter) = ??? ;

will access an empty Mat "Extrema[i][j - 1]".

You may put

if( Extrema[i][j - 1].empty() == true )
   cerr << "Fatal error" << endl ;

before it and you will see.

Always check your Mat by Mat::empty() before you use it. That is what I meant "Check the size" at this post. I didn't mean to check the size of arrays.

Remember, "Assertion failed" usually means you have passed an empty Mat or Mat with incorrect size as expectation.

这篇关于实施SIFT算法时出现断言错误:opencv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆