溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Python OpenCV處理圖像之圖像直方圖和反向投影的示例分析

發(fā)布時(shí)間:2021-06-11 15:03:29 來(lái)源:億速云 閱讀:166 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)Python OpenCV處理圖像之圖像直方圖和反向投影的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

本文實(shí)例為大家分享了Python OpenCV圖像直方圖和反向投影的具體代碼,供大家參考,具體內(nèi)容如下

當(dāng)我們想比較兩張圖片相似度的時(shí)候,可以使用這一節(jié)提到的技術(shù)

直方圖對(duì)比

反向投影

關(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)

感謝各位的閱讀!關(guān)于“Python OpenCV處理圖像之圖像直方圖和反向投影的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI