Python OpenCV處理圖像之圖像直方圖和反向投影
更新時間:2018年07月10日 17:13:04 作者:Leo
這篇文章主要為大家詳細介紹了Python OpenCV處理圖像之圖像直方圖和反向投影,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Python OpenCV圖像直方圖和反向投影的具體代碼,供大家參考,具體內(nèi)容如下
當我們想比較兩張圖片相似度的時候,可以使用這一節(jié)提到的技術
關于這兩種技術的原理可以參考我上面貼的鏈接,下面是示例的代碼:
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)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python實現(xiàn)基本數(shù)據(jù)結構中隊列的操作方法示例
這篇文章主要介紹了Python實現(xiàn)基本數(shù)據(jù)結構中隊列的操作方法,結合實例形式演示了Python針對數(shù)據(jù)結構中隊列的初始化、插入、刪除、判斷隊列滿及隊列空等相關操作技巧,需要的朋友可以參考下2017-12-12
Python數(shù)據(jù)分析的八種處理缺失值方法詳解
缺失值可能是數(shù)據(jù)科學中最不受歡迎的值,然而,它們總是在身邊。忽略缺失值也是不合理的,因此我們需要找到有效且適當?shù)靥幚硭鼈兊姆椒?/div> 2021-11-11
Python基于httpx模塊實現(xiàn)發(fā)送請求
這篇文章主要介紹了Python基于httpx模塊實現(xiàn)發(fā)送請求,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-07-07
分享15?個python中的?Scikit-Learn?技能
這篇文章主要介紹了分享15?個python中的?Scikit-Learn?技能,Scikit-Learn?是一個非常棒的?python?庫,用于實現(xiàn)機器學習模型和統(tǒng)計建模,有降維、特征選擇、特征提取、集成技術等特征,下文相關內(nèi)容需要的朋友可以參考一下2022-03-03最新評論

