一个简单的变换:载入一副图像进行平滑处理
#include
#include
#include
using namespace std;
void example2_4(IplImage* image)
{
//Create some windows to show the input
//and output images in.
//
cvNamedWindow("Example4-in");
cvNamedWindow("Example4-out");
//Create a window to show our input image
//
cvShowImage("Example4-in", image);
//Create an image to hold the smoothed output
//cvCreateImage为创建图像的函数
//
IplImage* out = cvCreateImage(
cvGetSize(image), //当前图像结构的大小
IPL_DEPTH_8U, //每个像素点的数据类型
3 //通道数
);
//Do the smoothing
//cvSmooth为平滑处理函数
//
cvSmooth(image, out, CV_GAUSSIAN, 3, 3); //参数分别表示:输入图像,输出图像,平滑处理的方法,平滑处理的相关参数
//此处最后两个参数表示使用每个像素周围3*3的区域进行高斯平滑处理
//show the smoothed image in the output window
//
cvShowImage("Example4-out", out);
//be tidy
//
cvReleaseImage(&out);
//wait for the user to hit a key,then clean up the windows
//
cvWaitKey(0);
cvDestroyWindow("Example4-in");
cvDestroyWindow("Example4-out");
}
int main()
{
IplImage* img = cvLoadImage("Example1.jpg");
example2_4(img);
cvReleaseImage(&img);
}
(1)对图像尺寸进行变换
#include
#include
#include
using namespace std;
IplImage* doPyrDowm(IplImage* in, int filter = CV_GAUSSIAN_5x5) // CV_GAUSSIAN_5x5在书中为IPL_GAUSSIAN_5x5,但IPL_GAUSSIAN_5x5不在库中
{
//best to make sure input image is divisible by two.
//
//assert(in->width % 2 != 0 && in->height % 2 == 0); //条件正确继续进行,错误则终止程序,根据资料此函数不常用
//若将in->width % 2 != 0改为in->width % 2 == 0,不可运行
//思考后得出:可能是因为老版本对尺寸要求比较严格,3.0版opencv无尺寸问题
IplImage* out = cvCreateImage( CvSize(in->width / 2, in->height / 2),
//大小只有输入图像1/4的图像
in->depth, //输入图像每个像素点的数据类型
in->nChannels); //输入图像的通道数
cvPyrDown(in, out);
return(out);
}
int main()
{
IplImage* img1 = cvLoadImage("Example1.jpg");
IplImage* img = doPyrDowm(img1, CV_GAUSSIAN_5x5);
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvReleaseImage(&img1);
cvDestroyWindow("Example1");
}
(2)Canny边缘检测将输出写入一个单通道(灰度级)图像
#include
#include
#include
using namespace std;
IplImage* doCanny(IplImage* in, double lowThresh, double highThresh, double aperture)
{
if (in->nChannels != 1) //确保输入图像为灰度图像
return(0);
IplImage* out = cvCreateImage( CvSize(cvGetSize(in)), //cvGetSize函数提取输入函数的图像大小
IPL_DEPTH_8U, //IPL_DEPTH_8U表示图像像素类型:无符号8位整数
1); //单通道(灰度级)
cvCanny(in, out, lowThresh, highThresh, aperture); //见下方函数说明
return(out);
}
int main()
{
IplImage* img1 = cvLoadImage("Example1.jpg",0);
IplImage* img = doCanny(img1, 10, 100, 3);
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
cvShowImage("Example1", img1);
cvShowImage("Example2", img);
cvWaitKey(0);
cvReleaseImage(&img1);
cvReleaseImage(&img);
cvDestroyWindow("Example1");
cvDestroyWindow("Example2");
}
(3)连续进行两次缩放处理与Canny边缘检测
#include
#include
#include
using namespace std;
IplImage* doPyrDowm(IplImage* in, int filter = CV_GAUSSIAN_5x5) // CV_GAUSSIAN_5x5在书中为IPL_GAUSSIAN_5x5,但IPL_GAUSSIAN_5x5不在库中
{
//best to make sure input image is divisible by two.
//
//assert(in->width % 2 != 0 && in->height % 2 == 0); //条件正确继续进行,错误则终止程序,根据资料此函数不常用
//若将in->width % 2 != 0改为in->width % 2 == 0,不可运行
//思考后得出:可能是因为老版本对尺寸要求比较严格,3.0版opencv无尺寸问题
IplImage* out = cvCreateImage( CvSize(in->width / 2, in->height / 2),
//大小只有输入图像1/4的图像
in->depth, //输入图像每个像素点的数据类型
in->nChannels); //输入图像的通道数
cvPyrDown(in, out);
return(out);
}
IplImage* doCanny(IplImage* in, double lowThresh, double highThresh, double aperture)
{
if (in->nChannels != 1) //确保输入图像为灰度图像
return(0);
IplImage* out = cvCreateImage( CvSize(cvGetSize(in)), //cvGetSize函数提取输入函数的图像大小
IPL_DEPTH_8U, //IPL_DEPTH_8U表示图像像素类型:无符号8位整数
1); //单通道(灰度级)
cvCanny(in, out, lowThresh, highThresh, aperture);
return(out);
}
int main()
{
IplImage* img = cvLoadImage("Example1.jpg", 0);
IplImage* img1 = doPyrDowm(img, CV_GAUSSIAN_5x5);
IplImage* img2 = doPyrDowm(img1, CV_GAUSSIAN_5x5);
IplImage* img3 = doCanny(img2, 10, 100, 3);
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
cvShowImage("Example1", img);
cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
cvShowImage("Example2", img1);
cvNamedWindow("Example3", CV_WINDOW_AUTOSIZE);
cvShowImage("Example3", img2);
cvNamedWindow("Example4", CV_WINDOW_AUTOSIZE);
cvShowImage("Example4", img3);
cvWaitKey(0);
cvReleaseImage(&img);
cvReleaseImage(&img1);
cvReleaseImage(&img2);
cvReleaseImage(&img3);
cvDestroyWindow("Example1");
cvDestroyWindow("Example2");
cvDestroyWindow("Example3");
cvDestroyWindow("Example4");
}
int main()
{
IplImage* img = cvLoadImage("Example1.jpg", 0);
IplImage* out;
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
cvShowImage("Example1", img);
cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
out = doPyrDowm(img, CV_GAUSSIAN_5x5);
out = doPyrDowm(out, CV_GAUSSIAN_5x5);
out = doCanny(out, 10, 100, 3);
cvShowImage("Example2", out); //直接显示最后效果
cvWaitKey(0);
cvReleaseImage(&img);
cvReleaseImage(&out);
cvDestroyWindow("Example1");
cvDestroyWindow("Example2");
}
说明:虽然内存垃圾处理在Opencv中很重要,但我们只需要释放自己的显式分配的内存空间。