项目功能实现:对灰度图片进行直方图均衡化操作
按照之前的博文结构来,这里就不在赘述了
一、头文件
histogram_equal.h
#pragma once
#include<opencv2/opencv.hpp>
using namespace cv;
class HISTOGRAM_EQUAL {
public:
void histogram_equal(Mat& image);
};
#pragma once
二、函数实现
histogram_equal.cpp
cvtColor(image, gray, COLOR_BGR2GRAY);
转灰度图
equalizeHist(gray, result);
对灰度图进行直方图均衡化
直方图均衡化函数equalizeHist
只支持对灰度图像的直方图均衡化
#include"histogram_equal.h"
#include<iostream>
#include<opencv2/opencv.hpp>
void HISTOGRAM_EQUAL::histogram_equal(Mat& image) {
Mat gray, result;
cvtColor(image, gray, COLOR_BGR2GRAY);
imshow("gray", gray);
equalizeHist(gray, result);
imshow("histogram_equal", result);
}
三、主函数
yy_main.cpp
#include <opencv2/opencv.hpp>
#include <iostream>
#include"histogram_equal.h"
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src = cv::imread("E:/C++_workspace/beyond.jpg", IMREAD_COLOR);
if (src.empty()) {
printf("load image is false...\n");
return -1;
}
namedWindow("yanyu", WINDOW_FREERATIO);
imshow("yanyu", src);
HISTOGRAM_EQUAL yy;
yy.histogram_equal(src);
waitKey(0);
destroyAllWindows();
return 0;
}
项目结构如下:
运行结果如下:
四、课后作业
项目功能实现:对彩色图片进行直方图均衡化操作
思路:直方图均衡化本质是调整亮度,在HSV色域中对应的V通道
①把图像转到HSV空间中
②对图片的HSV进行分解通道,对V通道进行直方图均衡化
③合并HSV空间的图片
④转换到RGB色彩空间
histogram_equal.cpp
①把图像转到HSV空间中
cvtColor(image, hsv, COLOR_BGR2HSV);
②对图片的HSV进行分解通道,对V通道进行直方图均衡化
split(hsv, hsv_channels);
equalizeHist(result_hsv, hsv_channels[2]);
分离之后的通道,HSV中V为hsv_channels[2]
③合并HSV空间的图片
merge(hsv_channels,result_hsv);
④转换到RGB色彩空间
cvtColor(result_hsv, result_bgr, COLOR_HSV2BGR);
#include"histogram_equal.h"
#include<iostream>
#include<opencv2/opencv.hpp>
void HISTOGRAM_EQUAL::histogram_equal(Mat& image) {
Mat hsv, result_hsv, result_bgr;
std::vector<Mat>hsv_channels;
cvtColor(image, hsv, COLOR_BGR2HSV);
split(hsv, hsv_channels);
equalizeHist(result_hsv, hsv_channels[2]);
merge(hsv_channels,result_hsv);
cvtColor(result_hsv, result_bgr, COLOR_HSV2BGR);
imshow("hsv", result_hsv);
imshow("rgb", result_bgr);
}
效果图如下: