dipsmac
dipsmac

Reputation: 41

OpenCV Having issues with cv::FAST

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;

}

Upvotes: 1

Views: 12793

Answers (5)

Tanmoy Mondal
Tanmoy Mondal

Reputation: 449

Please follow the following code to have your desired result. For showing an example, I am considering an image only but you can simply use the same idea for video frames

Mat img = imread("IMG.jpg", IMREAD_UNCHANGED);
if( img.empty())
  {
    cout << "File not available for reading"<<endl;
    return -1;
  }
Mat grayImage;
if(img.channels() >2){
   cvtColor( img, grayImage, CV_BGR2GRAY ); // converting color to gray image
    }
else{
   grayImage =  img;
}
double sigma = 1;
GaussianBlur(grayImage, grayImage, Size(), sigma, sigma); // applying gaussian blur to remove some noise,if present

int thresholdCorner = 40;
vector<KeyPoint> keypointsCorners;
FAST(grayImage,keypointsCorners,thresholdCorner,true); // applying FAST key point detector
if(keypointsCorners.size() > 0){
   cout << keypointsCorners.size() << endl;
}
// Drawing a circle around corners
for( int i = 0; i < keypointsCorners.size(); i++ )
   {
       circle( grayImage, keypointsCorners.at(i).pt, 5,  Scalar(0), 2, 8, 0 );
   }
cv::namedWindow("Display Image");
cv::imshow("Display Image", grayImage);
cvWaitKey(0);
cvDestroyWindow( "Display Image" );

Upvotes: 0

Rui Marques
Rui Marques

Reputation: 8904

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.

Upvotes: 1

david
david

Reputation: 11

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.

Upvotes: 1

shihongzhi
shihongzhi

Reputation: 1931

You need change this

cvCvtColor(img,tempImg,CV_8U);

To

cvCvtColor(img,tempImg,CV_BGR2GRAY);

You can read this

Good Luck

Upvotes: 3

karlphillip
karlphillip

Reputation: 93410

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.

Upvotes: 6

Related Questions