Feature point Tracking - c++

I am trying to track set of feature points in a sequence of grayscale images using OpenCV 2.4.0.
I already know how to implement SIFT or SURF for detecting feature points and initially computing the descriptors. However, I need help in computing the SIFT descriptor of a feature point whose location (u,v) is only known to me. A working example code for SIFT is shown below.
For example, if I use Haris corner detector to detect features at dv_scenePoints_t like:
cvGoodFeaturesToTrack (source2, eig_img, temp_img, dv_scenePoints_t, &corner_count, 0.3, 3.0, mask, 7, 1);
Then in such a case, how would I compute the SIFT descriptor of points at dv_scenePoints_t.
Also, if I have to track feature points by a particle filter. Then, how would I use the SIFT descriptor for computing the weight of each particle (feature point hypothesis).
Thanks.
#include "stdafx.h"
#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/nonfree/features2d.hpp>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/legacy/legacy.hpp"
#include "opencv2/legacy/compat.hpp"
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <string.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
Mat source1 = imread("KITTI_train.png",CV_LOAD_IMAGE_GRAYSCALE);
Mat source2 = imread("KITTI_trainRotate90.png",CV_LOAD_IMAGE_GRAYSCALE);
vector<KeyPoint> dv_sceneKeypoints_t, dv_objectKeypoints_t;
vector< DMatch > matches;
SiftFeatureDetector detector(400,5,0.03);
detector.detect(source1, dv_objectKeypoints_t);
detector.detect(source2, dv_sceneKeypoints_t);
SiftDescriptorExtractor extractor;
Mat descriptors1,descriptors2;
extractor.compute(source1,dv_objectKeypoints_t,descriptors1);
extractor.compute(source2,dv_sceneKeypoints_t,descriptors2);
FlannBasedMatcher matcher;
matcher.match(descriptors1,descriptors2, matches);
Mat target;
drawMatches(source1,dv_objectKeypoints_t,source2,dv_sceneKeypoints_t,matches,target);
imshow("Matches", target);
waitKey(0);
return 0;
}

Keypoint structure contains some members such as size and response:
http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_feature_detectors.html?#KeyPoint
You can use these features to determine the relative weights of particles.

Related

OpenCV 3.0 Segmentation Fault (Bag of visual words)

I am trying to set up a bag of visual words using openCV 3.0. I have looked a bit everywhere and all I seem to be able to find is code that is only compatible with versions in the 2.x domain. As of now this is what I have:
#include <opencv2/core/core.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, const char** argv) {
Ptr<FeatureDetector> features;
Ptr<DescriptorExtractor> descriptors;
Ptr<DescriptorMatcher> matcher;
int MAX_ITER = 100;
int EPS = 2;
TermCriteria tc(MAX_ITER + EPS,1,0.001);
int dictSize = 1000;
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bowTrainer(dictSize,tc,retries,flags);
BOWImgDescriptorExtractor bowDE(descriptors,matcher);
Mat img1 = imread("/Users/Lucas/Desktop/pic2.jpg");
Mat img2 = imread("/Users/Lucas/Desktop/2.jpg");
vector<KeyPoint> keypoints,keypoints2;
features->detect(img1, keypoints);
features->detect(img2, keypoints2);
Mat myFeatures;
Mat myFeatures2;
descriptors->compute(img1, keypoints, myFeatures);
descriptors->compute(img2, keypoints2, myFeatures2);
bowTrainer.add(myFeatures);
bowTrainer.add(myFeatures2);
Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);
cout << dictionary << endl;
return 0;
}
I have put this together by using a few tutorials and snippets, but I am running into an issue. When the program gets to
features->detect(img1, keypoints);
it exits with a segmentation fault 11, whatever that means. Could someone help me and point out what it is I am doing wrong?
you have to create your FeatureDetector, DescriptorExtractor first. atm, you got null-pointer instances (that's your segfault).
#include <opencv2/xfeatures2d.hpp>
...
Ptr<FeatureDetector> features = xfeatures2d::SIFT::create();
Ptr<DescriptorExtractor> descriptors = xfeatures2d::SIFT::create();
Ptr<DescriptorMatcher> matcher = makePtr<BFMatcher>(NORM_L2);
note, that since you have to use SIFT or SURF, you will need the opencv_contrib repo installed for this

OpenCV resize is not a member of cv (OpenCV Basics)

I successfully wrote a tool that converts an image's colors space from linear to sRGB, so opencv is working. Then i wanted to rescale the image with the cv::resize function to generate Thumbnails. However it didn't work, here is the reproduced code-snippet.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
// Load images in the C++ format
cv::Mat img = cv::imread("something.jpg");
cv::Mat src = cv::imread("src.jpg");
// Resize src so that is has the same size as img
**cv::resize**(src, src, img.size());
return 0;
}
I am using OpenCV 2.4.8. What am i doing wrong?
you're lacking a header file:
#include "opencv2/imgproc/imgproc.hpp"
(ofc, you have to link opencv_imgproc, too)
#include "opencv2/opencv.hpp"
would have avoided the 1st error, but you still have to care for the correct libs

Exception thrown writing to InputOutputArray

I'm trying to test some motion estimation in Visual Studio 2013, using OpenCV v3.0 (which is probably my 1st mistake!). I got an unhandled exception trying to use createOptFlow_DualTVL1() and createOptFlow_Farneback(), and then, for testing, tried cv::accumulate(), which threw the same exception.
It seems that OpenCV can't write to the Mat object that I'm passing these functions. I can't read the actual cvException because I don't have the PDB files, because I didn't compile this version myself. That might be my next stop, but before I do I figured I'd see if anyone's seen this behaviour before.
Here's a minimal working example:
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/tracking.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
#include <regex>
using namespace std;
using namespace cv;
int main(int argc, const char** argv)
{
VideoCapture captureDevice;
std::string videoname = "example.mp4";
captureDevice.open(videoname);
//setup image files used in the capture process
Mat currFrame, dst;
captureDevice >> currFrame;
accumulate(currFrame, dst, cv::noArray());
imshow("outputCapture", dst);
//pause for 33ms
waitKey(33);
return 0;
}
dst should be of same size as that currFrame and of type CV_32FC3.
So, add this line of code before calling accumulate -
dst.create(currFrame.size(), CV_32FC3);
Since dst is of float type, you will need to convert it to uchar to display it. For that, convert as shown below -
Mat dst_disp;
dst.convertTo(dst_disp, CV_8UC3);
imshow("outputCapture",dst_disp );
Additionally, as you accumulate more frames in dst, you will need to normalize by number of frames(let's say N) cached in dst. Simply, divide dst by the N, then convert the result into CV_8UC3 and display. For example, if you accumulated 1000 frames in dst do as shown below,
// Accumulate 1000 frames
for(int i = 0; i < 1000; i++)
accumulate(currFrame, dst, cv::noArray());
// Normalize
dst = dst/ 1000;
// Display the frame
Mat dst_disp;
dst.convertTo(dst_disp, CV_8UC3);
imshow("outputCapture",dst_disp );
else, you might get an all white image.
UPDATE
From #berak's comment below.
For normalization, simply use
dst.convert(dst_disp, CV_8UC3, 1.0/N);
where N in example above will be 1000.

OpenCV SURF extractor.compute error

I use OpenCV 2.44 and Visual Studio C++ 2010
When I compile this
#include <opencv2/imgproc/imgproc_c.h>
#include <stdio.h>
#include <math.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>
using namespace cv;
void main()
{
Mat img1 = imread( "hh.jpg", CV_LOAD_IMAGE_GRAYSCALE );
Mat img2 = imread( "hh.jpg", CV_LOAD_IMAGE_GRAYSCALE );
// detecting keypoints
FastFeatureDetector detector(15);
vector<KeyPoint> keypoints1;
detector.detect(img1, keypoints1);
// computing descriptors
SurfDescriptorExtractor extractor;
Mat descriptors1;
extractor.compute(img1, keypoints1, descriptors1);
when I run the code I get Unhandled exception at 0x580f375b in prj.exe: 0xC0000005: Access violation reading location 0x001f7014.
the error is at extractor
I'm using this tutorial link
It's looks like that you forget to init non-free module. Try to call appropriate function before using SurfDescriptorExtractor:
#include <opencv2/nonfree/nonfree.hpp>
...
cv::initModule_nonfree();

OpenCV 2.42 FeatureDetector FREAK

I want to try the new class FREAK in OpenCV 2.4.2.
I tried to use common interface of feature detector to construct FREAK, but,of course, it doesn't work. How should I revise my code to get result?
#include <stdio.h>
#include <iostream>
#include <opencv\cxcore.h>
#include <opencv2\nonfree\features2d.hpp>
#include <opencv\highgui.h>
#include <opencv2\features2d\features2d.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main(){
Mat mat1;
mat1 = imread("Testimg06.jpg",0);
vector<KeyPoint> P1;
Ptr<FeatureDetector> freakdes;
Ptr<DescriptorExtractor> descriptorExtractor;
freakdes = FeatureDetector::create("FREAK");
freakdes->detect(mat1,P1);
Mat keypoint_img;
drawKeypoints( mat1, P1, keypoint_img, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
imshow("Keypoints 1", keypoint_img );
cvWaitKey(0);
}
FREAK is descriptor only. There is no corresponding feature detector.
So you need to combine it with one of the available detectors: FAST, ORB, SIFT, SURF, MSER or use goodFeaturesToTrack function.
There is an OpenCV example that shows how to use FREAK combined with FAST.
The basic instructions are:
FREAK extractor;
BruteForceMatcher<Hamming> matcher;
std::vector<KeyPoint> keypointsA, keypointsB;
Mat descriptorsA, descriptorsB;
std::vector<DMatch> matches;
FAST(imgA,keypointsA,10);
FAST(imgB,keypointsB,10);
extractor.compute( imgA, keypointsA, descriptorsA );
extractor.compute( imgB, keypointsB, descriptorsB );
matcher.match(descriptorsA, descriptorsB, matches);