#include"opencv.hpp"
void my_LUT(const cv::Mat &src, cv::Mat &dst, double low = -0.02, double high = 0.02);
void my_hist(const cv::Mat& src, cv::Mat &hist_dst);
int main() {
cv::Mat img = cv::imread(R"(D:\LJ\WXWork\1688856871246917\Cache\File\2022-09\up1first.tif)", cv::IMREAD_UNCHANGED);
//img *= 100;
double low = -0.012;
double high = 0.012;
cv::Rect rect(cv::Point(153, 153), cv::Point(345 + 1, 344 + 1));
cv::Mat img2 = img(rect).clone();
cv::Mat lutdst;
my_LUT(img2, lutdst, low, high);
cv::Mat histdst;
my_hist(lutdst, histdst);
cv::waitKey(0);
return 0;
}
void my_LUT(const cv::Mat &src, cv::Mat &dst, double low, double high) {
//low = -0.012;
//high = 0.012;
double grade = 255 / (high - low);
dst = cv::Mat::zeros(src.size(), CV_8UC1);
for (int i = 0; i < dst.rows; i++) {
for (int j = 0; j < dst.cols; j++) {
float a = src.at<float>(i, j);
if (a < low) {
a = low;
}
else if (a > high) {
a = high;
}
int grayval = (a - low)*grade;
dst.at<uchar>(i, j) = grayval;
}
}
return;
}
void my_hist(const cv::Mat& src,cv::Mat &hist_dst) {
cv::Mat b = src.clone();
cv::Mat hist_b;
float range[] = { 0,256 };
int histsize = 256;
const float*ranges = { range };
calcHist(&b, 1, 0, cv::Mat(), hist_b, 1, &histsize, &ranges);
double maxval = 0;
hist_b *= 3;
cv::minMaxLoc(hist_b, NULL, &maxval, NULL, NULL);
int height = maxval, width = 256 * 5 + 1;
hist_dst = cv::Mat(height, width, CV_8UC3, cv::Scalar(0, 0, 0));
//normalize(hist_b, hist_b, 0, histImg_b.rows, cv::NORM_MINMAX, -1, cv::Mat());
int bin_w = 5;// cvRound((double)width / height);
for (int i = 1; i < hist_b.rows; i++) {
//Point q((i - 1)*bin_w, height - cvRound(hist_b.at<float>(i - 1, 0)));
//Point h((i)*bin_w, height - cvRound(hist_b.at<float>(i,0)));
//line(histImg, q, h, Scalar(255, 0, 0));
//画线
line(hist_dst, cv::Point((i - 1)*bin_w, height - cvRound(hist_b.at<float>(i - 1, 0))),
cv::Point((i)*bin_w, height - cvRound(hist_b.at<float>(i, 0))),
cv::Scalar(255, 0, 0), bin_w);
}
}
我的直方图和LUT
最新推荐文章于 2024-10-31 19:01:46 发布
本文介绍了一个使用OpenCV进行图像处理的示例程序,包括通过查找指定范围内的灰度值来实现图像的查找表(LUT)转换,并展示了如何从处理后的图像中计算并绘制直方图。该程序读取特定路径下的图像文件,应用了灰度级拉伸以增强对比度,并通过自定义函数计算和显示图像的灰度直方图。
摘要由CSDN通过智能技术生成