您好,登錄后才能下訂單哦!
本篇內容主要講解“C++怎么實現直方圖歸一化”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C++怎么實現直方圖歸一化”吧!
圖像處理100問,這個項目切切實實的包含了100個各種直擊你薄弱底子的問題,看完可以幫你完善很多的知識漏洞和誤區。
直接看看目錄吧:
截取了三張,應該能看出他覆蓋的還是很全面的了叭。附帶python和c++兩套代碼,可以根據自己條件選擇。
來隨便找一個問題看看:
問題簡單直接,還附帶一點點知識點介紹,該項目作者本人奉行的是手寫代碼實現,而不是簡單的調用一句opencv的API,可以看看這道題的答案:
C++版:
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <math.h>
// histogram normalization
cv::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<cv::Vec3b>(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<cv::Vec3b>(y, x)[_c];
if (val < a){
out.at<cv::Vec3b>(y, x)[_c] = (uchar)a;
}
else if (val <= b){
out.at<cv::Vec3b>(y, x)[_c] = (uchar)((b - a) / (d - c) * (val - c) + a);
}
else {
out.at<cv::Vec3b>(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 cv2
import numpy as np
import matplotlib.pyplot as plt
# histogram normalization
def 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 image
img = cv2.imread("imori_dark.jpg").astype(np.float)
H, W, C = img.shape
# histogram normalization
out = hist_normalization(img)
# Display histogram
plt.hist(out.ravel(), bins=255, rwidth=0.8, range=(0, 255))
plt.savefig("out_his.png")
plt.show()
# Save result
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.imwrite("out.jpg", out)
到此,相信大家對“C++怎么實現直方圖歸一化”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。