利用OpenCV和C++实现由RGB图像转化为灰度图,再将灰度图转化为二值图的程序

#include
#include
#include
#include

int main()
{
	//【1】加载原图
	IplImage* srcImg = cvLoadImage(".\\srcImg\\No.08.jpg");   
	cvNamedWindow("【srcImg】");
	cvShowImage("【srcImg】",srcImg);

	CvSize grayImgSize;
	grayImgSize.height =  srcImg->height;
	grayImgSize.width  =  srcImg->width;
	//【2】转换为灰度图
	IplImage* grayImg = cvCreateImage(grayImgSize,srcImg->depth,1);
	cvCvtColor(srcImg,grayImg,CV_BGR2GRAY);
	cvNamedWindow("【grayImg】");
	cvShowImage("【grayImg】",grayImg);
	//【3】转换为二值图
	IplImage* binaryImg = cvCreateImage(grayImgSize,grayImg->depth,1);
	cvThreshold(grayImg,binaryImg,145,255,CV_THRESH_BINARY);
	cvNamedWindow("【binaryImg】");
	cvShowImage("【binaryImg】",binaryImg);
	

	cvSaveImage(".\\srcImg\\binaryImg.jpg",binaryImg);  //保存二值图
	cv::waitKey(0);
	//【4】释放内存空间
	cvDestroyWindow("【srcImg】");
	cvDestroyWindow("【grayImg】");
	cvDestroyWindow("【binaryImg】");
	cvReleaseImage(&srcImg);
	cvReleaseImage(&grayImg);
	cvReleaseImage(&binaryImg);
	return 0;
}

你可能感兴趣的:(OpenCV笔记,C++程序设计学习笔记)