直方图反投影

2022-05-29 09:37:37 浏览数 (2)

代码语言:javascript复制
import cv2
import numpy as np
roi=cv2.imread('C:/Users/xpp/Desktop/Lena00.png')#读取感兴趣区域
hsv=cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)#读取图像
target=cv2.imread('C:/Users/xpp/Desktop/Lena.png')
hsvt=cv2.cvtColor(target, cv2.COLOR_BGR2HSV)#寻找的ROI对象
roihist=cv2.calcHist([hsv],[0,1],None,[180,256],[0,180,0,256])#计算ROI对象的直方图
cv2.normalize(roihist, roihist, 0, 255, cv2.NORM_MINMAX)#直方图均衡化并应用直方图反投影
dst=cv2.calcBackProject([hsvt],[0,1],roihist,[0,180,0,256],1)
#使用椭圆进行卷积
disc=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
cv2.filter2D(dst,-1,disc,dst)
#图像阈值化并使用按位与
ret,thresh=cv2.threshold(dst,50,255,0)
thresh=cv2.merge((thresh,thresh,thresh))
res=cv2.bitwise_and(target,thresh)
res=np.vstack((target,thresh,res))
cv2.imwrite('C:/Users/xpp/Desktop/result.png',res)

True

算法:直方图反投影是创建了一个与输入图像大小相同(但只有一个通道)的图像,其中每个像素对应于该像素属于感兴趣对象的概率。一般情况下,输出图像将使感兴趣的对象比其余部分更白。颜色直方图优于灰度直方图,因为物体的颜色比灰度强度更好地定义物体。

链接:https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_histograms/py_histogram_backprojection/py_histogram_backprojection.html#histogram-backprojection

0 人点赞