opecv第一階段問題
- opencv學習第二彈
- 問題
- 代碼
- 結果圖
opencv學習第二彈
問題
將影像灰度化吧!
灰度是一種影像亮度的表示方法,通過下式計算:
Y=0.2126R+0.7152G+0.0722*B
代碼
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
// BGR -> Gray
cv::Mat BGR2GRAY(cv::Mat img){
// get height and width
int width = img.cols;
int height = img.rows;
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
// each y, x
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// BGR -> Gray
out.at<uchar>(y, x) = 0.2126 * (float)img.at<cv::Vec3b>(y, x)[2] \
+ 0.7152 * (float)img.at<cv::Vec3b>(y, x)[1] \
+ 0.0722 * (float)img.at<cv::Vec3b>(y, x)[0];
}
}
return out;
}
int main(int argc, const char* argv[]){
// read image
cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR);
// BGR -> Gray
cv::Mat out = BGR2GRAY(img);
//cv::imwrite("out.jpg", out);
cv::imshow("sample", out);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}
結果圖


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/299260.html
標籤:其他
