本文的的目标就是实现对图片的侵蚀、膨胀和反转。
具体的代码如下:
#include "stdafx.h"//由于是新建的空工程,所以需要新建stdafx.h和targetver.h两个头文件,不然会出现找不到头文件的问题
#include "opencv/cv.h"
#include "opencv/highgui.h"
int main()
{
//display the original image
IplImage* img = cvLoadImage("pic.jpg");
IplImage* imgErode = cvLoadImage("pic.jpg");
IplImage* imgDilate = cvLoadImage("pic.jpg");
IplImage* imgInvert = cvLoadImage("pic.jpg");
cvNamedWindow("MyWindow");
cvShowImage("MyWindow", img);
// 侵蚀
cvErode(img, imgErode, 0, 10);
cvNamedWindow("Eroded");
cvShowImage("Eroded", imgErode);
//膨胀
cvDilate(img, imgDilate, 0, 3);
cvNamedWindow("Dilated");
cvShowImage("Dilated", imgDilate);
// 反转
cvNot(img, imgInvert);
cvNamedWindow("Inverted");
cvShowImage("Inverted", imgInvert);
cvWaitKey(0);
//cleaning up
cvDestroyWindow("MyWindow");
cvDestroyWindow("Eroded");
cvReleaseImage(&img);
return 0;
}
1.需要把pic.jpg拷贝到工程目录同cpp文件的文件夹下。
2.需要定义stdafx.h和targetver.h头文件。
//stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
//targetver.h
#pragma once
// Including SDKDDKVer.h defines the highest available Windows platform.
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
#include <SDKDDKVer.h>
3.直接运行会碰到fopen方法不安全(error:C4996)的问题。
问题提示见下图:
解决办法:加上#pragma warning(disble:4996)这句话
4.运行效果如下: