qq_26845835
INFP_DIP
采纳率0%
2017-08-16 09:09 阅读 2.2k

opencv如何实现在视频中画任意四边形进行检测?

#include<iostream>  

#include

#include

#include

#include
#include

using namespace std;

using namespace cv;

Mat srcframe, tmpframe, copyframe;

vector pt1;

const string wndName("输出视频");

static void onMouse(int, int, int, int, void*);//SetMouseCallBack的回调函数

int main()

{

system("color 2F");//设定界面颜色
bool stop = false;
bool gotROI = false;
VideoCapture cap("F:/视频检测/测试视频/20100323_华美达广场对面.avi");
if (!cap.isOpened())

{

cout << "读取视频有误" << endl;

return -1;

}
double rate = cap.get(CV_CAP_PROP_FPS);

namedWindow("输出视频",1);

while(!stop)
{               
    cap>>srcframe;
    if(!srcframe.data)
        break;
    imshow("输出视频",srcframe);
    int delay = 1000 / rate;
    int c = waitKey(delay);  
    if ((char)c == 27 )  
    {  
        stop = true;  
    } 
    setMouseCallback("输出视频",onMouse,NULL);
    if (c >= 0)  
    {  
        waitKey(0);  
    } 
}
waitKey(0);  
return 0;  

}

static void onMouse(int event, int x, int y, int flags, void*)

{

static int i = 0;//点击计数

switch (event)

{

case CV_EVENT_LBUTTONDOWN://左击,获取坐标
pt1.push_back(Point(x, y));
if (i == 0||i % 4 == 0)

{

        circle(srcframe, pt1[i], 1, Scalar(0, 0, 255));//描第一个点  
    }  
    else  
    {  
        if((i+1)%4 == 0)
        {

            circle(srcframe, pt1[i], 1, Scalar(0, 0, 255));//描点 
            line(srcframe, pt1[i], pt1[i-1], Scalar(0, 255, 0));//与前一点进行连
            line(srcframe, pt1[i], pt1[i-3], Scalar(0, 255, 0));//与前一点进行连
            line(srcframe, pt1[i], pt1[i-1], Scalar(255));//映射到掩
            line(srcframe, pt1[i], pt1[i-3], Scalar(255));//映射到掩
        }
        else
        {

            circle(srcframe, pt1[i], 1, Scalar(0, 0, 255));//描点  
            line(srcframe, pt1[i], pt1[i-1], Scalar(0, 255, 0));//与前一点进行连
            line(srcframe, pt1[i], pt1[i-1], Scalar(255));//映射到掩
        }
    }  
    i++;  
    break;  

}  
imshow(wndName, srcframe);  

}

如上,我的代码能实现在停止的视频帧上画出任意四边形,但是如何能实现四边形一直显示在视频中.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐