简体   繁体   中英

How to used linspace matlab function to get the and the y of all pixels in image?

I have an image of 200x200 * 3, and I should to find the x and the y of each pixel of the image by using the linspace Matlab function. I have already used the mesh grid function with the following code:

Ny=200; % # pixels in y direction 
Nx=200; % # pixels in x direction 
resolution=1; % size of a pixel 
img=rand(Ny,Nx,3); % random image 
y=(img(1:Ny))*resolution-resolution/2; 
x=(img(1:Nx))*resolution-resolution/2; 
[x,y]=meshgrid(x,y); 

But my supervisor told me to use the function linspace and I cannot understand how.Can someone help me please

The only reason I can think of that your supervisor wants you to use linspace is for creating the x and y vector inputs to meshgrid . linspace alone is not sufficient to generate all of the pixel coordinates, so you'll have to use meshgrid , ndgrid , repelem or repmat .

The one advantage that linspace has over simply doing something like 1:resolution:Ny is that linspace ensures that the endpoints always appear in the range. For example, if you do

>> 1:.37:4
ans =
    1.0000    1.3700    1.7400    2.1100    2.4800    2.8500    3.2200    3.5900    3.9600

you don't get 4 as the last point. However, if you use linspace :

>> linspace(1,4,9)
ans =
   1.0000   1.3750   1.7500   2.1250   2.5000   2.8750   3.2500   3.6250   4.0000

the spacing is automatically adjusted to make the last element 4 .

So assuming that resolution is the multiplier for the number of points you want, for 2 you want 401 evenly spaced points and for 1/2 you want 101 points, your code would look something like this:

x_points = linspace(1, Nx, Nx*resolution+1);
y_points = linspace(1, Ny, Ny*resolution+1);

(I'm still not sure what the resolution/2 part is supposed to do, so we'll need to clarify that.)

Then you would pass those to meshgrid :

[X,Y] = meshgrid(x_points, y_points);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM