Find all top most black colored points in a binary image on MATLAB and draw a curve joining all of them

1 view (last 30 days)
I have to detect the top most black points of each character in a word which is converted as a binary image, and draw a line using those points.
Tasks to be completed- 1. Identify top most (colored) point in each character of a word image 2. Draw a curve touching those points. Please help.
The curve should be made of all the top most points of each character.

Answers (1)

DGM
DGM on 30 Mar 2023
These two statements are apparently contradictory.
"Identify top most point in each character of a word image. Draw a curve touching those points."
"The curve should be made of all the top most points of each character."
In either case, "the top most point" is still ambiguous. If it is to be a single maximum for each character, which point is chosen for a character such as the E? If it is to be multiple points, is that all vertically-exposed pixels, or is it merely only those which form the upper-most connected group?
Because the original needs don't really matter anymore, I'm going to be lazy and assert that we're after all vertically-exposed pixels, and that "drawing a curve" can be done with plot() instead of drawing it in the image directly.
% an image
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/149068/image.png');
% for sake of clarity, i'm going to get rid of a bunch of padding
inpict = imcrop(inpict,[76 24 158 54]);
% convert to a binary mask
mask = rgb2gray(inpict)<128;
% pad the top edge of the mask so that
% we know no objects touch the top edge
paddedmask = padarray(mask,[1 0],0,'pre');
% find first object pixel in each column
x = 1:size(mask,2); % we'll need this
[~,y] = max(paddedmask,[],1); % find first pixel
% if the result is the top row, we know no object is in that column
badpts = y==1; % no object exists in this column
x(badpts) = []; % remove invalid entries
y(badpts) = [];
y = y-1; % compensate for padding row
% draw the curve
imshow(mask); hold on
plot(x,y,'linewidth',2)

Community Treasure Hunt

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

Start Hunting!