When capturing moving points, we can use an algorithm called optical flow (optical flow), where the flow derivative of all points is the same around the key point, so that we can determine the key point (feature points)
Here's the code:
Classcameracaliberator
{
Vector<vector<point3f>> objectpoints;
The real points
Vector<vector<point2f>> imagepoints;
Points found Inimage
Mat Cameramatrix;
Mat distcoeffs;
int flag;
Mat Map1, MAP2;
BOOL Mustinitundistort;
Public
Cameracaliberator (): Flag (0), Mustinitundistort (True)
{
};
int Addchessboardpoints (
Const vector<string>& filelist, Size &boardsize) {
Vector<point2f> imagecorners;
Vector<point3f> objectcorners;
for (int i = 0; i < boardsize.height;i++)
{
for (int j = 0; J < boardsize.width;j++)
{
Objectcorners.push_back (point3f (i, j,0.0f));
}
}
Mat image;
int successes = 0;
for (int i = 0; i < filelist.size (); i++)
{
Image = Imread (Filelist[i], 0);
BOOL found = findchessboardcorners (image, Boardsize, imagecorners);
Cornersubpix (image, Imagecorners, size (5, 5), Size (-1,-1),
Termcriteria (Termcriteria::max_iter + termcriteria::eps, 30, 0.1));
Used to define when to stop
if (imagecorners.size () = = Boardsize.area ())
{
Addpoints (Imagecorners, objectcorners);
successes++;
}
}
return successes;
}
Double calibrate (Size &imagesize) {
Mustinitundistort = true;
Vector<mat> Rvecs, Tvecs;
Return Calibratecamera (Objectpoints,
Imagepoints,
ImageSize,
Cameramatrix,
Distcoeffs,
Rvecs,
Tvecs,
Flag);
}
Mat Remap (const mat&image) {
Mat undistorted;
if (Mustinitundistort) {
Initundistortrectifymap (Cameramatrix,
Distcoeffs,
Mat (), Mat (),
Image.size (),
CV_32FC1,
MAP1, MAP2);
}
Mustinitundistort = false;
Cv::remap (image, undistorted, Map1, MAP2, inter_linear);
return undistorted;
};
Set The calibration options
8radialCoeffEnabled shouldbe true if 8 radial coefficients is required (5 is default)
Tangentialparamenabledshould bes True if tangeantial distortion is present
Voidcameracaliberator::setcalibrationflag (bool radial8coeffenabled, booltangentialparamenabled) {
//
Set the flag used in Cv::calibratecamera ()
flag= 0;
if (!tangentialparamenabled) flag + = Cv_calib_zero_tangent_dist;
if (radial8coeffenabled) flag + = Cv_calib_rational_model;
// }
Private
void Addpoints (vector<point2f>& imagecorners,vector<point3f>& objectcorners) {
Imagepoints.push_back (imagecorners);
Objectpoints.push_back (objectcorners);
}
};
void M () {
vector<string> filelist;
Size boardsize (6, 4);
for (int i = 1; i <=; i++) {
StringStream str;
STR << "Chessboards\\chessboard" <<STD::SETW (2) << Std::setfill (' 0 ') << i << ". jpg";
cout << str.str () << Endl;
Filelist.push_back (Str.str ());
Image= Cv::imread (str.str (), 0);
Cv::imshow ("image", image);
//
Cv::waitkey (100);
}
Mat Image=imread (filelist[0]);
Cameracaliberator Cameracaliberator;
Cameracaliberator.addchessboardpoints (filelist,boardsize);
Cameracaliberator.calibrate (Image.size ());
Matuimage= Cameracaliberator.remap (image);
Imshow ("", uimage);
}
Openthe Video file
Cv::videocapturecapture ("Bike.avi");
CHECKIF Video successfully opened
if (!capture.isopened ())
return 1;
Getthe frame rate
Doublerate = Capture.get (Cv_cap_prop_fps);
Boolstop (FALSE);
Cv::matframe; Current video Frame
Cv::namedwindow ("Extractedframe");
Delaybetween each frame in MS
Corresponds to video frame rate
int delay= 1000/rate;
ForAll frames in video
while (!stop) {
Read Next Frame if any
if (!capture.read (frame))
Break
Cv::imshow ("extracted frame", frame);
Introduce a delay
or press key to stop
if (Cv::waitkey (delay) >= 0)
Stop = true;
}
Closethe video file.
Notrequired since called by destructor
Capture.release ();
Class Featuretracker : public Frameprocessor {
Cv::matgray; Current Gray-level image
Cv::matgray_prev; Previous Gray-level image
STD::VECTOR<CV::P oint2f> points[2]; Tracked features From0->1
STD::VECTOR<CV::P oint2f> initial; Initial position of tracked points
STD::VECTOR<CV::P oint2f> features; Detected features
int max_count; Maximum number of features to detect
Double Qlevel; Qualitylevel for feature detection
Double mindist; Minimumdistance between, feature points
std::vector<uchar> status; Status of tracked features
Std::vector<float> err; Error in tracking
Public
Featuretracker (): Max_count ($), Qlevel (0.01), Mindist (10.) {}
Processing method
void Process (CV:: Mat &frame, CV:: Mat &output) {
Convert to Gray-level image
Cv::cvtcolor (frame, Gray, Cv_bgr2gray);
Frame.copyto (output);
1. If new feature points must be added
if (Addnewpoints ())
{
Detect feature Points
Detectfeaturepoints ();
Add the detected features to the currently tracked features
Points[0].insert (Points[0].end (), Features.begin (), Features.end ());
Initial.insert (Initial.end (), Features.begin (), Features.end ());
}
For first image of the sequence
if (Gray_prev.empty ())
Gray.copyto (Gray_prev);
2. Track features
Cv::calcopticalflowpyrlk (Gray_prev, Gray,//2 consecutive images
Points[0],//input point position in first image
POINTS[1],//Output point postion in the second image
Status,//Tracking success
ERR); Tracking error
2. Loop over the tracked points to reject the undesirables
int k=0;
for (int i= 0; i < points[1].size (); i++) {
Do we keep this point?
if (Accepttrackedpoint (i)) {
Keep this point in vector
Initial[k]= Initial[i];
points[1][k++] = Points[1][i];
}
}
Eliminate unsuccesful points
Points[1].resize (k);
Initial.resize (k);
3. Handle the accepted tracked points
Handletrackedpoints (frame, output);
4. Current points and image become previous ones
Std::swap (Points[1], points[0]);
Cv::swap (Gray_prev, Gray);
}
Feature Point Detection
void Detectfeaturepoints () {
Detect the Features
Cv::goodfeaturestotrack (Gray,//the image
Features,//The outputdetected features
Max_count,//The Maximumnumber of features
Qlevel,//quality level
Mindist); Min Distancebetween Features
}
Determine if new points should be added
BOOL Addnewpoints () {
If too few points
Return Points[0].size () <=10;
}
Determine which tracked point should be accepted
BOOL Accepttrackedpoint (int i) {
return Status[i] &&
If Point has moved
(ABS (points[0][i].x-points[1][i].x) +
(ABS (POINTS[0][I].Y-POINTS[1][I].Y)) >2);
}
Handle the currently tracked points
void Handletrackedpoints (CV:: Mat &frame, CV:: Mat &output) {
For all tracked points
for (int i= 0; i < points[1].size (); i++) {
Draw Line and Circle
Cv::line (Output,initial[i], points[1][i], Cv::scalar (255,255,255));
Cv::circle (output, Points[1][i], 3, Cv::scalar (255,255,255),-1);
}
}
};
#endif
OpenCV Getting Started note four read and write video capture move point