Ask Your Question

OpenCV Optical Flow Point matching, tiny error

asked 2013-05-17 04:08:29 -0500

januka gravatar image

I am trying to simulate Optical Flow using 2 images. This program give no compilation error,but it crashes when i run it. However i have 2 warning. I think the error could be in vector variable initialization or conversion

warning C4018: '<' : signed/unsigned mismatch warning C4700: uninitialized local variable 'matches' used

#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\video\video.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <set>
using namespace cv;
using namespace std;
int main()
  Mat img1 = imread("0000.jpg", 1);
  Mat img2 = imread("0001.jpg", 1);
  vector<Point2f> j_pts;
  vector<DMatch>* matches;
  vector<Point2f> to_find;
 // Detect keypoints in the left and right images
FastFeatureDetector detector(50);
vector<KeyPoint> left_keypoints,right_keypoints;
detector.detect(img1, left_keypoints);
detector.detect(img2, right_keypoints);
// making sure images are grayscale

Mat prevgray,gray;
if (img1.channels() == 3) {
} else {
prevgray = img1;
gray = img2;
// Calculate the optical flow field:
// how each left_point moved across the 2 images
vector<uchar>vstatus; vector<float>verror;
calcOpticalFlowPyrLK(prevgray, gray, left_points, right_points,vstatus, verror);
// First, filter out the points with high error
for (unsigned int i=0; i<vstatus.size(); i++) {
if (vstatus[i] &&verror[i] < 12.0) {
// Keep the original index of the point in the
// optical flow array, for future use
// Keep the feature point itself
} else {
vstatus[i] = 0; // a bad flow
// for each right_point see which detected feature it belongs to
Mat right_points_to_find_flat = Mat(right_points_to_find).reshape(1,to_find.size()); //flatten array
vector<Point2f>right_features; // detected features
Mat right_features_flat = Mat(right_features).reshape(1,right_features.size());
// Look around each OF point in the right image
// for any features that were detected in its area
// and make a match.
BFMatcher matcher(CV_L2);
// Check that the found neighbors are unique (throw away neighbors
// that are too close together, as they may be confusing)

std::set<int>found_in_right_points; // for duplicate prevention
for(int i=0;i<nearest_neighbors.size();i++) {
DMatch _m;
if(nearest_neighbors[i].size()==1) {
_m = nearest_neighbors[i][0]; // only one neighbor
} else if(nearest_neighbors[i].size()>1) {
// 2 neighbors – check how close they are
double ratio = nearest_neighbors[i][0].distance /
if(ratio < 0.7) { // not too close
// take the closest (first) one
_m = nearest_neighbors[i][0];
} else { // too close – we cannot tell which is better
continue; // did not pass ratio test – throw away
} else {
continue; // no neighbors... :(
// prevent duplicates
if (found_in_right_points.find(_m.trainIdx) == found_in_right_points.
end()) {
// The found neighbor was not yet used:
// We should match it with the original indexing
// ofthe left point
_m.queryIdx = right_points_to_find_back_index[_m.queryIdx];
matches->push_back(_m); // add this match
cout ...
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2013-05-17 04:22:06 -0500

berak gravatar image

vector<DMatch>* matches never gets initialized, that's your problem here.

so matches->push_back(_m) will crash, as well as all further usage of matches

well, you could initialize it properly:

vector<DMatch>* matches = new vector<DMatch>;

but, why a pointer in the first place ?

i'd use vector<DMatch> matches , matches.push_back(_m) , and matches.size() instead

edit flag offensive delete link more


THank you,that removed 1 warning. "warning C4018: '<' : signed/unsigned mismatch " is still there :( When i run the programme it crashes saying 'Vector subscript out of range'

januka gravatar imagejanuka ( 2013-05-17 06:29:01 -0500 )edit
  1. signed/unsigned mismatch, that's probably here: <code>

    for(int i=0;i&lt;nearest_neighbors.size();i++)   // should be unsigned int


  2. the crash(detetctive story) i guess it 's here : right_points_to_find.push_back(j_pts[i]);

i can't find where in your code j_pts is ever initialized. you can't say vector[i] unless you initialize it properly

berak gravatar imageberak ( 2013-05-17 08:55:20 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-05-17 04:08:29 -0500

Seen: 916 times

Last updated: May 17 '13