MATLAB exercise program (template matching)

Source: Internet
Author: User

Here is just a simple implementation, which is very similar to histogram reverse projection.

The book says, "the advantage is theoretically, and it is difficult to develop a satisfactory template matching technology ".

Code:

Clear all; close all1_clc1_img1_imread('lena.jpg '); imshow (IMG); IMG = double (IMG); mask = double (imcrop (); [m n] = size (IMG ); [h w] = size (mask); If Mod (H, 2 )~ = 1 h = H + 1; endif Mod (W, 2 )~ = 1 W = W + 1; endmask = imresize (mask, [H 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); Re = imgn; for I = HH + 1: m + HH for J = WW + 1: N + ww tmp = imgn (I-hh: I + HH, J-ww: J + ww); Re (I, j) = sum (TMP-mask ). ^ 2); % least square difference endendfigure; Re = mat2gray (RE (HH + 1: m + HH, WW + 1: N + ww )); imshow (1-Re );

Source image

Template

Result

The brightest point is the position of the eye.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.