How do I make an image of numbers using imagesc? Change values in the matrix?

14 vues (au cours des 30 derniers jours)
Macy
Macy le 21 Fév 2023
Commenté : Macy le 22 Fév 2023
Hello, I am new to MatLab and manipulating matrices.
I have been instructed to make a 2-D matrix 700x400 of any non-zero numbers I want. I have been asked to use [X,Y]=meshgrid(va,vb) where va and vb are vectors (I beleive the purpose of this is so that I can then use the variables X and Y in other functions).
Then I want to make an image of the function using "imagesc" but in greyscale.
Then I want to change the value of the array elements X: 300:310, Y: 40:50 so that they are the same number and thus show up as a spot in the image.
This is what I have so far. I believe the matrix has been created properly but please correct if not. Then when I try to do imagesc, it only lets me put in one variable, X or Y. It id not in greyscale. And then I am not sure how to change values within the array for X and Y.
Thank you so much!
va=1:700;
vb=1:400;
[X,Y] = meshgrid(va,vb);

Réponse acceptée

DGM
DGM le 22 Fév 2023
I'm going to change a few things, but I think you can fill in the gaps.
As background, any matrix can be considered an image, but image data is typically expected to be presented on a certain scale. The expected scale of image data is determined by its numeric class. The nominal range of an integer-class image is the maximum dynamic range allowed by the datatype (e.g. 0-255 for uint8). For floating-point classes, images are expected to be unit-scale (black is 0, white is 1). Most times when you're creating variables programmatically, they're floating-point (class 'double')
So the question becomes "do I want to create an image, or do I want to take data on some arbitrary scale and temporarily represent it as an image for visualization purposes". Both are valid objectives.
Let's say we just wanted to create an image. Then if we're working in 'double', it should be expected that the matrix we create falls within the unit interval.
outsize = [256 512]; % [y x]
% create some vectors
x = linspace(0,1,outsize(2)); % a row
y = linspace(0,1,outsize(1)).'; % a column
% you could use meshgrid, but it's slower and unnecessary
% post-R2016b implicit expansion means we can just use the vectors
% [x y] = meshgrid(x,y);
% create a matrix of some sort
% this image is nominally unit-scale
% in this case, the actual range is [0.2 0.8]
Z = 0.2 + 0.3*(x+y);
% imshow() will render the image fine as-is
imshow(Z)
% imagesc() will render the image the same with some setup
imagesc(Z) % use 'scaled' cdata mapping
caxis([0 1]) % specify the scale
colormap(gray(256)) % specify a gray colormap
axis image % preserve aspect ratio
On the other hand, let's say we're creating some arbitrarily-scaled data. We just want to view the data as an image. There's now no expectation that the data is any particular range. That's fine. We can still view it.
outsize = [256 512]; % [y x]
% create some vectors
x = linspace(0,1,outsize(2)); % a row
y = linspace(0,1,outsize(1)).'; % a column
% you could use meshgrid, but it's slower and unnecessary
% post-R2016b implicit expansion means we can just use the vectors
% [x y] = meshgrid(x,y);
% create a matrix of some sort
% this data is on some arbitrary scale
% in this case, the actual range is [-300 -100]
Z = 100*(x+y) - 300;
% imshow() normally expects the image to fall within [0 1]
% since the data is far less than 0, it is rendered as black
imshow(Z)
% otherwise, you can specify the displayrange parameter
% if this parameter is empty, it uses the range of the data extrema
imshow(Z,[])
% imagesc() will render the image the same with some setup
% by default, imagesc() renders a 2D array WRT its extrema
imagesc(Z) % use 'scaled' cdata mapping
colormap(gray(256)) % specify a gray colormap
axis image % preserve aspect ratio
Note that in the latter example, the displayed image extends from black to white, whereas in the first example, it extends from dark gray to light gray. In the second example, we're displaying the data with respect to its extrema, not some fixed values defining where black and white are, so the rendered image always extends from black to white. If our goal were to render an image as an image, this loss of brightness/contrast information is generally undesirable. Bear in mind that you can always set the displayrange parameter (or caxis()) to any explicit range you want. If there are contextually-important expectations regarding the possible range of the data, you might choose to render it with respect to those instead of to the data extrema.
As to how to change part of the data/image to be constant valued, that's just simple array indexing.
% change part of the image
% remember we're working with the prior example data
Z(50:100,50:100) = -150;
imshow(Z,[])
  1 commentaire
Macy
Macy le 22 Fév 2023
Thanks, I will look at this in depth to understand the different ways and when they should be used!

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 21 Fév 2023
Try this:
imshow(yourImage, []); % Display matrix.
axis('on', 'image'); % Show tick marks.
impixelinfo; % Let user mouse around and see RGB values and (x,y) coordinate.
If that doesn't work then explain how you got your matrix. Is it a formula where you used X and Y to compute some surface?
  4 commentaires
Image Analyst
Image Analyst le 22 Fév 2023
Of course "yourImage" is the matrix you made. You never told me what you're calling it so I just called it yourImage and figured you'd know to replace that variable with the actual name of your image matrix. I guess not, so here is an example with a sample image I made from the two vectors:
columns = 1:700;
rows = 1:400;
[X,Y] = meshgrid(columns, rows);
subplot(2, 2, 1);
imshow(X, [])
title('X', 'fontSize', 20);
subplot(2, 2, 2);
imshow(Y, [])
title('Y', 'fontSize', 20);
% Make an image that is X^2 + y^2, just as an example function.
yourImage = X .^ 2 + Y .^ 2;
subplot(2, 2, 3);
imshow(yourImage, [])
title('X .^ 2 + Y .^ 2', 'fontSize', 20, 'Interpreter','none');
Macy
Macy le 22 Fév 2023
Ah I see now, thank youI

Connectez-vous pour commenter.

Catégories

En savoir plus sur Images dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by