opencv多通道图像转单通道

废话不说,直接上代码

至于三通道的比例参数,那是因为在此情况下,视觉效果最好

	IplImage *g = cvLoadImage("C:\\Users\\Administrator\\Desktop\\21.jpg");
	IplImage* src = cvCreateImage(cvGetSize(g), IPL_DEPTH_8U, 1);

	//转化为单通道黑白照片
	CvScalar pixel1;
	double temp;
	for (int i = 0; i < g->height - 1; ++i)
	{
		for (int j = 0; j < g->width - 1; ++j)
		{
			pixel1 = cvGet2D(g, i, j);
			temp = 0.11*pixel1.val[0] + 0.59*pixel1.val[1] + 0.30*pixel1.val[2];
			cvSet2D(src, i, j, temp);
		}
	}
	cvNamedWindow("image1", 1);
	cvShowImage("image1", g);
	cvNamedWindow("image", 1);
	cvShowImage("image", src);
	cvWaitKey(0);
	cvDestroyWindow("image");
	cvDestroyWindow("image1");
	cvReleaseImage(&src);
	cvReleaseImage(&g);

 原图如下: 
  

opencv多通道图像转单通道_第1张图片

效果图如下:

opencv多通道图像转单通道_第2张图片

你可能感兴趣的:(opencv,通道转换)