The objective is to determine the position of all instances of a given template in an image. The template is given as a separate image.
The source image. | The template. | The detected instances of the template. |
The method is based on the cross correlation. Cross-correlating an image with a known template gives high values wherever the template is a match.
The method is quite simple :
The cross correlation measures for each pixel the degree of matching between the template and the image centered at the pixel. The correlation value is between [0..1], where 1 corresponds to a perfect match.
The correlation is implemented by convolution:
where t is the mean value of template image and fu,v is the mean value of the image pixels inside the window around the pixel (u,v).
pany2pan page.png page.pan pcrosscorrelation page.pan letter.pan image1.pan | |
image1.pan : The result of the cross-correlation seen as level map. |
The localization consists in finding all the pixels that are local maxima and have a correlation value greater than 0.7 out of 1.
pabs image1.pan tmp1.pan plocalmaxima 8 tmp1.pan tmp2.pan pbinarization 0.7 1 tmp2.pan image2.pan | |
image2.pan : The peaks of cross-correlation. |
For the sake of visualization, the output image is built with the pixels of the input image inside a window around the maxima. The size of the window is the size of the template image.
i) Unix/Linux (bash)
pproperty 0 letter.pan; ncol=`pstatus` pproperty 1 letter.pan; nrow=`pstatus` pgetwindowaroundpoints $ncol $nrow 0 page.pan image2.pan image3.pan pproperty 0 letter.pan call pstatus call pset ncol pproperty 1 letter.pan call pstatus call pset nrow pgetwindowaroundpoints %ncol% %nrow% 0 page.pan image2.pan image3.pan ppan2png image3.pan output.png | |
image3.pan : The detected instances of the template. |