Opencv step by step - 图像变换

时间:2021-03-15 19:25:55

这里举出三个案例:

#include <cv.h>
#include <highgui.h> void image_smooth(IplImage * image)
{
cvNamedWindow("#1image input");
cvShowImage("#1image input", image); /* create 8 bit 3 channel image*/
IplImage *out_image = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 3);
/* smooth 3*3 around every point */
cvSmooth(image, out_image, CV_GAUSSIAN, 3, 3);
cvNamedWindow("#1image output");
cvShowImage("#1image output", out_image); cvReleaseImage(&out_image);
cvWaitKey(0); cvDestroyWindow("#1image input");
cvDestroyWindow("#1image output");
} IplImage *doPyrDown(IplImage *in)
{
IplImage *out = cvCreateImage(cvSize(in->width/2, in->height/2) ,in->depth, in->nChannels);
cvPyrDown(in, out);
return out;
} IplImage *doCanny(IplImage *in, double lowTh, double highTh, double aperture)
{
//3 channel is ok
printf("channels = %d\n", in->nChannels);
//if(1 != in->nChannels)
// return NULL;
printf("start processing...\n");
IplImage *out = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U, 1);
cvCanny(in, out, lowTh, highTh, aperture);
return out;
} int main(int argc,char **argv)
{ printf("this is image transforming\n");
IplImage *image;
/* todo:how to judge load fail?*/
image = cvLoadImage(argv[1]); if(argc != 2)
{
std::cout << "No image data\n";
return -1;
} /*
image_smooth(image);
*/ cvNamedWindow("image input");
cvNamedWindow("image output");
//IplImage *img1 = doPyrDown(image);//#1
IplImage *img1 = doCanny(image, 10, 100, 3);//#2 cvShowImage("image input", image);
cvShowImage("image output", img1);
cvReleaseImage(&image);
cvReleaseImage(&img1);
cvWaitKey(0);
cvDestroyWindow("image input");
cvDestroyWindow("image output"); return 0;
}

其中,

image_smooth

是将图像平滑处理。

其中关键的一行是

	cvSmooth(image, out_image, CV_GAUSSIAN, 3, 3);

效果如下:

Opencv step by step - 图像变换

这样处理后,图片变模糊了。

doPyrDown

是图像变成原来的0.25倍,它调用的

cvPyrDown

函数对于传入参数有较严格的要求,传入的两个图像要接近0.25倍,详情看官方文档.

Opencv step by step - 图像变换

doCanny

是进行边缘检测,虽然书中不支持三通道的,但是代码里面是支持的,所以我直接传了张一般的图片进去。。

效果如下(女神 奥黛丽·赫本):

Opencv step by step - 图像变换