Histogram backprojection on other spaces

asked 2018-05-17 12:52:43 -0500

dberga gravatar image

I have been trying the histogram backprojection with the image convolved with several kernels, being the representation as a multidimensional MxNx54 matrix.

See an example code:

supermatrix1=mytransformation(roi) #[M,N,54]
supermatrix2=mytransformation(img)
supermatrix1_split=cv2.split(supermatrix1)
channels=range(supermatrix1.shape[2]) #from 0 to 53
histSize=np.repeat(256,len(channels))
ranges=np.zeros(len(histSize)*2)
ranges[1::2]=histSize
hist = cv2.calcHist(supermatrix1_split, channels, None, histSize, ranges)
cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
dst = cv2.calcBackProject([supermatrix2],channels,hist,ranges,1)
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
cv2.filter2D(dst,-1,disc,dst)

The code crashes on the calcHist part, saying:

SystemError: <built-in function="" calchist=""> returned NULL without setting an error

if I change my transformation by using the rgb, which has 3 components, does not crash (although it is really bad on predicting the result). Using the hue and saturation from the hsv components works perfectly, namely:

supermatrix1=cv2.cvtColor(roi,cv2.COLOR_BGR2HSV) #[M,N,3]
supermatrix2=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
supermatrix1_split=cv2.split(supermatrix1)
channels=[0,1] #h and s
histSize=[180,256]
ranges=np.zeros(len(histSize)*2)
ranges[1::2]=histSize
hist = cv2.calcHist(supermatrix1_split, channels, None, histSize, ranges)
cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
dst = cv2.calcBackProject([supermatrix2],channels,hist,ranges,1)
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
cv2.filter2D(dst,-1,disc,dst)

There is a way to make it work and not crash?

The reference is from here: https://docs.opencv.org/3.3.1/dc/df6/...

edit retag flag offensive close merge delete