Can I convert RGB pixel values to Letters in an Image?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have classified an image using imsegkmeans with 5 classes and displayed it in RGB. I would like to convert those 5 colors into letters, such as "W", "A", "B", etc. to indicate the landcover classification and display them as a text image like old time landcover classification maps used to do. The matrix might look like:
AAAAWBBB
AAAWBBBB
AAWBBBBB
where letters fill each pixel value in the resulting "image"
Thanks
2 Kommentare
Jan
am 24 Mär. 2022
What are the available inputs? Describing the method you have applied to create them is less useful than telling the type and size, e.g. with some example data.
Akzeptierte Antwort
Voss
am 24 Mär. 2022
Bearbeitet: Voss
am 24 Mär. 2022
I'm not sure if this is what you're going for, but if not, it may contain something useful. It's simply labeling each pixel with a letter according to the pixel's color.
% 'class' names:
classes = 'WRGBO';
% corresponding colors:
colors = uint8([ ...
255 255 255; ... white (W)
255 0 0; ... red (R)
0 255 0; ... green (G)
0 0 255; ... blue (B)
255 128 0; ... orange (O)
]);
% create a random MxNx3 uint8 RGB image consisting of only those 5 colors:
M = 10;
N = 7;
im = reshape(permute(colors(randi(5,M*N,1),:),[1 3 2]),[M N 3]);
imshow(im);
% now, given that image, find the index in 'colors' of the color of each
% pixel (1->white, 2->red, 3->green, 4->blue, 5-> orange):
[~,idx] = ismember(reshape(im,[M*N 3]),colors,'rows');
idx = reshape(idx,M,N)
% and index 'classes' with that index matrix:
pixel_classes = classes(idx)
% finally create a text centered in each pixel, labeling the pixel:
[X,Y] = meshgrid(1:N,1:M);
arrayfun(@(x,y)text(x,y,pixel_classes(y,x), ...
'HorizontalAlignment','center','VerticalAlignment','middle'),X,Y);
0 Kommentare
Weitere Antworten (1)
DGM
am 24 Mär. 2022
Bearbeitet: DGM
am 25 Mär. 2022
I'm not sure if this is what you're after, but you can generate images of text in various ways. Bear in mind that for a MxN input, your output will be at least M*glyphheight x N*glyphwidth. This means that the output may be quite large. This also means that the aspect ratio of the characters will influence the aspect ratio of the objects in the image.
The computer vision toolbox has insertText() which can insert text into a raster image. You'll have to find a way of dealing with alignment and placement. The characters are going to generally be large and non-square.
There are a number of text-to-image tools on the File Exchange. MIMT has textim(), which is built around using legacy bitmap fonts with some emphasis on creating images of compact text. Textim() can create text that's smaller than what insertText() can do.
Let's say we have a 5x5 image that was made with a known colormap:

inpict = imread('mappict.png'); % an RGB image
cmap = hsv(6); % the map used to create the above image
inpict = rgb2ind(inpict,cmap)+1;
classes = {'A','B','C','D','E'};
intext = reshape([classes{inpict.'}],fliplr(size(inpict))).'
% build the lines of text
nl = size(intext,1);
outpict = {nl,1};
for n = 1:nl
%outpict{n} = textim(intext(n,:),'micronum'); % 5x4 font
%outpict{n} = textim(intext(n,:),'hp-100x-8x8'); % 8x8 font
outpict{n} = textim(intext(n,:),'wyse-700b'); % 32x16 font
end
% merge them into a single array
outpict = imstacker(outpict,'gravity','w','padding',0,'dim',1);
% wyse-700b is a double-height font, so it can be rescaled to be square
% don't do this with any font which is not double-height!
outpict = imresize(outpict,[size(outpict,1)/2 size(outpict,2)],'nearest');
% add some padding on the edges
outpict = addborder(outpict,5);
imshow(outpict)


The smaller image uses 'hp-100x-8x8' font at native resolution
The larger image uses 'wyse-700b' font at half native vertical resolution.
You mentioned that the output should be RGB. If there is supposed to be some colorization going on, you'll have to describe what that should be. As is, the output is a single-channel image. You can always do something like:
inpict = imread('mappict.png'); % an RGB image
cmap = hsv(6); % the map used to create the above image
indpict = rgb2ind(inpict,cmap)+1;
classes = {'A','B','C','D','E'};
intext = reshape([classes{indpict.'}],fliplr(size(indpict))).'
% build the lines of text
nl = size(intext,1);
outpict = {nl,1};
for n = 1:nl
%outpict{n} = textim(intext(n,:),'hp-100x-8x8'); % 8x8 font
outpict{n} = textim(intext(n,:),'wyse-700b'); % 32x16 font
end
% merge them into a single array
outpict = imstacker(outpict,'gravity','w','padding',0,'dim',1);
% wyse-700b is a double-height font, so it can be rescaled to be square
% don't do this with any font which is not double-height!
outpict = imresize(outpict,[size(outpict,1)/2 size(outpict,2)],'nearest');
% apply a color to the output image
colorpict = repelem(inpict,16,16,1); % use the current character size
outpict = im2double(colorpict).*outpict;
% add some padding on the edges
outpict = addborder(outpict,5);
imshow(outpict)


Or you could invert the image prior to composition:


0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!