%%% % % CNS/EE148 % % this code is used in Problem 1 of Homework 1 % % %%% % this is the threshold used for matched filtering % how should this be chosen ? MF_THRESH = 6000000; % we assume the imagefiles are in the current directory. % if not, adjust the filename. inputImg = double(imread('img1.jpg')); % we make a template from this image... testImg = double(imread('img2.jpg')); % and do a correlation with this one. % Show the image in a figure; figure(1); imagesc(inputImg / max(inputImg(:)) ); colormap(gray); axis('image'); title('Input Image'); % select a template by hand. T = getTemplate(inputImg); % show the template we just extracted. imagesc(T); axis('image'); title('Correlation Template'); % Note that the template is not normalized with respect % to brightness or contrast. % !!! ADD CODE HERE TO NORMALIZE THE TEMPLATE !!! % !!! ADD CODE THAT COMPUTES THE CORRELATION BETWEEN TEMPLATE AND TEST % IMAGE -> CALCULATES R !!! % * for example, you can note that a *correlation* is equivalent to a *convolution* % (function conv2 for images convolution) if the kernel (template) is % mirrored in x and y direction (functions flipud and fliplr to mirror an % image). % % * another possibility is to consider both images as vectors (function % reshape) and to do a scalar product of vectors ( = v1 * v2'; ) % Show the results of the correlation figure(3); imagesc(R); axis('image'); title('Correlation Result'); % Use a nice colormap, and display legend. colormap jet; colorbar; % find local maxima in 3x3 neighborhoods. this % is important since the template will probably % respond strongly with in a couple pixel neighborhood % of the object to be detected % % There's a built-in localmax function in matlab, but it doesn't seem to % do what we want (a bonus to whoever figures out how to get it working !) % You can download another 'localmax.m' from the % homework web page. lm = localmax(R); % find local maxima that exceed threshold detectionsIdx = find(lm & R > MF_THRESH); % Since 'find' treats all matrices as 1D vectors, we need to do % some work to recover the (i, j) indices detcJ = ceil(detectionsIdx / size(testImg, 1)); detcI = detectionsIdx - (detcJ - 1) * size(testImg, 1); % Make nice plot by overlaying image with detected locations figure(4); clf; imagesc(testImg); colormap(gray); axis('image'); title('Detection Results'); hold on; plot(detcJ, detcI, 'r+'); hold off; % Make a colorful PostScript file that can be send to the printer % and attached to the homework. print -depsc results.eps