学习opencv之图像的创建,复制,保存

IplImage* cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR )

第二个参数的具体意思为:

– >0 Return a 3-channel color image
– =0 Return a grayscale image
– <0 Return the loaded image as is. Note that in the current implementation the alpha
channel, if any, is stripped from the output image. For example, a 4-channel RGBA
image is loaded as RGB if flags  0 .

所以在此代码中加入了先把rgb图转化成灰度图在继续复制和保存。

代码如下:

// 1.1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"opencv243.h"

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	IplImage* pImg; //声明IplImage指针
	pImg = cvLoadImage( "C:\\Users\\sony\\Desktop\\Lena.jpg", 0);
	//载入图像,强制转化为Gray
	if( !pImg==0 )
	{

		IplImage* pImg2 = cvCreateImage(cvGetSize(pImg),pImg->depth,pImg->nChannels);
		cvCopy(pImg, pImg2, NULL);

		cvSaveImage("C:\\Users\\sony\\Desktop\\lena_new.jpg", pImg2);//把图像写入文件

		cvNamedWindow( "Image", 1 );//创建窗口
		cvShowImage( "Image", pImg );//显示图像

		cvWaitKey(0); //等待按键

		cvDestroyWindow( "Image" );//销毁窗口
		cvReleaseImage( &pImg ); //释放图像
		cvReleaseImage( &pImg2 ); //释放图像
		return 0;
	}

	return -1;

}


你可能感兴趣的:(学习opencv之图像的创建,复制,保存)