当前位置: 移动技术网 > IT编程>脚本编程>Python > python使用numpy实现直方图反向投影示例

python使用numpy实现直方图反向投影示例

2020年03月09日  | 移动技术网IT编程  | 我要评论

万宁官网,英语速成法,新郑龙湖镇邮编

最近跟着opencv2-python-tutorials在学习python_opencv中直方图的反向投影时,第一种方法是使用numpy实现将图中的红色玫瑰分割出来,教程给的代码缺了一句函数,导致实现不出来。

自己加上了后(也不知到这样加对不对)代码和效果如下:

代码:
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

rose.jpg

result:

以上这篇python使用numpy实现直方图反向投影示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网