I'me new to this platform and also new to computer vision world. I'm working on a project where i'm using histogram BackProjection to detect a colored object. There are two ways to implement this method, either using numpy or a built-in function form opencv (cv2.calcBackProject). However, I do really want to use the numpy version since it allows to better understand what happens. The code for numpy implementation is shown below:
1 import cv2
2 import numpy as np
3 import matplotlib.pyplot as plt
4 #roi is the object or region of object we need to find
5 roi = cv2.imread('D:/downloads/messi_ground.jpg')
6 hsv = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)
7 #target is the image we search in
8 target = cv2.imread('D:/downloads/messi.jpg')
9 hsvt = cv2.cvtColor(target,cv2.COLOR_BGR2HSV)
10 # Find the histograms using calcHist.
11 M = cv2.calcHist([hsv],[0, 1], None, [180, 256], [0, 180, 0, 256] )
12 I = cv2.calcHist([hsvt],[0, 1], None, [180, 256], [0, 180, 0, 256] )
13 R = M/I
14 h,s,v = cv2.split(hsvt)
15 B = R[h.ravel(),s.ravel()
16 B = np.minimum(B,1)
17 B = B.reshape(hsvt.shape[:2])
This algorithm finds pixels in the target image which are similar to the ones in the Model histogram (in my case the histogram of the roi, M)
So my questions are:
- what literally happens when one histogram is divided over another (line 13)?
- how the resulted histogram (in this case R in line 13) is back projected using the h,s channel of the target image? In other words, how does line 15 back project the Resulted R-histogram?
- Line 16 supposes to make numbers between 0 and 1 in order to view pixel values as probability belonging to the Model histogram. However, I skipped it and got the same results though, so why the algorithms uses it ?
Unfortunately, the paper that explains this method is very abstract and doesn't explicit the functionality of the algorithm (Paper: Indexing via color histogram) I know that this websites supposes to help with direct programming problems. However, I think it should also helps understanding some operations related to programming.