Image processing 使用miniflann时出现OpenCV错误
我在网上找到了这个代码。。我想试试看>Image processing 使用miniflann时出现OpenCV错误,image-processing,opencv,Image Processing,Opencv,我在网上找到了这个代码。。我想试试看> int main( int argc, char **argv ) { VideoCapture cam1, cam2; //Middle and left cameras //VideoCapture cam3; //Right camera cam1.open(0); cam2.open(1); //cam3.open(2); cam1.set(CV_CAP_PROP_FRAME_WIDTH, 320); cam1.set(CV_CAP_PROP
int main( int argc, char **argv )
{
VideoCapture cam1, cam2; //Middle and left cameras
//VideoCapture cam3; //Right camera
cam1.open(0);
cam2.open(1);
//cam3.open(2);
cam1.set(CV_CAP_PROP_FRAME_WIDTH, 320);
cam1.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
cam2.set(CV_CAP_PROP_FRAME_WIDTH, 320);
cam2.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
//cam3.set(CV_CAP_PROP_FRAME_WIDTH, 320);
//cam3.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
Mat frm1, frm2;
while(1)
{
//Step 1: Get frames from a few cameras
cam1 >> frm1; //Reference plane
cam2 >> frm2; //Right-side target plane
// cam3 >> frm3;
if( frm1.empty()||frm2.empty() )
break;
//Step2: SURF detection
int minHessian = 800;
SurfFeatureDetector detector( minHessian );
vector<KeyPoint> keypoints_1, keypoints_2;
detector.detect( frm1, keypoints_1 );
detector.detect( frm2, keypoints_2 );
SurfDescriptorExtractor extractor; ///
Mat descriptors_1, descriptors_2;
extractor.compute( frm1, keypoints_1, descriptors_1 );
extractor.compute( frm2, keypoints_2, descriptors_2 );
//Step 3: Feature matching
//-- Matching descriptor vectors with a matcher
FlannBasedMatcher matcher;
vector< DMatch > matches;
matcher.match( descriptors_1, descriptors_2, matches);
//vector< vector<DMatch> > matches;
//matcher.knnMatch( descriptors_1, descriptors_2, matches,2);
double max_dist = 0; double min_dist = 50;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_1.rows; i++ )
{ double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
//printf("-- Max dist : %f \n", max_dist );
//printf("-- Min dist : %f \n", min_dist );
//-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
vector< DMatch > good_matches;
for( int i = 0; i < descriptors_1.rows; i++ )
{ if( matches[i].distance < 2*min_dist )
{ good_matches.push_back( matches[i]); }
}
Mat img_matches;
drawMatches( frm1, keypoints_1, frm2, keypoints_2,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
//-- Localize the object from frame1 in frame2
vector<Point2f> frame1;
vector<Point2f> frame2;
for( int i = 0; i < good_matches.size(); i++ )
{
//-- Get the keypoints from the good matches
frame1.push_back( keypoints_1[ good_matches[i].queryIdx ].pt );
frame2.push_back( keypoints_2[ good_matches[i].trainIdx ].pt );
}
//Step 4: RANSAC and Homography
Mat H = findHomography( Mat(frame2), Mat(frame1), CV_RANSAC, 5.0 );
cout << "Homography for mapping the r-side plane to the ref. plane: " << endl << H << endl;
//-- Show detected matches
imshow("Matches", img_matches );
//Step 5:Warping
Mat result;
warpPerspective(frm2,result,H,Size(3*frm1.cols,1.5*frm1.rows));
imshow("Warping result",result);
Mat half(result,Rect(0,0,frm2.cols,frm2.rows));
frm1.copyTo(half);//Reference image
//Step 6:Blending
//Step 7:Showing the panoramic video
imshow("Blended view",result);
char c=waitKey(20);
if (c==27)
break;
}
return 0;
}
你知道这里会出什么问题吗
谢谢我的老板建议重新启动电脑,因为我用的是USB摄像头。。他说有时候你需要在一些linux系统中重启它,比如Ubuntu,它才能再次工作!你猜怎么着?重启我的电脑后,它真的起作用了 以下内容解决了我的问题: 在第3步之前添加以下内容:
descriptors_1.convertTo(descriptors_1,CV_32F);
descriptors_2.convertTo(descriptors_2,CV_32F);
希望这有帮助。您是否连接了两个摄像头?您是否检查过是否可以同时打开这两个文件?为什么要使用旧版本的OpenCV?添加一些错误检查怎么样?是的,我在不同的代码中测试了它,我同时从3个不同的摄像头获取视频,效果很好!你指的是哪个版本?2.4.1 ? 我不认为这是版本控制问题,是吗?嗨,我也是。我也有同样的问题,但在windows中,重新启动并没有解决我的问题,有什么帮助吗?你在尝试完全相同的代码吗?因为有时它会因为图像之间没有匹配而终止。是的,确切地说,它会因为没有匹配而终止,这是因为我的相机帧没有得到正确处理,我没有找到解决方案,但通过cam进行了更改,效果很好。至于python接口,请尝试
des1.astype(np.float32)
。
descriptors_1.convertTo(descriptors_1,CV_32F);
descriptors_2.convertTo(descriptors_2,CV_32F);