opencv,调用摄像头对画面进行canny处理


注意canny处理的一定是灰度图像,所以先转灰度图像再处理


#include <opencv2/opencv.hpp>  

using namespace cv;
using namespace std; 



//定义视频中的帧图像,灰度帧图像和边缘检测图像
Mat frame, frame_gray, frame_canny;
int a = 70;
//cvCreateTrackbar的回调函数  
void on_trackbar(int threshold)
{
//canny边缘检测  
Canny(frame_gray, frame_canny, threshold, threshold * 3, 3);
imshow("Canny", frame_canny);
}
int main(int argc, char *argv[])
{
VideoCapture capture;
capture.open(0);
bool fromfile = false;


//Init camera  
if (!capture.isOpened())
{
cout << "capture device failed to open!" << endl;
return -1;
}




//创建窗口 
cvNamedWindow("Video", CV_WINDOW_AUTOSIZE);  //原图
cvNamedWindow("Gray", CV_WINDOW_AUTOSIZE);  //灰度图
cvNamedWindow("Canny", CV_WINDOW_AUTOSIZE);  //边缘图




capture >> frame;  //原图
cvtColor(frame, frame_gray, CV_RGB2GRAY); //灰度




while (1)
{
if (!fromfile)
capture >> frame;

cvtColor(frame, frame_gray, CV_RGB2GRAY);
imshow("Video", frame);
imshow("Gray", frame_gray);
on_trackbar(a);
if (cvWaitKey(20) == 'q')
return 1;
}


return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值