0

I am using ORB detector for detecting the keypoints of frame of a video but it gives me the following error:

OpenCV Error: Assertion failed (img.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3))) in detectAsync

The CPU functions for ORB detector works fine. But somehow for GPU it is not able to detect the keypoints of the image. I have also tried using FastFeatureDetector from CUDA but it also fails.

I am attaching my code below:

int main()
{
   cv::VideoCapture input("/home/admin/Pictures/cars.mp4");

   // Create image matrix
   cv::Mat img,desc;
   cv::cuda::GpuMat obj1;

   // create vector keypoints
   std::vector<cv::KeyPoint>keypoints;

   // create keypoint detector
   cv::Ptr<cv::cuda::ORB> detector = cv::cuda::ORB::create();

   for(;;)
   {
       if(!input.read(img))
           break;
       obj1.upload(img);
       detector->detect(obj1,keypoints, cv::cuda::GpuMat());
       obj1.download(desc);
       //Create cricle at keypoints
       for(size_t i=0; i<keypoints.size(); i++)
          cv::circle(desc, keypoints[i].pt, 2,cv::Scalar(0,0,255),1);

       // Display Image
       cv::imshow("img", desc);
       char c = cv::waitKey();

       // NOTE: Press any key to run the next frame
       // Wait for a key to press
       if(c == 27) // 27 is ESC key code
           break;
    }
}

The main problem is that the detector takes CV_8UC1 as the input format for Mat. But the format of my image is CV_8UC3. I have tried converting that image using img.convertTo(img1, CV_8UC1)but still it is unable to process and throws me the error OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat.

talonmies
  • 70,661
  • 34
  • 192
  • 269
Nukul Khadse
  • 108
  • 7

1 Answers1

1

You have to convert your image from CV_8UC3 - 3 channel image to CV_8UC1 - single channel (grayscale) image. To do so simply call

cv::cvtColor(img, img, cv::BGR2GRAY);

prior to uploading data to the GPU.

Piotr Siekański
  • 1,665
  • 8
  • 14
  • Thanks...Any idea why it is not taking the output obtained by convertTo() function? – Nukul Khadse Jan 17 '19 at 17:11
  • 1
    Look at this thread: https://stackoverflow.com/questions/22174002/why-does-opencvs-convertto-function-not-work. To make long story short - convertTo is used to change the data type not the number of channels – Piotr Siekański Jan 18 '19 at 09:51