OpenCV C++ 07 - Histogram Equalization of a Color image with OpenCV
生活随笔
收集整理的這篇文章主要介紹了
OpenCV C++ 07 - Histogram Equalization of a Color image with OpenCV
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Code
/* 作者:鄭大峰 時間:2019年09月20日 環(huán)境:OpenCV 4.1.1 + VS2017 內(nèi)容:Histogram Equalization of a Color image with OpenCV */#include "pch.h" #include <iostream> #include <opencv2/opencv.hpp>using namespace std; using namespace cv;int main() {Mat image = imread("15484.jpg");if (image.empty()){cout << "Could not open or find the image" << endl;cin.get();return -1;}//Convert the image from BGR to YCrCb color spaceMat hist_equalized_image;cvtColor(image, hist_equalized_image, COLOR_BGR2YCrCb);//Split the image into 3 channels; Y, Cr and Cb channels respectively and store it in a std::vectorvector<Mat> vec_channels;split(hist_equalized_image, vec_channels);//Equalize the histogram of only the Y channel equalizeHist(vec_channels[0], vec_channels[0]);//Merge 3 channels in the vector to form the color image in YCrCB color space.merge(vec_channels, hist_equalized_image);//Convert the histogram equalized image from YCrCb to BGR color space againcvtColor(hist_equalized_image, hist_equalized_image, COLOR_YCrCb2BGR);//Define the names of windowsString windowNameOfOriginalImage = "Original Image";String windowNameOfHistogramEqualized = "Histogram Equalized Color Image";// Create windows with the above namesnamedWindow(windowNameOfOriginalImage, WINDOW_NORMAL);namedWindow(windowNameOfHistogramEqualized, WINDOW_NORMAL);// Show images inside the created windows.imshow(windowNameOfOriginalImage, image);imshow(windowNameOfHistogramEqualized, hist_equalized_image);waitKey(0); // Wait for any key strokedestroyAllWindows(); //destroy all open windowsreturn 0; }Result
從這個圖片中,我們可以發(fā)現(xiàn),原始圖像在經(jīng)過直方圖均衡化之后,左側(cè)偏暗的部分,稍稍變亮了,右側(cè)偏亮的部分,稍稍變暗了。這就是直方圖均衡化的作用了。
Explanation
//Convert the image from BGR to YCrCb color space Mat hist_equalized_image; cvtColor(image, hist_equalized_image, COLOR_BGR2YCrCb);我們加載的圖像是 BGR 顏色空間的,因為在該顏色空間下三個通道(藍、綠、紅)都存儲了顏色信息,所以我們不能對他們進行直方圖均衡化,否則會改變顏色。我們可以將圖片的顏色空間改成 YCrCb ,在這個顏色空間下,Y 通道只存儲灰度信息,而 Cr 和 Cb 通道存儲顏色信息,所以我們可以僅對 Y 通道進行直方圖均衡化,再轉(zhuǎn)換成 BGR 顏色空間。
轉(zhuǎn)載于:https://www.cnblogs.com/zdfffg/p/11558430.html
總結(jié)
以上是生活随笔為你收集整理的OpenCV C++ 07 - Histogram Equalization of a Color image with OpenCV的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV C++ 06 - His
- 下一篇: html select 可输入 可编辑