When tracking meanshift objects, there is a step in the middle called histogram reverse projection, So I implemented this step first.
The histogram reverse projection is simply template matching. If a small target template is given, the original image and the template image are traversed one by one, and the same image blocks are compared, save the comparison result to a new image. The global extreme value in the new image is the position of the template in the original image. The main trouble here is how to compare the histograms of two image blocks. opencv Implements Five comparison methods, so I have implemented five corresponding methods here.
The five methods are correl (correlation), chisqr (chi-square), intersect (intersection), and bhattacharyya (long name --!!) , EMD (earth mover's distance ).
The following is the formula for these five methods. H1 is the histogram sequence of the first image block, while H2 is the histogram sequence of the second image block:
Related methods:
Where:
Card method:
Intersection method:
Bhattacharyya:
EMD:
The first four methods are relatively simple, and EMD is quite troublesome. below is my understanding. This algorithm uses the original sequence to generate the two matrices F and D, and then processes the two matrices. In my understanding, the D matrix is the distance between each element in H1 and the position of each element in H2. For example, H1 and H2 are sequences of only three elements. H1 (1) the distance from H2 (1) is 0, so the distance between D (1, 1) = 0, H1 (3) and H2 (2) is 1, so d (3, 2) = 1, this d = [0 1 2; 1 0 1; 2 1 0], here d and H1, H2 values do not matter. F is difficult to determine. It is a headache to look at the constraints. There is no way to write a program according to the constraints, but I think the result of a clever method just meets the four constraints: first, no matter the first three conditions, only use the fourth condition to obtain the matrix F. Then, subtract the adjacent left column from the rightmost column of F, and then subtract the adjacent top row from the rightmost column of F, the obtained results meet the conditions. I can't explain why, so I want to find a math to explore.
The following code is used:
Main. m
Close all; clear all1_clc1_img1_imread('lena.jpg '); imshow (IMG); [m n] = size (IMG); W = imcrop (); % here, the image to be cropped is displayed in the [H w] = size (w); hist1 = histcount (w); HH = floor (H/2 ); WW = floor (W/2); imgn = zeros (m + 2 * hh + 1, n + 2 * ww + 1); imgn (HH + 1: m + HH, WW + 1: N + ww) = IMG; imgn (1: hh, WW + 1: N + ww) = IMG (1: hh, 1: N ); imgn (1: m + HH, N + WW + 1: n + 2 * ww + 1) = imgn (1: m + HH, N: N + ww ); imgn (m + HH + 1: m + 2 * hh + 1, WW + 1: n + 2 * ww + 1) = imgn (M: m + HH, WW + 1: n + 2 * ww + 1); imgn (1: m + 2 * hh + 1, 1: ww) = imgn (1: m + 2 * Hh + 1, WW + * ww); RE1 = imgn; re2 = imgn; RE3 = imgn; RE4 = imgn; re5 = imgn; for I = HH + 1: m + HH for J = WW + 1: N + ww s = imgn (I-hh: I + HH, J-ww: J + ww ); hist2 = histcount (s); RE1 (I, j) = correl (hist1, hist2); % correlation method re2 (I, j) = chisqr (hist1, hist2 ); % card method RE3 (I, j) = intersect (hist1, hist2); % intersection method RE4 (I, j) = bhattacharyya (hist1, hist2 ); % method re5 (I, j) = EMD (hist1, hist2) with a long name; % because there is no optimization, the speed is too slow. Run it for at least one night. Use it with caution !! Endendfigure; RE1 = RE1 (HH + 1: m + HH, WW + 1: N + ww); imshow (mat2gray (RE1); figure; re2 = re2 (HH + 1: m + HH, WW + 1: N + ww); imshow (mat2gray (re2); figure; RE3 = RE3 (HH + 1: m + HH, WW + 1: N + ww); imshow (mat2gray (RE3); figure; RE4 = RE4 (HH + 1: m + HH, WW + 1: N + ww); imshow (mat2gray (RE4); figure; re5 = re5 (HH + 1: m + HH, WW + 1: N + ww ); imshow (mat2gray (re5 ));
Histcount. m statistical histogram
function hist=histcount(w) [H W]=size(w); w=uint8(w); hist=zeros(1,256); for i=1:H for j=1:W hist(w(i,j)+1)=hist(w(i,j)+1)+1; end end hist=hist/(H*W);end
Correl. m Correlation Method
function d=correl(H1,H2) d=sum(H1-mean(H1).*(H2-mean(H2)))/sqrt(sum((H1-mean(H1)).^2)*sum((H2-mean(H2)).^2));end
Chisqr. M card method
function d=chisqr(H1,H2) d=sum(((H1-H2).^2)/(H1+H2));end
Intersect. m intersection method
function d=intersect(H1,H2) d=sum(min(H1,H2));end
Bhattacharyya. m
function d=bhattacharyya(H1,H2) d=sqrt(1-sum(sqrt(H1.*H2))/sqrt(sum(H1)*sum(H2)));end
EMD. m is not optimized here. Use it with caution!
function re=emd(H1,H2) m=length(H1); n=length(H2); f=zeros(m,n); d=zeros(m,n); for i=1:m for j=1:n if i==j d(i,j)=0; end if j>i d(i,j)=j-i; end if j<i d(i,j)=i-j; end f(i,j)=min(sum(H1(1:i)),sum(H2(1:j))); end end for i=m:-1:2 f(:,i)=f(:,i)-f(:,i-1); end for j=n:-1:2 f(j,:)=f(j,:)-f(j-1,:); end re=(sum(sum(f.*d)))/sum(sum(f)); end
The following figure shows the running effect:
Source image
The template used is Lena's right eye.
Correlation Method
Card method
Intersection method
Bhattacharyya Method
EMD Method
EMD has the longest time, and the effect is good. The fastest way to card, the worst effect. The intersection method and bhattacharyya have good results, and the time is not too slow.
Refer:
1. http://www.cnblogs.com/xrwang/archive/2010/02/04/HowToUseHistogram.html
2. http://blog.163.com/woshitony111@126/blog/static/71379539201262202820650/
3. http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/RUBNER/emd.htm
4. http://vision.stanford.edu /~ Rubner/papers/rubnericcv98.pdf