2015-05-14 134 views
0

我是opencv的新手。只是设法安装并设置为Visual 2013.我用我的笔记本电脑相机的实时流样本对其进行了测试,结果正常。现在我想计算与网络摄像头之间的距离,将其显示在屏幕中间的一个红色激光点(live_stream)。告诉我我从哪里开始?我知道我必须从屏幕中间找到R(红色)像素,但我不知道如何做到这一点以及我可以使用哪些功能。请帮忙吗? 从摄像头的作品的实时流如下所示:用opencv计算到红点的距离

#include "opencv2/highgui/highgui.hpp" 
    #include <opencv2/objdetect/objdetect.hpp> 
    #include <opencv2/imgproc/imgproc.hpp> 
    #include <iostream> 
    #include <vector> 
    #include <stdio.h> 

int main() 
{ 

//Data Structure to store cam. 
CvCapture* cap=cvCreateCameraCapture(0); 
//Image variable to store frame 
IplImage* frame; 
//Window to show livefeed 
cvNamedWindow("Imagine Live",CV_WINDOW_AUTOSIZE); 

while(1) 
{ 
//Load the next frame 
frame=cvQueryFrame(cap); 
//If frame is not loaded break from the loop 
if(!frame) 
    printf("\nno");; 
//Show the present frame 
cvShowImage("Imagine Live",frame); 
//Escape Sequence 
char c=cvWaitKey(33); 
//If the key pressed by user is Esc(ASCII is 27) then break out of the loop 
if(c==27) 
    break; 
} 
//CleanUp 
cvReleaseCapture(&cap); 
cvDestroyAllWindows(); 
} 
+1

请不要使用弃用的C Api(IplImage,cvShowImage等),因为您会遇到如此多的问题。 – GPPK

+0

那我该用什么? –

+0

最好是opencv 3.0,但它在测试版中,所以使用2.4.10并使用C++ Api(CV :: mat,CV :: imshow)等 – GPPK

回答

0

你的红点是最有可能会显示为相机流中的全白,所以我建议

  1. 转换为灰度使用cvtColor()
  2. 阈值使用threshold(),对于参数使用类似thresh = 253,maxval = 255和模式THRESH_BINARY。这应该会给你一个全黑的图像,并且你的激光是一个小白点。
  3. 然后,您可以使用findContours()在图像中找到您的点。获取轮廓的boundingRect(),然后可以计算其中心以获得点的精确坐标。

同样如前所述,不要使用已弃用的C API,而应使用C++ API。

+0

谢谢!我会尝试你告诉我的,我会带回更多的问题。 –