首页 > 代码库 > 调用摄像头,并进行灰度,边缘检测

调用摄像头,并进行灰度,边缘检测

// 机器人.cpp : 定义控制台应用程序的入口点。
//

 


#include "stdafx.h"

IplImage * doPyrDown(IplImage* in, int filter = IPL_GAUSSIAN_5x5)//使用cvPyrDown()创建一个宽度和高度为输入图像一般尺寸的图像
{
assert(in->width % 2 == 0 && in->height % 2 == 0);
IplImage * out = cvCreateImage(cvSize(in->width / 2, in->height / 2), in->depth, in->nChannels);
cvPyrDown(in, out);
/*
该函数实现创建高度和宽度为输入图像的的一般的尺寸
其中的filter在这个函数中为默认定义
其真实形式为 cvPyDown(in,out,filter);
IPL_GAUSSIAN_5x5意思是默认用的就是5*5的高斯模板。
*/
return (out);
};

IplImage* doCanny(IplImage* in, double low, double high, double aperture)//为图像做边缘检测
{
// if (in->nChannels != 1) return 0;
IplImage* out = cvCreateImage(
cvGetSize(in),
IPL_DEPTH_8U,
1);
cvCanny(in, out, low, high, aperture);
return (out);
};


IplImage* doGray(IplImage* in)//为图像做灰度
{
IplImage* out =cvCreateImage(cvGetSize(in), IPL_DEPTH_8U, 1);
cvConvertImage(in, out, 1);
return (out);
}


/*IplImage* dobinary(IplImage* in)//为图像做二值化
{
IplImage* out = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U, 1);
cvThreshold(in,out, 200, 255, CV_THRESH_BINARY);
return out;
}*/

 

void main()
{
cvNamedWindow("Video", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Gray", CV_WINDOW_AUTOSIZE);
cvNamedWindow("binary", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Canny", CV_WINDOW_AUTOSIZE);
CvCapture * capture;


capture = cvCreateCameraCapture(0);
IplImage* in;
while (1){
in = cvQueryFrame(capture);
IplImage* img1 = doPyrDown(in);
IplImage* img2 = doGray(img1);
IplImage* img3 = dobinary(img2);
IplImage* img4 = doCanny(img1, 10,100,3);

cvShowImage("Video", in);
cvFlip(img2,img2);
cvShowImage("Gray", img2);
cvFlip(img3,img3);
cvShowImage("binary", img3);
cvShowImage("Canny", img4);


char c = cvWaitKey(33);
if (c == 27)
break;
in = cvQueryFrame(capture);
}
}

调用摄像头,并进行灰度,边缘检测