通过直方图反向投影,根据目标衣服颜色的特征来进行定位
cv2.calcHist([roi_hsv],[0,1],None,[32,48],[0,180,0,256])
其中[32,48]表示bin的个数,可以修改,当然范围越小越精确
import cv2
import numpy as np
from matplotlib import pyplot as pltdef back_projection():sample = cv2.imread(r"G:\Juptyer_workspace\study\opencv\opencv3\yg1.jpg")target = cv2.imread(r"G:\Juptyer_workspace\study\opencv\opencv3\yg.jpg")roi_hsv = cv2.cvtColor(sample,cv2.COLOR_BGR2HSV)target_hsv = cv2.cvtColor(target,cv2.COLOR_BGR2HSV)cv2.imshow("sample",sample)cv2.imshow("target",target)roiHist = cv2.calcHist([roi_hsv],[0,1],None,[32,48],[0,180,0,256])cv2.normalize(roiHist,roiHist,0,255,cv2.NORM_MINMAX)dst = cv2.calcBackProject([target_hsv],[0,1],roiHist,[0,180,0,256],1)cv2.imshow("back_projection",dst)back_projection()
cv2.waitKey(0)
cv2.destroyAllWindows()
效果图如下: