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 (최근 30일)
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)

채택된 답변

Walter Roberson
Walter Roberson 2022년 1월 6일
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
DGM
DGM 2022년 10월 21일
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)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by