溫馨提示×

溫馨提示×

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

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

python如何使用numpy實(shí)現(xiàn)直方圖反向投影

發(fā)布時間:2021-03-30 09:35:22 來源:億速云 閱讀:213 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)python如何使用numpy實(shí)現(xiàn)直方圖反向投影,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

自己加上了后(也不知到這樣加對不對)代碼和效果如下:

代碼:
import cv2
import numpy as np
roi = cv2.imread('./data/rose_red.jpg')
hsv = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)
#target is the image we search in
target = cv2.imread('./data/rose.jpg')
cv2.imshow('target',target)
hsvt = cv2.cvtColor(target,cv2.COLOR_BGR2HSV)
# Find the histograms using calcHist. Can be done with np.histogram2d also
M = cv2.calcHist([hsv],[0, 1], None, [180, 256], [0, 180, 0, 256] )
print(M)
I = cv2.calcHist([hsvt],[0, 1], None, [180, 256], [0, 180, 0, 256] )
h,s,v = cv2.split(hsvt)
#斜體是自己加上的
R=M/I
print(R.shape)
B = R[h.ravel(),s.ravel()]
print(B)
B = np.minimum(B,1)
print(B)
B = B.reshape(hsvt.shape[:2])
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(9,9))
B=cv2.filter2D(B,-1,disc)
B = np.uint8(B)
cv2.normalize(B,B,0,255,cv2.NORM_MINMAX)
cv2.imshow('B',B)
ret,thresh = cv2.threshold(B,2,255,0)
cv2.imshow('thresh',thresh)
res = cv2.bitwise_and(target,target,mask=thresh)
cv2.imshow('res',res)
cv2.waitKey(0)

效果:

rose_red.jpg

python如何使用numpy實(shí)現(xiàn)直方圖反向投影

rose.jpg

python如何使用numpy實(shí)現(xiàn)直方圖反向投影

result:

python如何使用numpy實(shí)現(xiàn)直方圖反向投影

關(guān)于“python如何使用numpy實(shí)現(xiàn)直方圖反向投影”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

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

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

AI