灰度化(Grayscale)
生活随笔
收集整理的這篇文章主要介紹了
灰度化(Grayscale)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Y=0.2126R+0.7152G+0.0722BY = 0.2126\ R + 0.7152\ G + 0.0722\ B Y=0.2126?R+0.7152?G+0.0722?B
//=============================================================== //FileName: // 灰度化.cpp //Date: // 2019/12/6 //Author: // khoing(https://blog.csdn.net/qq_45391763) //===============================================================#include<iostream> #include <opencv2/opencv.hpp>// BGR -> Gray cv::Mat BGR2GRAY(cv::Mat img) {// prepare outputcv::Mat out = cv::Mat::zeros(img.rows, img.cols, CV_8UC1);//----------------------------------------------------------//通過以下公式得到灰度圖像單通道的像素值//Y = 0.2126R + 0.7152G + 0.0722Bfor (int row = 0; row < img.rows; row++) {for (int col = 0; col < img.cols; col++) {// BGR -> Grayout.at<uchar>(row, col) = 0.2126 * (float)img.at<cv::Vec3b>(row, col)[2] \+ 0.7152 * (float)img.at<cv::Vec3b>(row, col)[1] \+ 0.0722 * (float)img.at<cv::Vec3b>(row, col)[0];}}//----------------------------------------------------------return out; }int main() {// read imagecv::Mat img = cv::imread("imgs/imori.jpg", cv::IMREAD_COLOR);//----------------------------------------------------------// BGR -> Graycv::Mat out = BGR2GRAY(img);//----------------------------------------------------------cv::imshow("img", img);cv::imshow("sample", out);//----------------------------------------------------------cv::waitKey(0);cv::destroyAllWindows();return 0;}總結
以上是生活随笔為你收集整理的灰度化(Grayscale)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 会声会影如何调整素材的运行速度?
- 下一篇: filter: grayscale(1)