OpenCV VideoCapture C++: reading 2 frames gives the same picture -



OpenCV VideoCapture C++: reading 2 frames gives the same picture -

i writing programme loop captures 2 different frames in each iteration of loop. webcam directed @ changing.

the 1st , sec frame within single iteration of loop different each other, expected. however, many times (but not of time), 2nd frame of i'th iteration of loop looks exact same 1st frame of (i+1)th iteration of loop. why happen? here code...

#include "opencv2/core/core.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/nonfree/features2d.hpp" #include "opencv2/calib3d/calib3d.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/nonfree/nonfree.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <fstream> #include <sstream> #include <stdlib.h> using namespace cv; using namespace std; int main(int argc, const char *argv[]) { //open webcam capture video int choice; cout << " 1. save frames , homography matrices file.\n"; cout << " 2. read in frames , homography matrices file.\n"; cin >> choice; // selection 1: output if (choice == 1){ cout << "you picked selection 1!"; videocapture cap(0); if(!cap.isopened()) homecoming -1; mat frame1c; // hold i'th frame, color mat frame1; // ^^, grayscale mat frame2c; // hold (i+1)'th frame, color mat frame2; // ^^, grayscale std::vector<mat> theframes; // hold frames int myvar; string hmatrices; // hold values of h matrices. std::stringstream s; //used converting numerical values string //for(;;){ for(int = 0; i<10; i++){ // capture 2 frames cvwaitkey(500); cap.read(frame1c); cvwaitkey(200); cap.read(frame2c); cvwaitkey(200); cvtcolor(frame1c,frame1,cv_rgb2gray); cvtcolor(frame2c,frame2,cv_rgb2gray); //detect points on 2 successive images int minhessian = 400; surffeaturedetector detector(minhessian); std::vector<keypoint> keypoints1, keypoints2; detector.detect(frame1, keypoints1); detector.detect(frame2, keypoints2); // if not plenty keypoints found (e.g. when thinkpad webcam displays black screen initially), // go next iteration of loop. if (keypoints1.size() < 4){ cout << " keypoints size <4\n\n"; continue; } // if frames have keypoints, save them "theframes" cout << "pushback " << << " f1c\n"; theframes.push_back(frame1c.clone()); cout << "pushback " << << " f2c\n"; theframes.push_back(frame2c.clone()); // calculate feature-vectors of points surfdescriptorextractor extractor; mat descriptors1, descriptors2; extractor.compute(frame1, keypoints1, descriptors1); extractor.compute(frame2, keypoints2, descriptors2); // match points on 2 successive images comparing feature-vectors flannbasedmatcher matcher; std::vector<dmatch> matches; matcher.match(descriptors1, descriptors2, matches); cout << " matches size " << matches.size(); //eliminate weaker matches double maxdist = 0; double mindist = 100; (int j = 0; j < descriptors1.rows; j++){ double dist = matches[j].distance; if( dist < mindist ) mindist = dist; if( dist > maxdist ) maxdist = dist; } //build list of "good" matches std::vector<dmatch> goodmatches; for( int k = 0; k < descriptors1.rows; k++ ){ if( matches[k].distance <= 3*mindist ){ goodmatches.push_back(matches[k]); } } //now compute homography matrix between stronger matches //-- localize object std::vector<point2f> obj; std::vector<point2f> scene; cout << "goodmatches size " << goodmatches.size(); unsigned int i; std::stringstream s; for(int l = 0; l < goodmatches.size(); l++){ //-- keypoints matches obj.push_back(keypoints1[goodmatches[l].queryidx].pt); scene.push_back(keypoints2[goodmatches[l].trainidx].pt); } mat hmatrix; cout << "obj size " << obj.size(); hmatrix = findhomography(obj, scene, cv_ransac); cout << "found hmatrix\n"; //from http://computer-vision-talks.com/articles/2013-06-07-undocumented-opencv/ //the documentation of cv::findhomography not state it, homecoming value of cv::findhomography 3x3 matrix of cv_64fc1 type //cv_64fc1 type double // can cout << m.at<double>(0,0), cout << m.at<double>(0,1), etc // save values of h matrix hmatrices string s << hmatrix.at<double>(0,0) << " "; s << hmatrix.at<double>(0,1) << " "; s << hmatrix.at<double>(0,2) << " "; s << hmatrix.at<double>(1,0) << " "; s << hmatrix.at<double>(1,1) << " "; s << hmatrix.at<double>(1,2) << " "; s << hmatrix.at<double>(2,0) << " "; s << hmatrix.at<double>(2,1) << " "; s << hmatrix.at<double>(2,2) << "\n"; hmatrices = hmatrices + s.str(); /* std::string test = s.str(); cout << "now printing matrix " << test << " , done"; */ }//end loop //std::string tosave = s.str(); cout << "now printing matrix " << hmatrices << " , done"; ofstream savetofile; savetofile.open("hmatrices.txt"); hmatrices.pop_back(); savetofile << hmatrices; savetofile.close(); // save theframes jpeg files: for(int m = 0; m<theframes.size(); m++){ cout << "saving jpg" << m << "num\n"; imwrite("frame" + std::to_string((long long)m) + ".jpg", theframes[m]); cout << "made after theframes \n"; cout << "inforloop theframes size " << theframes.size() << " that\n"; } int theint; cin >> theint; homecoming 0;} //choice 2: read in else{ string filename; cout << "you picked selection 2!"; cout << "enter name of file homography matrices: "; cin >> filename; cout << "\nyou entered filename " << filename << "\n"; // read in homography matrices ifstream readh; readh.open(filename); vector <double[3]> hs; int counter = 0; int = 0; cout << "made far"; vector<mat> thematrices; double dataformat [3][3]; while(readh>>dataformat[0][0]>>dataformat[0][1]>>dataformat[0][2]>>dataformat[1][0]>>dataformat[1][1]>>dataformat[1][2]>>dataformat[2][0]>>dataformat[2][1]>>dataformat[2][2]){ cout << "made whileloop iteration number " << i; thematrices.push_back(mat(3,3,cv_64fc1,dataformat)); cout << "now printing matrix in mat form..\n"; cout << thematrices[i]; cout << "\nfinished printing mat matrix\n"; i++; } int numh = thematrices.size(); vector<mat> theimages; // read in image files, named i.jpg = 1,2,3,..... for(int n = 0; n < numh; n++){ theimages.push_back(imread("frame" + std::to_string((long long)n) + ".jpg")); } imshow("title",theimages[0]); waitkey(1000); imshow("title",theimages[1]); waitkey(1000); imshow("title",theimages[2]); waitkey(1000); imshow("title",theimages[3]); waitkey(1000); /* int theint; cin >> theint;*/ homecoming 0;} homecoming 0; }

what fps of camera? it's quite possible in loop reading lastly frame photographic camera , when seek read 1 time again (second time in loop) there no new frame photographic camera gives latest frame. seek check whether frames equal , if read next frame till 'something new'. , if want sure images equal compare both images using subtract function , phone call countnonzero. if result 0, images equal.

c++ opencv webcam

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -