OpenCV学习笔记__入门篇(一)

对图像的处理:

缩放处理cvPyrDown()、灰度处理cvCvtColor()、边缘检测cvCanny    看下面的代码

#include "cv.h"
#include "highgui.h"

IplImage* doCanny(IplImage* in,double lowThresh,double highThresh,double aperture)
{
	IplImage* out = cvCreateImage(cvGetSize(in),in->depth,1);
	cvCanny(in, out, lowThresh, highThresh, aperture);
	return(out);
};

IplImage* doPyrDown(IplImage* in)
{
	IplImage* out = cvCreateImage(cvSize(in->width / 2, in->height / 2),in->depth,in->nChannels);
	cvPyrDown(in, out);
	return(out);
};

int main(int argc, char** argv)
{
	IplImage* img_rgb = cvLoadImage("e:\\jay.jpg");
	IplImage* img_gry = cvCreateImage(cvSize(img_rgb->width, img_rgb->height), img_rgb->depth, 1);
	cvCvtColor(img_rgb, img_gry, CV_BGR2GRAY);
	IplImage* img_pyr = doPyrDown(img_gry);
	IplImage* img_pyr2 = doPyrDown(img_pyr);
	IplImage* img_cny = doCanny(img_pyr2, 10, 100, 3);

	cvNamedWindow("Example Gray", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Example Pyr", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Example Canny", CV_WINDOW_AUTOSIZE);
	cvShowImage("Example Gray", img_gry);
	cvShowImage("Example Pyr", img_pyr2);
	cvShowImage("Example Canny", img_cny);
	cvWaitKey(0);
	cvReleaseImage(&img_rgb);
	cvReleaseImage(&img_gry);
	cvReleaseImage(&img_pyr);
	cvReleaseImage(&img_pyr2);
	cvReleaseImage(&img_cny);
	cvDestroyWindow("Example Gray");
	cvDestroyWindow("Example Pyr");
	cvDestroyWindow("Example Canny");
}


#include "cv.h"
#include "highgui.h"

IplImage* doCanny(IplImage* in,double lowThresh,double highThresh,double aperture)
{
	IplImage* out = cvCreateImage(cvGetSize(in),in->depth,1);
	cvCanny(in, out, lowThresh, highThresh, aperture);
	return(out);
};

IplImage* doPyrDown(IplImage* in)
{
	// Best to make sure input image is divisible by two.
	//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);
	return(out);
};

int main(int argc, char** argv)
{
	cvNamedWindow("Example Gray", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Example Pyr", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Example Canny", CV_WINDOW_AUTOSIZE);
	IplImage* img_rgb = cvLoadImage("e:\\jay.jpg");
	IplImage* out;

	out = cvCreateImage(cvSize(img_rgb->width, img_rgb->height), img_rgb->depth, 1);
	cvCvtColor(img_rgb, out, CV_BGR2GRAY);
	cvShowImage("Example Gray", out);
	out = doPyrDown(out);
	out = doPyrDown(out);
	cvShowImage("Example Pyr", out);
	out = doCanny(out, 10, 100, 3);
	cvShowImage("Example Canny", out);

	cvWaitKey(0);
	cvReleaseImage(&out);
	cvDestroyWindow("Example Gray");
	cvDestroyWindow("Example Pyr");
	cvDestroyWindow("Example Canny");
}

OpenCV学习笔记__入门篇(一)_第1张图片

上面的两个代码事例的区别涉及到内存释放问题......

你可能感兴趣的:(OpenCV)