# Detect letters in an image

52 views (last 30 days)
Ana Gabriela Guedes on 8 Dec 2021
Answered: Rishabh Singh on 4 Jan 2022
Hey!
I have a text image from where I want to detect the letters 'y' by:
• select one morphological operation for the purpose
• design the corresponding structuring element
• apply the filter to the image once
• calculate the skeleton of the detected letter
And I really have no idea how can I do this. Can someone please help? The input image is 'text.png' from matlab

Rishabh Singh on 4 Jan 2022
Hey,
You can refer to this, or you can run the code below.
clear all
close all
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')
Also refer to documentation for more help and also checkout "bwmorph".

### Categories

Find more on Morphological Operations in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!