## Python + opencv: histogram backprojection

Machine vision 001 2020-11-16 01:29:10
python opencv histogram backprojection

## Python+OpenCV： Histogram back projection (Histogram Backprojection)

### Algorithm in Numpy

1. First we need to calculate the color histogram of both the object we need to find (let it be 'M') and the image where we are going to search (let it be 'I').

``````import numpy as np
import cv2 as cvfrom matplotlib import pyplot as plt
#roi is the object or region of object we need to find
hsv = cv.cvtColor(roi,cv.COLOR_BGR2HSV)
#target is the image we search in
hsvt = cv.cvtColor(target,cv.COLOR_BGR2HSV)
# Find the histograms using calcHist. Can be done with np.histogram2d also
M = cv.calcHist([hsv],[0, 1], None, [180, 256], [0, 180, 0, 256] )
I = cv.calcHist([hsvt],[0, 1], None, [180, 256], [0, 180, 0, 256] )``````

2. Find the ratio . Then backproject R, ie use R as palette and create a new image with every pixel as its corresponding probability of being target. ie B(x,y) = R[h(x,y),s(x,y)] where h is hue and s is saturation of the pixel at (x,y).

After that apply the condition B(x,y)=min[B(x,y),1].

``````h,s,v = cv.split(hsvt)
B = R[h.ravel(),s.ravel()]
B = np.minimum(B,1)
B = B.reshape(hsvt.shape[:2])``````

3. Now apply a convolution with a circular disc, B=D∗B, where D is the disc kernel.

``````disc = cv.getStructuringElement(cv.MORPH_ELLIPSE,(5,5))
cv.filter2D(B,-1,disc,B)
B = np.uint8(B)
cv.normalize(B,B,0,255,cv.NORM_MINMAX)``````

4. Now the location of maximum intensity gives us the location of object. If we are expecting a region in the image, thresholding for a suitable value gives a nice result.

``ret,thresh = cv.threshold(B,50,255,0)``

That's it !!

### Backprojection in OpenCV

``````####################################################################################################
# Image histogram back projection (Histogram Backprojection)
def lmc_cv_image_histogram_backprojection():
"""
The functionality : Image histogram back projection (Histogram Backprojection).
"""
# Image histogram back projection (Histogram Backprojection)
hsv_roi = lmc_cv.cvtColor(roi, lmc_cv.COLOR_BGR2HSV)
hsv_target = lmc_cv.cvtColor(target, lmc_cv.COLOR_BGR2HSV)
target = lmc_cv.cvtColor(target, lmc_cv.COLOR_BGR2RGB)
# calculating object histogram
roihist = lmc_cv.calcHist([hsv_roi], [0, 1], None, [180, 256], [0, 180, 0, 256])
# normalize histogram and apply backprojection
lmc_cv.normalize(roihist, roihist, 0, 255, lmc_cv.NORM_MINMAX)
dst = lmc_cv.calcBackProject([hsv_target], [0, 1], roihist, [0, 180, 0, 256], 1)
# Now convolute with circular disc
disc = lmc_cv.getStructuringElement(lmc_cv.MORPH_ELLIPSE, (5, 5))
lmc_cv.filter2D(dst, -1, disc, dst)
# threshold and binary AND
ret, thresh = lmc_cv.threshold(dst, 50, 255, 0)
thresh = lmc_cv.merge((thresh, thresh, thresh))
res = lmc_cv.bitwise_and(target, thresh)
res = np.hstack((target, thresh, res))
pyplot.figure('Image Display 1')
pyplot.imshow(res, cmap='gray')
pyplot.xticks([])
pyplot.yticks([])
pyplot.title('Histogram Backprojection')
pyplot.show()
# Save images based on user input
if ord("q") == (lmc_cv.waitKey(0) & 0xFF):
# Destruction of the window
pyplot.close()
return``````