Related Posts
Thailand Excellence Community
ฮิสโตแกรม
// example : histogram plot
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
//Mat src = imread("histogram.png", IMREAD_COLOR);
Mat src = imread("Lena.png", IMREAD_COLOR);
//Mat src = imread("rgb.png", IMREAD_COLOR);
if (src.empty())
{
return EXIT_FAILURE;
}
vector<Mat> bgr_planes;
split(src, bgr_planes);
int histSize = 256;
float range[] = { 0, 256 }; //the upper boundary is exclusive
const float* histRange = { range };
bool uniform = true, accumulate = false;
Mat b_hist, g_hist, r_hist;
calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate);
int hist_w = 512, hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
for (int i = 1; i < histSize; i++)
{
line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h - cvRound(b_hist.at<float>(i))),
Scalar(255, 0, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(g_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h - cvRound(g_hist.at<float>(i))),
Scalar(0, 255, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(r_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h - cvRound(r_hist.at<float>(i))),
Scalar(0, 0, 255), 2, 8, 0);
}
imshow("Source image", src);
imshow("calcHist Demo", histImage);
waitKey();
return EXIT_SUCCESS;
}
// ========================
// example : histogram equalization
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat src = imread("histogram.png", IMREAD_GRAYSCALE);
if (src.empty())
{
return EXIT_FAILURE;
}
Mat dst;
equalizeHist(src, dst);
imshow("Source image", src);
imshow("Equalized image", dst);
waitKey();
return EXIT_SUCCESS;
}
การปรับค่าความสว่าง ด้วย Log Transformation การปรับค่า เช่นตัดหมอก
การปรับค่าให้เท่ากัน
example : logarithmic transformation
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
//Mat input_image = imread("lena.png", IMREAD_GRAYSCALE);
Mat input_image = imread("histogram.png", IMREAD_GRAYSCALE);
Mat processed_image;
input_image.convertTo(processed_image, CV_32F);
processed_image = processed_image + 1;
log(processed_image, processed_image);
normalize(processed_image, processed_image, 0, 255, NORM_MINMAX);
convertScaleAbs(processed_image, processed_image);
//applyColorMap(processed_image, processed_image, COLORMAP_RAINBOW); //COLORMAP_JET);
imshow("Input image", input_image);
imshow("Processed Image", processed_image);
waitKey(0);
return 0;
}