千家信息网

C++怎么实现直方图归一化

发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,本篇内容主要讲解"C++怎么实现直方图归一化",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C++怎么实现直方图归一化"吧!图像处理100问,这个项目切切实
千家信息网最后更新 2025年02月04日C++怎么实现直方图归一化

本篇内容主要讲解"C++怎么实现直方图归一化",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C++怎么实现直方图归一化"吧!


图像处理100问,这个项目切切实实的包含了100个各种直击你薄弱底子的问题,看完可以帮你完善很多的知识漏洞和误区。

直接看看目录吧:

截取了三张,应该能看出他覆盖的还是很全面的了叭。附带python和c++两套代码,可以根据自己条件选择。

来随便找一个问题看看:

问题简单直接,还附带一点点知识点介绍,该项目作者本人奉行的是手写代码实现,而不是简单的调用一句opencv的API,可以看看这道题的答案:

C++版:

#include #include #include #include // histogram normalizationcv::Mat histogram_normalization(cv::Mat img, int a, int b){  // get height and width  int width = img.cols;  int height = img.rows;  int channel = img.channels();
int c, d; int val;
// prepare output cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);
// get [c, d] for (int y = 0; y < height; y++){ for (int x = 0; x < width; x++){ for (int _c = 0; _c < channel; _c++){ val = (float)img.at(y, x)[_c]; c = fmin(c, val); d = fmax(d, val); } } }
// histogram transformation for (int y = 0; y < height; y++){ for ( int x = 0; x < width; x++){ for ( int _c = 0; _c < 3; _c++){ val = img.at(y, x)[_c];
if (val < a){ out.at(y, x)[_c] = (uchar)a; } else if (val <= b){ out.at(y, x)[_c] = (uchar)((b - a) / (d - c) * (val - c) + a); } else { out.at(y, x)[_c] = (uchar)b; } } } }
return out;}
int main(int argc, const char* argv[]){ // read image cv::Mat img = cv::imread("imori_dark.jpg", cv::IMREAD_COLOR);
// histogram normalization cv::Mat out = histogram_normalization(img, 0, 255);
//cv::imwrite("out.jpg", out); cv::imshow("answer", out); cv::waitKey(0); cv::destroyAllWindows();
return 0;}

python版本:

import cv2import numpy as npimport matplotlib.pyplot as plt
# histogram normalizationdef hist_normalization(img, a=0, b=255): # get max and min c = img.min() d = img.max()
out = img.copy()
# normalization out = (b-a) / (d - c) * (out - c) + a out[out < a] = a out[out > b] = b out = out.astype(np.uint8)
return out
# Read imageimg = cv2.imread("imori_dark.jpg").astype(np.float)H, W, C = img.shape
# histogram normalizationout = hist_normalization(img)
# Display histogramplt.hist(out.ravel(), bins=255, rwidth=0.8, range=(0, 255))plt.savefig("out_his.png")plt.show()
# Save resultcv2.imshow("result", out)cv2.waitKey(0)cv2.imwrite("out.jpg", out)

到此,相信大家对"C++怎么实现直方图归一化"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0