Calculates the back projection.
The function calculates the back project of the histogram. For each tuple of pixels at the same position of all input single-channel images the function puts the value of the histogram bin, corresponding to the tuple in the destination image. In terms of statistics, the value of each output image pixel is the probability of the observed tuple given the distribution (histogram). For example, to find a red object in the picture, one may do the following:
That is the approximate algorithm of Camshift color object tracker, except for the 3rd step, instead of which CAMSHIFT algorithm is used to locate the object on the back projection given the previous object position.
Locates a template within an image by using a histogram comparison.
The function calculates the back projection by comparing histograms of the source image patches with the given histogram. Taking measurement results from some image at each location over ROI creates an array image . These results might be one or more of hue, x derivative, y derivative, Laplacian filter, oriented Gabor filter, etc. Each measurement output is collected into its own separate image. The image image array is a collection of these measurement images. A multi-dimensional histogram hist is constructed by sampling from the image image array. The final histogram is normalized. The hist histogram has as many dimensions as the number of elements in image array.
Each new image is measured and then converted into an image image array over a chosen ROI. Histograms are taken from this image image in an area covered by a “patch” with an anchor at center as shown in the picture below. The histogram is normalized using the parameter norm_factor so that it may be compared with hist . The calculated histogram is compared to the model histogram; hist uses The function cvCompareHist with the comparison method= method ). The resulting output is placed at the location corresponding to the patch anchor in the probability image dst . This process is repeated as the patch is slid over the ROI. Iterative histogram update by subtracting trailing pixels covered by the patch and adding newly covered pixels to the histogram can save a lot of operations, though it is not implemented yet.
Back Project Calculation by Patches
Calculates the histogram of image(s).
The function calculates the histogram of one or more single-channel images. The elements of a tuple that is used to increment a histogram bin are taken at the same location from the corresponding input images.
# Calculating and displaying 2D Hue-Saturation histogram of a color image import sys import cv def hs_histogram(src): # Convert to HSV hsv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, hsv, cv.CV_BGR2HSV) # Extract the H and S planes h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) cv.Split(hsv, h_plane, s_plane, None, None) planes = [h_plane, s_plane] h_bins = 30 s_bins = 32 hist_size = [h_bins, s_bins] # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */ h_ranges = [0, 180] # saturation varies from 0 (black-gray-white) to # 255 (pure spectrum color) s_ranges = [0, 255] ranges = [h_ranges, s_ranges] scale = 10 hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) (_, max_value, _, _) = cv.GetMinMaxHistValue(hist) hist_img = cv.CreateImage((h_bins*scale, s_bins*scale), 8, 3) for h in range(h_bins): for s in range(s_bins): bin_val = cv.QueryHistValue_2D(hist, h, s) intensity = cv.Round(bin_val * 255 / max_value) cv.Rectangle(hist_img, (h*scale, s*scale), ((h+1)*scale - 1, (s+1)*scale - 1), cv.RGB(intensity, intensity, intensity), cv.CV_FILLED) return hist_img if __name__ == '__main__': src = cv.LoadImageM(sys.argv) cv.NamedWindow("Source", 1) cv.ShowImage("Source", src) cv.NamedWindow("H-S Histogram", 1) cv.ShowImage("H-S Histogram", hs_histogram(src)) cv.WaitKey(0)
Divides one histogram by another.
The function calculates the object probability density from the two histograms as:
So the destination histogram bins are within less than scale .
The function sets all of the histogram bins to 0 in the case of a dense histogram and removes all histogram bins in the case of a sparse array.
Compares two dense histograms.
The function compares two dense histograms using the specified method ( denotes the first histogram, the second):
where N is the number of histogram bins.
Bhattacharyya distance (method=CV_COMP_BHATTACHARYYA)
The function returns .
Note: the method CV_COMP_BHATTACHARYYA only works with normalized histograms.
To compare a sparse histogram or more general sparse configurations of weighted points, consider using the CalcEMD2 function.
Creates a histogram.
The function creates a histogram of the specified size and returns a pointer to the created histogram. If the array ranges is 0, the histogram bin ranges must be specified later via the function SetHistBinRanges . Though CalcHist and CalcBackProject may process 8-bit images without setting bin ranges, they assume thy are equally spaced in 0 to 255 bins.
Finds the minimum and maximum histogram bins.
The function finds the minimum and maximum histogram bins and their positions. All of output arguments are optional. Among several extremas with the same value the ones with the minimum index (in lexicographical order) are returned. In the case of several maximums or minimums, the earliest in lexicographical order (extrema locations) is returned.
Normalizes the histogram.
The function normalizes the histogram bins by scaling them, such that the sum of the bins becomes equal to factor .