Display images instead of numbers in a Matrix.

8 vues (au cours des 30 derniers jours)
Zerbin Singleton
Zerbin Singleton le 14 Avr 2023
I want to create a matrix, that displays pictures in the matrix instead of Numbers. So if the value in the Matrix is a one I want it to display one picture and if it is a zero another picture. 0 = red house. 1 equal green house.
0 1 0
1 1 1
0 0 0

Réponses (4)

DGM
DGM le 15 Avr 2023
An example using montage()/imtile().
% the images
type1 = imread('F1.png'); % 100x70
type2 = imread('F2.png'); % 70x100
% an explicit tiling map
typemap = [2 1 2
1 2 1
2 1 2
1 1 1];
% prepare things for montage()/imtile()
cases = {type1,type2}; % make things addressable
typemap = typemap.'; % tiling is row-major
tiling = [size(typemap,2) size(typemap,1)]; % [x y]
% display only
montage(cases(typemap),'size',tiling,'backgroundcolor','w')
% output as an image which can then be edited/saved/displayed/etc
outpict = imtile(cases(typemap),'gridsize',tiling,'backgroundcolor','w');
imwrite(outpict,'myfruit.png') % write it
imshow(outpict) % or display it
Note the images here do not share the same geometry. If they did share the same geometry, everything would simplify.
% the same images
type1 = imread('F1.png'); % 100x70
type2 = imread('F2.png'); % 70x100
% i'm rotating one of the images
% so they both have the same geometry
% for sake of the example
type2 = rot90(type2,1); % 100x70
% an explicit tiling map
typemap = [2 1 2
1 2 1
2 1 2
1 1 1];
% construct the output image
cases = {type1,type2};
outpict = cell2mat(cases(typemap));
% display/edit/save it as desired
imshow(outpict)
Note that in all of these cases, the output is either an image array, or a graphics object displayed in a figure. I don't know where you would be displaying numbers, but I doubt you could display images in the same place you might display said numbers.

Walter Roberson
Walter Roberson le 14 Avr 2023
There is no way to do that.
If you were to create your own object class, you could get the information about the images into one place, and maybe the content of the images too. You could create your own disp() and display() methods for the class.
However, what you cannot do is display images to the command window. Displaying characters (that might happen to be emoji) to the command window is the most you can do.
You can use tiledlayout to create a grid of images in a figure() or uifigure(), or you could use the XData and YData properties of image() to place images exactly where you wanted inside one axes (instead of a grid of axes). But you cannot display images to the command window.

Image Analyst
Image Analyst le 15 Avr 2023
You would have to create a blank image (canvass) and then run through your numerical matrix pasting the appropriate image onto the blank canvass at the appropriate location. See copy and paste demo, attached. Of course you can use imshow() to display the image you're building, either after each house is pasted, or after all are pasted.

Adam Danz
Adam Danz le 15 Avr 2023
Here are some solutions using emojis.
One emoji, different colors
emoji = char([55356 57312]); % "🏠"
data = [0 1 0; 1 1 1; 0 0 0];
[x,y] = meshgrid(1:height(data),1:width(data));
idx = data==1;
figure()
text(x(idx),y(idx),emoji,...
'Color',[0 .5 0],...
'HorizontalAlignment','Center',...
'VerticalAlignment','Middle',...
'FontSize',60)
text(x(~idx),y(~idx),emoji,...
'Color',[1 0 0],...
'HorizontalAlignment','Center',...
'VerticalAlignment','Middle',...
'FontSize',60)
xlim([0,4])
ylim([0,4])
axis('ij')
Two emojis
emoji1 = char([55356 57312]); % "🏠"
emoji2 = char([55356 57323]); % "🏫"
data = [0 1 0; 1 1 1; 0 0 0];
[x,y] = meshgrid(1:height(data),1:width(data));
idx = data==1;
figure()
text(x(idx),y(idx),emoji1,...
'HorizontalAlignment','Center',...
'VerticalAlignment','Middle',...
'FontSize',60)
text(x(~idx),y(~idx),emoji2,...
'HorizontalAlignment','Center',...
'VerticalAlignment','Middle',...
'FontSize',60)
xlim([0,4])
ylim([0,4])
axis('ij')

Community Treasure Hunt

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

Start Hunting!

Translated by