Hey,
clear all
close all
% Read in image
I = imread('textsample.png');
I = rgb2gray(I);
% Have user select a section of the image and create template
a_template = imcrop(I);
figure
imshow(a_template)
% Cross-correlate the image with the template
Xcorr = normxcorr2(a_template, I);
% Threshold the image
t_Xcorr = graythresh(Xcorr);
Xcorr_threshold = Xcorr > 0.7;
figure
imshow(Xcorr_threshold);
% Dim of the template and image are needed for drawing boxes and loops
temp_dim = size(a_template);
I_dim = size(I);
y_len = temp_dim(1); x_len = temp_dim(2);
% Find the blods in the thresholded image
L = bwlabel(Xcorr_threshold);
blobs = regionprops(L);
% Draw red box around all the centroids of the blobs
figure
imshow(I)
for k = 1:length(blobs)
y1 = blobs(k).Centroid(2) - y_len/2; x1 = blobs(k).Centroid(1) - x_len/2;
pos_vec = [x1-x_len/2 y1-y_len/2 temp_dim(2) temp_dim(1)];
rectangle('Position', pos_vec, 'EdgeColor', 'r');
drawnow;
end
% Display the number of a's
text(175,100, "There are: " + length(blobs) + " a's", 'FontSize', 14, 'color', 'r')