日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OpenCV:直方图均衡

發(fā)布時間:2024/1/8 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenCV:直方图均衡 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

步驟:

第 1 步:手動均衡

第 2 步:通過使用 OpenCV 函數(shù)

什么是圖像直方圖?

它是圖像強度分布的圖形表示。它量化了所考慮的每個強度值的像素數(shù)。

第 1 步:手動均衡

%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)

輸出:

<matplotlib.image.AxesImage at 0x1d0b37d2250>

顯示彩色圖像

將圖像轉(zhuǎn)換為 numpy 數(shù)組,以便 OpenCV 可以使用:

img?=?np.asanyarray(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')

輸出:

<matplotlib.image.AxesImage at 0x1d0b415e100>

我們現(xiàn)在知道如何處理直方圖了

img.max()

輸出:

255 img.min()

輸出:

0 img.shape

輸出:

(2354,?2560)

把它展平:

flat?=?img.flatten() #?1?row?2354?x?2560?=?6.026.240flat.shape

輸出:

(6026240,)

顯示直方圖

plt.hist(flat,?bins=50)

請注意,灰度值在某個值周圍分布很差

什么是直方圖均衡?

為了更清楚,從上圖中,你可以看到像素似乎聚集在可用強度范圍的中間。直方圖均衡所做的就是擴大這個范圍。

#?formula?for?creating?the?histogram display(Math(r'P_x(j)?=?\sum_{i=0}^{j}?P_x(i)')) #?create?our?own?histogram?functiondef?get_histogram(image,?bins):#?array?with?size?of?bins,?set?to?zeroshistogram?=?np.zeros(bins)#?loop?through?pixels?and?sum?up?counts?of?pixelsfor?pixel?in?image:histogram[pixel]?+=?1#?return?our?final?resultreturn?histogram hist?=?get_histogram(flat,?256) plt.plot(hist) [<matplotlib.lines.Line2D?at?0x1d0b4e4da90>] #?create?our?cumulative?sum?function def?cumsum(a):a?=?iter(a)b?=?[next(a)]for?i?in?a:b.append(b[-1]?+?i)return?np.array(b)#?execute?the?fn cs?=?cumsum(hist)#?display?the?result plt.plot(cs) [<matplotlib.lines.Line2D?at?0x1d0b4eafb20>] #?formula?to?calculate?cumulation?sumdisplay(Math(r's_k?=?\sum_{j=0}^{k}?{\frac{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) [<matplotlib.lines.Line2D?at?0x1d0b4f0b7c0>]

Casting:

#?cast?it?back?to?uint8?since?we?can't?use?floating?point?values?in?images cs?=?cs.astype('uint8') plt.plot(cs)

輸出:

[<matplotlib.lines.Line2D?at?0x1d0b5f8bd60>]

獲取 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)

它是如何工作的?

均衡意味著將一個分布(給定的直方圖)映射到另一個分布(強度值的更廣泛和更均勻的分布),因此強度值分布在整個范圍內(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.add_subplot(1,2,1) plt.imshow(img,?cmap='gray')#?display?the?new?image fig.add_subplot(1,2,2) plt.imshow(img_new,?cmap='gray') plt.show(block=True)

使用 OpenCV equalizeHist(img) 方法

第 2 步:通過使用 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.add_subplot(1,2,1) plt.imshow(img,?cmap='gray')#?display?the?Equalized?(equ)?image fig.add_subplot(1,2,2) plt.imshow(equ,?cmap='gray') plt.show(block=True) print("That′s?it!?Thank?you?once?again!\nI?hope?will?be?helpful.")

輸出:

That′s?it!?Thank?you?once?again! I?hope?will?be?helpful.

👉木星筆記本鏈接:https://drive.google.com/file/d/1xWwLLGZF1XRA9ua97upcCXOssi1Z-s7k/view?usp=sharing

👉 Github:https://github.com/giljr/pyImage

參考:

  • https://www.udemy.com/course/python-for-computer-vision-with-opencv-and-deep-learning/

  • https://en.wikipedia.org/wiki/Haar-like_feature

☆ END ☆

如果看到這里,說明你喜歡這篇文章,請轉(zhuǎn)發(fā)、點贊。微信搜索「uncle_pn」,歡迎添加小編微信「 woshicver」,每日朋友圈更新一篇高質(zhì)量博文。

掃描二維碼添加小編↓

總結(jié)

以上是生活随笔為你收集整理的OpenCV:直方图均衡的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。