Python OpenCV處理圖像之圖像直方圖和反向投影
本文實(shí)例為大家分享了Python OpenCV圖像直方圖和反向投影的具體代碼,供大家參考,具體內(nèi)容如下
當(dāng)我們想比較兩張圖片相似度的時(shí)候,可以使用這一節(jié)提到的技術(shù)
關(guān)于這兩種技術(shù)的原理可以參考我上面貼的鏈接,下面是示例的代碼:
0x01. 繪制直方圖
import cv2.cv as cv
def drawGraph(ar,im, size): #Draw the histogram on the image
minV, maxV, minloc, maxloc = cv.MinMaxLoc(ar) #Get the min and max value
hpt = 0.9 * histsize
for i in range(size):
intensity = ar[i] * hpt / maxV #Calculate the intensity to make enter in the image
cv.Line(im, (i,size), (i,int(size-intensity)),cv.Scalar(255,255,255)) #Draw the line
i += 1
#---- Gray image
orig = cv.LoadImage("img/lena.jpg", cv.CV_8U)
histsize = 256 #Because we are working on grayscale pictures which values within 0-255
hist = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0,histsize]], 1)
cv.CalcHist([orig], hist) #Calculate histogram for the given grayscale picture
histImg = cv.CreateMat(histsize, histsize, cv.CV_8U) #Image that will contain the graph of the repartition of values
drawGraph(hist.bins, histImg, histsize)
cv.ShowImage("Original Image", orig)
cv.ShowImage("Original Histogram", histImg)
#---------------------
#---- Equalized image
imEq = cv.CloneImage(orig)
cv.EqualizeHist(imEq, imEq) #Equlize the original image
histEq = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0,histsize]], 1)
cv.CalcHist([imEq], histEq) #Calculate histogram for the given grayscale picture
eqImg = cv.CreateMat(histsize, histsize, cv.CV_8U) #Image that will contain the graph of the repartition of values
drawGraph(histEq.bins, eqImg, histsize)
cv.ShowImage("Image Equalized", imEq)
cv.ShowImage("Equalized HIstogram", eqImg)
#--------------------------------
cv.WaitKey(0)
0x02. 反向投影
import cv2.cv as cv
im = cv.LoadImage("img/lena.jpg", cv.CV_8U)
cv.SetImageROI(im, (1, 1,30,30))
histsize = 256 #Because we are working on grayscale pictures
hist = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0,histsize]], 1)
cv.CalcHist([im], hist)
cv.NormalizeHist(hist,1) # The factor rescale values by multiplying values by the factor
_,max_value,_,_ = cv.GetMinMaxHistValue(hist)
if max_value == 0:
max_value = 1.0
cv.NormalizeHist(hist,256/max_value)
cv.ResetImageROI(im)
res = cv.CreateMat(im.height, im.width, cv.CV_8U)
cv.CalcBackProject([im], res, hist)
cv.Rectangle(im, (1,1), (30,30), (0,0,255), 2, cv.CV_FILLED)
cv.ShowImage("Original Image", im)
cv.ShowImage("BackProjected", res)
cv.WaitKey(0)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)對(duì)象轉(zhuǎn)換為xml的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)對(duì)象轉(zhuǎn)換為xml的方法,結(jié)合實(shí)例形式分析了Python對(duì)象屬性、節(jié)點(diǎn)的操作及與xml相互轉(zhuǎn)換的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06
Python讀取mat文件,并轉(zhuǎn)為csv文件的實(shí)例
今天小編就為大家分享一篇Python讀取mat文件,并轉(zhuǎn)為csv文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
python的程序分支結(jié)構(gòu)用法及說(shuō)明
這篇文章主要介紹了python的程序分支結(jié)構(gòu)用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Python實(shí)現(xiàn)計(jì)算AUC的三種方式總結(jié)
AUC(Area?under?curve)是機(jī)器學(xué)習(xí)常用的二分類評(píng)測(cè)手段,直接含義是ROC曲線下的面積。本文總結(jié)了Python語(yǔ)言實(shí)現(xiàn)計(jì)算AUC的三種方式,感興趣的可以學(xué)習(xí)一下2022-07-07
如何基于Python實(shí)現(xiàn)word文檔重新排版
這篇文章主要介紹了如何基于Python實(shí)現(xiàn)word文檔重新排版,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
python對(duì)驗(yàn)證碼降噪的實(shí)現(xiàn)示例代碼
這篇文章主要介紹了python對(duì)驗(yàn)證碼降噪的實(shí)現(xiàn)示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

