Ask Your Question

calcOpticalFlowPyrLK() assertion failure

asked 2013-01-24 12:35:00 -0600

smishra87 gravatar image

updated 2013-01-29 13:07:15 -0600

I have a simple program which detects point in prevImg and finds the matching points in the nextImg using the calcOpticalFlowPyrLK() function. I use all the default params but no matter the setting I always end up with

"OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0)) in create, file /tmp/buildd/ros-fuerte-opencv2-2.4.2-0lucid-20120908-1627/modules/core/src/matrix.cpp, line 1405"

The code is as follows;

#include <opencv2/opencv.hpp>
#include <iostream>

int main(int argc, char* argv[]) {

std::string file1_string("img0.pgm"), file2_string("img1.pgm");
std::string win1_string("showing img 1"),win2_string("showing img 2");
cv::Mat img1_gray_Mat, img2_gray_Mat,img1_bgr_Mat,img2_bgr_Mat;
img1_gray_Mat = cv::imread(file1_string.c_str(),CV_LOAD_IMAGE_GRAYSCALE);
img2_gray_Mat = cv::imread(file2_string.c_str(),CV_LOAD_IMAGE_GRAYSCALE);

cv::namedWindow(win1_string.c_str()), cv::namedWindow(win2_string.c_str());

int nfeatures=10;
float qualityLevel=0.01;
int minDistance=1;
int blockSize=3;
bool useHarrisDetector=false;
float k=0.04;
cv::GoodFeaturesToTrackDetector gfttDetector(nfeatures,qualityLevel,minDistance,blockSize,useHarrisDetector,k);

std::vector<cv::KeyPoint> keyPoints1_vec;
std::vector<char> status;
std::vector<float> err;
cv::Size winSize(15,15);

std::vector<cv::Point2f> points1_vec, points2_vec;
for(std::vector<cv::KeyPoint>::iterator it=keyPoints1_vec.begin();it!=keyPoints1_vec.end();it++)
cv::calcOpticalFlowPyrLK(img1_gray_Mat,img2_gray_Mat,points1_vec,points2_vec, status, err, winSize);

cv::imshow(win1_string, img1_gray_Mat); cv::imshow(win2_string,img2_gray_Mat);
return 0;



edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-01-29 13:32:19 -0600

  1. Both of frames for calcOpticalFlowPyrLK must be gray scale images (1 channel) with the same color depth and image size. It seams that your images differ in some of this parameters.

  2. You tries to draw red circles around features on gray scale one channel image. In this case circle function uses the first element of the cv::Scalar only. You get white circles instead red one. I think it is not the thing you want to do. You need to create seporate Mat with 3 channels, convert your gray scale image to RGB image and then draw circles. You got something like that:

    Mat drawMat; cvtColor(img1_gray_Mat, drawMat, CV_GRAY2RGB);

    for(std::vector<cv::keypoint>::iterator it=keyPoints1_vec.begin() it!=keyPoints1_vec.end(); it++) { points1_vec.push_back(it->pt); cv::circle(drawMat, it->pt,2,cv::Scalar(0,0,255)); }

    cv::imshow(win1_string, drawMat);

edit flag offensive delete link more

Question Tools


Asked: 2013-01-24 12:35:00 -0600

Seen: 2,971 times

Last updated: Jan 29 '13