opencv 三通道图像转为单通道图像

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

#include "stdafx.h"
#include 
#include 

using namespace cv;
using namespace std;

int main()
{
	string path = "D:\\temp\\label\\image3.png";

	Mat src = imread(path);

	Mat src8bit = Mat::zeros(Size(src.cols, src.rows), CV_8UC1);

	float R, G, B;
	R = 0.299;
	G = 0.387;
	B = 0.114;
	//R = 0.109;
	//G = 0.387;
	//B = 0.340;
	for (int i = 0; i < src.rows; i++)
	{
		uchar* data = src.ptr(i);
		for (int j = 0; j < src.cols * 3; j = j + 3)
		{
			int r, g, b, val;
			r = R * data[j];
			g = G * data[j + 1];
			b = B * data[j + 2];
			val = r + g + b;
			src8bit.at(i, j / 3) = val;
		}
	}

    return 0;
}


你可能感兴趣的:(C++,OpenCV,opencv,计算机视觉,c++)