OpenCV Having issues with cv::FAST

12,503

Solution 1

Whenever you have a problem using the OpenCV API go check the tests/examples available in the source code: fast.cpp

This practice is extremely useful and educational. Now, if you take a look at that code you will notice that the image gets converted to grayscale before calling cv::FAST() on it:

Mat mat(tempImg);
Mat gray;
cvtColor(mat, gray, CV_BGR2GRAY);
FAST(gray,keypoints,threshold,true);

Seems pretty straight forward, indeed.

Solution 2

You need change this

cvCvtColor(img,tempImg,CV_8U);

To

cvCvtColor(img,tempImg,CV_BGR2GRAY);

You can read this

Good Luck

Solution 3

Take a look at this sample code. The code you are using looks quite outdated opencv, in this sample you will find how feature detectors should be used now. The sample is generic for several feature detectors (including FAST) so that is like it looks a bit more complicated.

http://code.opencv.org/projects/opencv/repository/entry/branches/2.4/opencv/samples/cpp/matching_to_many_images.cpp

You will also find more samples in the parent directory.

Solution 4

I started getting the same message with code that had worked previously, and i was certain my Mat was U8 grayscale. It turned out that one of the images i was trying to process was no longer there. So in my case it was a misleading error message.

Share:
12,503
dipsmac
Author by

dipsmac

Updated on June 15, 2022

Comments

  • dipsmac
    dipsmac almost 2 years

    I'm trying to use the open CV FAST algorithim in order to detect corners from a video feed. The method call and set-up seems pretty straight forward yet I'm running into a few problems. When I try and use this code

      while(run)
    {
        clock_t begin,end;
        img = cvQueryFrame(capture);
    
        key = cvWaitKey(10);
    
    
        cvShowImage("stream",img);
        //Cv::FAST variables
        int threshold=9;
        vector<KeyPoint> keypoints;
    
        if(key=='a'){
            //begin = clock();
    
    
            Mat mat(tempImg);
    
            FAST(mat,keypoints,threshold,true);
            //end = clock();
            //cout << "\n TIME FOR CALCULATION: " << double(diffClock(begin,end)) << "\n" ;
    
        }
    

    I get this error:

    OpenCV Error: Assertion failed (image.data && image.type() == CV_8U) in unknown function, file ........\ocv\opencv\src\cvaux\cvfast.cpp, line 6039

    So I figured its a problem with the depth of the image so I when I add this:

        IplImage* tempImg = cvCreateImage(Size(img->width,img->height),8,1);
            cvCvtColor(img,tempImg,CV_8U);
    

    I get:

    OpenCV Error: Bad number of channels (Incorrect number of channels for this conv ersion code) in unknown function, file ........\ocv\opencv\src\cv\cvcolor.cpp , line 2238

    I've tried using a Mat instead of a IplImage to capture but I keep getting the same kind of errors.

    Any suggestions or help? Thanks in advance.

    The entire file just to make it easier for anyone:

        #include "cv.h"
        #include "cvaux.hpp"
        #include "highgui.h"
    
        #include <time.h>
        #include <iostream>
    
    
    
        double diffClock(clock_t begin, clock_t end);
    
        using namespace std;
        using namespace cv;
    
        int main(int argc, char** argv)
        {
    //Create Mat img for camera capture 
    IplImage* img;
    bool run = true;
    
    CvCapture* capture= 0;
    capture = cvCaptureFromCAM(-1);
    int key =0;
    cvNamedWindow("stream", 1);
    
    while(run)
    {
        clock_t begin,end;
        img = cvQueryFrame(capture);
    
        key = cvWaitKey(10);
    
    
        cvShowImage("stream",img);
        //Cv::FAST variables
        int threshold=9;
        vector<KeyPoint> keypoints;
    
        if(key=='a'){
            //begin = clock();
            IplImage* tempImg = cvCreateImage(Size(img->width,img->height),8,1);
            cvCvtColor(img,tempImg,CV_8U);
    
            Mat mat(img);
    
            FAST(mat,keypoints,threshold,true);
            //end = clock();
            //cout << "\n TIME FOR CALCULATION: " << double(diffClock(begin,end)) << "\n" ;
    
        }
        else if(key=='x'){
            run= false;
        }
    }
    cvDestroyWindow( "stream" );
    return 0;
    

    }

  • dipsmac
    dipsmac about 12 years
    I had looked through the API and spent quite a bit of time looking around online. Guess I just wasn't looking in the right place. Thanks for the pointer.
  • Rui Marques
    Rui Marques almost 12 years
    That sample fast code is very outdated. Also, although it didn't fix your error, you should use Mat type, IplImage is deprecated.