欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【03】图像灰度化(opencv4.2.0+VS2019)

程序员文章站 2022-05-29 09:22:59
...
/*图像灰度化*/

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

Mat src, gray;

int main()
{
	src = imread("E:\\Image_Processing\\pics\\1.jpg");

	if (src.empty())
	{
		cout << "没有读取到图像" << endl;
		return -1;
	}

	namedWindow("源图像", WINDOW_AUTOSIZE);
	imshow("源图像", src);

	//cvtColor(src, gray, COLOR_RGB2GRAY);

	int row = src.rows;
	int col = src.cols;

	gray.create(row, col, CV_8UC1);

	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			gray.at<uchar>(i,j)= src.at<Vec3b>(i, j)[0] * 0.144 + src.at<Vec3b>(i, j)[1] * 0.587 + src.at<Vec3b>(i, j)[2] * 0.299;
		}
	}

	namedWindow("灰度图", WINDOW_AUTOSIZE);
	imshow("灰度图", gray);

	waitKey(0);
	return 0;
}
相关标签: 机器视觉