ภาพรวม
Histogram ของภาพแสดงความถี่ของค่าความเข้ม (Intensity) ของพิกเซล
Histogram Equalization ปรับปรุง Contrast โดยกระจายค่าความเข้มให้กระจายตัวทั่วช่วง Dynamic Range
Syntax
equalizeHist(src, dst)
- src: ภาพ Input (ต้องเป็น Grayscale / Single Channel)
- dst: ภาพ Output (ขนาดและ Type เหมือน src)
การใช้กับภาพสี (Color Image)
ต้องแปลงไป Color Space ที่แยก Luminance (เช่น YCrCb, Lab) → Equalize แค่ Channel Luminance → แปลงกลับ BGR
โค้ดตัวอย่าง C++ (Color Image Equalization)
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, const char** argv) {
Mat original = imread("sky.jpg");
vector<Mat> channels;
Mat hist;
// BGR → YCrCb (Y = Luminance)
cvtColor(original, hist, COLOR_BGR2YCrCb);
split(hist, channels); // แยก 3 Channels
// Equalize เฉพาะ Channel Y (index 0 ใน YCrCb แต่โค้ดต้นฉบับใช้ index 2 - อาจเป็น Lab)
equalizeHist(channels[2], channels[2]); // ปรับตาม Color Space
Mat histeql;
merge(channels, hist);
cvtColor(hist, histeql, COLOR_YCrCb2BGR);
namedWindow("original"); imshow("original", original);
namedWindow("hist"); imshow("hist", histeql);
waitKey(0);
return 0;
}
ผลลัพธ์
ภาพ "hist" มี Contrast ดีกว่า "original" รายละเอียดในเงาและไฮไลท์ชัดเจนขึ้น
