text.how can I find the largest and smallest size letters in a png image and display them in a different color in the image (without deleting other letters)?

2 views (last 30 days)
how can I find the largest and smallest size letters in the image below and display them in a different color in the image (without deleting other letters)? With the following code, I find the uppercase letter and the lowercase letter, but the other letters are deleted and I can't show them in a different color
BW = imread('text.png');
BW2 = bwpropfilt(BW,'perimeter',1);
figure,imshow(BW2)
BW4=bwareafilt(BW,1,'smallest');
figure, imshow(BW4)

Accepted Answer

Walter Roberson
Walter Roberson on 6 Jan 2022
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/855240/image.jpeg';
img = imread(filename);
if ndims(img) > 2; gray = rgb2gray(img); else gray = img; end
BW = imbinarize(gray);
BW2 = bwpropfilt(BW,'perimeter',1);
Warning: One or more ties for n-th place occurred when selecting objects. Some objects were not selected.
figure,imshow(BW2)
BW4 = bwareafilt(BW,1,'smallest');
Warning: One or more ties for n-th place occurred when selecting objects. Some objects were not selected.
figure, imshow(BW4)
BW3 = repmat(BW, 1, 1, 3);
BW3(:,:,1) = BW & ~ BW4;
BW3 = im2uint8(BW3);
figure, imshow(BW3)
  4 Comments
DGM
DGM on 21 Oct 2022
Just for fun, let's say we wanted to color code all the letters by their size (area).
inpict = imread('text.png'); % a binarized image
N = 256; % length of color table
CT0 = parula(N); % a color table
% get blob areas
S = regionprops(inpict,'area');
% rescale areas to create indices into color table
area = vertcat(S.Area);
ctidx = round(normalize(area,'range')*(N-1) + 1);
% create augmented CT
CT = [0 0 0; CT0(ctidx,:)];
% get label image, convert to RGB
L = bwlabel(inpict);
outpict = ind2rgb(L+1,CT); % FP-class indexed images are offset by 1
% display it
imshow(outpict)

Sign in to comment.

More Answers (0)

Categories

Find more on Images 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!