什么是灰度图:
百度百科
什么是灰度图
#include <opencv2/opencv.hpp>
#include <iostream>using namespace cv;
using namespace std;int main(int argc, char** argv)
{Mat src = Mat(4,4,CV_8UC3,Scalar(28,128,228));cout << src << endl;namedWindow("input", WINDOW_AUTOSIZE);imshow("input", src);//先保存再转成灰度图imwrite("D:/OpenCV/save/testOne1.png", src); Mat op = imread("D:/OpenCV/save/testOne1.png", IMREAD_GRAYSCALE);cout << "先保存再转灰度图:\n"<< op << endl;//直接转成灰度图Mat gray;cvtColor(src, gray, COLOR_BGR2GRAY);cout << "直接转灰度图:\n" << gray << endl;cout << "value:" << 228 * 0.3 + 128 * 0.59 + 28 * 0.11;waitKey(0);return 0;
}
可以看出OpenCV中,彩色图转灰度图用的是浮点法,即:Gray=R0.3+G0.59+B*0.11