这个技术常常用来跟踪目标,且需要转换在HSV色彩空间
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
def hist2D_demo(image):
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
hist = cv.calcHist([hsv],[0, 1], None, [180, 256], [0, 180, 0, 255])
plt.imshow(hist, interpolation='nearest')
plt.title('Histogram 2D image')
plt.show()
src = cv.imread('./data/lena.jpg', 1)
cv.imshow('souce', src)
hist2D_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
calcHist说明
hist = cv.calcHist([hsv],[0, 1], None, [32, 32], [0, 180, 0, 255])
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
def hist_projection_demo():
target = cv.imread('./data/real-madrid-vs-juventus.png',1)
sample = cv.imread('./data/violet.png',1)
sample_hsv = cv.cvtColor(sample, cv.COLOR_BGR2HSV)
target_hsv = cv.cvtColor(target, cv.COLOR_BGR2HSV)
cv.imshow('simple image', sample)
cv.imshow('target image', target)
sampleHist = cv.calcHist([sample_hsv], [0, 1], None ,[32, 48], [0, 180, 0, 256])
cv.normalize(sampleHist,sampleHist,0, 255, cv.NORM_MINMAX)
dst = cv.calcBackProject([target_hsv], [0,1], sampleHist, [0,180,0,256],1)
cv.imshow('projectionHistogram image', dst)
def hist2D_demo(image):
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
hist = cv.calcHist([hsv],[0, 1], None, [180, 256], [0, 180, 0, 255])
plt.imshow(hist, interpolation='nearest')
plt.title('Histogram 2D image')
plt.show()
hist_projection_demo()
cv.waitKey(0)
cv.destroyAllWindows()