注意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;
}