Create matching template to detect image in Matlab -
i trying detect numbers in sudoku grid using template image.i doing in matlab. example have cropped '1' image , need use image detect other '1' present in image. not going anywhere this. please me. ho
you have choosen hardest number start with, because if use typical template matching approach (using xcorr2
, normxcorr2
, or conv2
) you're going accidentally match horizontal , vertical lines upright part of 4's.
to better demonstrate basic process, start number 7 since little bit more unique of shape.
first want load images in grayscale (extract red channel)
board = imread('board.jpg') board = board(:,:,1); template = imread('seven.jpg') template = template(:,:,1)
in order detect template in image, can use normalized 2d cross-correlation. method shifts template on image , @ each shift computes correlation between template , image.
c = normxcorr2(template, board); figure; imshow(c);
as can see, correlation values high (white) when template matches (where 7's are) , dark doesn't match well.
as can see there 3 main peaks in resulting correlation matrix (knowing how many 7's there little difficult).
the nice thing correlations values mean something. can set cutoff "good". sake of example let's go 0.7 our cutoff;
[row, col] = find(c >= 0.7); hold on plot(col, row, 'y.');
there 35 points @ c
greater 0.7; however, cluster 3 groups.
you cluster these variety of ways. take average center of each group.
now deal more difficult problem of 1's
if @ result of normalized cross-correlation 1 image, this.
as can see, pick ones pick of vertical lines. thankfully can detect using vertical white line , 2d convolution
vline = ones(size(board,1), 1); lineimage = conv2(board, vline, 'same');
you use lineimage
weighting correlation values return "true" peaks.
% normalize lineimage = lineimage ./ max(lineimage(:)); c = c .* lineimage;
we can see true ones little better.
we can use similar method before draw dots (note lower cutoff correlation value).
[row, col] = find(c >= 0.4) p = plot(col, row, 'y.')
as can see picked 4 it's worth shot.
while great exercise, use built-in ocr functionality of matlab
Comments
Post a Comment