OpenCV:直方圖均衡
步驟:
第 1 步:手動(dòng)均衡
第 2 步:通過(guò)使用 OpenCV 函數(shù)
什么是圖像直方圖?
它是圖像強(qiáng)度分布的圖形表示。它量化了所考慮的每個(gè)強(qiáng)度值的像素?cái)?shù)。
第 1 步:手動(dòng)均衡
%matplotlib inline
from IPython.display import display, Math, Latex
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
img = Image.open('DATA/einstein.jpg')
plt.imshow(img)
輸出:
顯示彩色圖像
將圖像轉(zhuǎn)換為 numpy 數(shù)組,以便 OpenCV 可以使用:
img = np.a(chǎn)sanyarray(img)
img.shape
輸出:
(2354, 2560, 3)
將 RGB 轉(zhuǎn)換為灰度:
import cv2
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img.shape
輸出:
(2354, 2560)
顯示圖像:
plt.imshow(img, cmap='gray')
輸出:
我們現(xiàn)在知道如何處理直方圖了
img.max()
輸出:
255
img.min()
輸出:
0
img.shape
輸出:
(2354, 2560)
把它展平:
flat = img.flatten()
# 1 row 2354 x 2560 = 6.026.240
flat.shape
輸出:
(6026240,)
顯示直方圖
plt.hist(flat, bins=50)
請(qǐng)注意,灰度值在某個(gè)值周圍分布很差
什么是直方圖均衡?
為了更清楚,從上圖中,你可以看到像素似乎聚集在可用強(qiáng)度范圍的中間。直方圖均衡所做的就是擴(kuò)大這個(gè)范圍。
# formula for creating the histogram
display(Math(r'P_x(j) = sum_{i=0}^{j} P_x(i)'))
# create our own histogram function
def get_histogram(image, bins):
# array with size of bins, set to zeros
histogram = np.zeros(bins)
# loop through pixels and sum up counts of pixels
for pixel in image:
histogram[pixel] += 1
# return our final result
return histogram
hist = get_histogram(flat, 256)
plt.plot(hist)
[
# create our cumulative sum function
def cumsum(a):
a = iter(a)
b = [next(a)]
for i in a:
b.a(chǎn)ppend(b[-1] + i)
return np.a(chǎn)rray(b)
# execute the fn
cs = cumsum(hist)
# display the result
plt.plot(cs)
[
# formula to calculate cumulation sum
display(Math(r's_k = sum_{j=0}^{k} {rac{n_j}{N}}'))
# re-normalize cumsum values to be between 0-255
# numerator & denomenator
nj = (cs - cs.min()) * 255
N = cs.max() - cs.min()
# re-normalize the cdf
cs = nj / N
plt.plot(cs)
[
Casting:
# cast it back to uint8 since we can't use floating point values in imagescs =
cs.a(chǎn)stype('uint8')
plt.plot(cs)
輸出:
[
獲取 CDF:
# get the value from cumulative sum for every index in flat, and set that as img_new
img_new = cs[flat]
# we see a much more evenly distributed histogram
plt.hist(img_new, bins=50)
它是如何工作的?
均衡意味著將一個(gè)分布(給定的直方圖)映射到另一個(gè)分布(強(qiáng)度值的更廣泛和更均勻的分布),因此強(qiáng)度值分布在整個(gè)范圍內(nèi)。
# get the value from cumulative sum for every index in flat, and set that as img_new
img_new = cs[flat]
# we see a much more evenly distributed histogram
plt.hist(img_new, bins=50)
# put array back into original shape since we flattened it
img_new = np.reshape(img_new, img.shape)
img_new
輸出:
array([[233, 231, 228, ..., 216, 216, 215],
[233, 230, 228, ..., 215, 215, 214],
[233, 231, 229, ..., 213, 213, 212],
...,
[115, 107, 96, ..., 180, 187, 194],
[111, 103, 93, ..., 187, 189, 192],
[111, 103, 93, ..., 187, 189, 192]], dtype=uint8)
一探究竟:
# set up side-by-side image display
fig = plt.figure()
fig.set_figheight(15)
fig.set_figwidth(15)
fig.a(chǎn)dd_subplot(1,2,1)
plt.imshow(img, cmap='gray')
# display the new image
fig.a(chǎn)dd_subplot(1,2,2)
plt.imshow(img_new, cmap='gray')
plt.show(block=True)
使用 OpenCV equalizeHist(img) 方法
第 2 步:通過(guò)使用 OpenCV 函數(shù)
# Reading image via OpenCV and Equalize it right away!
img = cv2.imread('DATA/einstein.jpg',0)
equ = cv2.equalizeHist(img)
準(zhǔn)備好!這就是你需要做的!
fig = plt.figure()
fig.set_figheight(15)
fig.set_figwidth(15)
fig.a(chǎn)dd_subplot(1,2,1)
plt.imshow(img, cmap='gray')
# display the Equalized (equ) image
fig.a(chǎn)dd_subplot(1,2,2)
plt.imshow(equ, cmap='gray')
plt.show(block=True)
print("That?s it! Thank you once again!I hope will be helpful.")
輸出:
That?s it! Thank you once again!
I hope will be helpful.
原文標(biāo)題 : OpenCV:直方圖均衡

發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
最新活動(dòng)更多
-
6月20日立即下載>> 【白皮書(shū)】精準(zhǔn)測(cè)量 安全高效——福祿克光伏行業(yè)解決方案
-
7月3日立即報(bào)名>> 【在線會(huì)議】英飛凌新一代智能照明方案賦能綠色建筑與工業(yè)互聯(lián)
-
7月22-29日立即報(bào)名>> 【線下論壇】第三屆安富利汽車生態(tài)圈峰會(huì)
-
7.30-8.1火熱報(bào)名中>> 全數(shù)會(huì)2025(第六屆)機(jī)器人及智能工廠展
-
7月31日免費(fèi)預(yù)約>> OFweek 2025具身機(jī)器人動(dòng)力電池技術(shù)應(yīng)用大會(huì)
-
免費(fèi)參會(huì)立即報(bào)名>> 7月30日- 8月1日 2025全數(shù)會(huì)工業(yè)芯片與傳感儀表展
推薦專題
- 1 AI 眼鏡讓百萬(wàn) APP「集體失業(yè)」?
- 2 大廠紛紛入局,百度、阿里、字節(jié)搶奪Agent話語(yǔ)權(quán)
- 3 深度報(bào)告|中國(guó)AI產(chǎn)業(yè)正在崛起成全球力量,市場(chǎng)潛力和關(guān)鍵挑戰(zhàn)有哪些?
- 4 上海跑出80億超級(jí)獨(dú)角獸:獲上市公司戰(zhàn)投,干人形機(jī)器人
- 5 國(guó)家數(shù)據(jù)局局長(zhǎng)劉烈宏調(diào)研格創(chuàng)東智
- 6 下一代入口之戰(zhàn):大廠為何紛紛押注智能體?
- 7 百億AI芯片訂單,瘋狂傾銷中東?
- 8 Robotaxi新消息密集釋放,量產(chǎn)元年誰(shuí)在領(lǐng)跑?
- 9 格斗大賽出圈!人形機(jī)器人致命短板曝光:頭腦過(guò)于簡(jiǎn)單
- 10 “搶灘”家用機(jī)器人領(lǐng)域,聯(lián)通、海爾、美的等紛紛入局