Create an image from x and y locations with greyscale value

1 次查看(过去 30 天)
I am asking essentially this same question, but I do not understand how to go from the vectors to the image without starting with an image (which I do not have). I would prefer a pixelated image I can export e.g. with imwrite, rather than plotting points on a graph and colouring by the z value (which is my backup plan).
I want to create an image when the only information I have is pixel locations (which I can round to a grid) and the greyscale at those points. The pixel locations will not be integers. Here is a small example with 9 data points.
X Y Greyscale
0 0 255
0 0.5 70
0 1 111
0.5 0 26
0.5 0.5 26
0.5 1 255
1 0 108
1 0.5 26
1 1 70

采纳的回答

Walter Roberson
Walter Roberson 2022-8-16
The following code does not assume that every grid location will have a value given, and also it does not assume that the coordinates are equally spaced. If there are multiple values for the same location, then it will average the values.
If you have a full grid of values, and the x are in strictly increasing order, and the distance between coordinates is consistent for each dimension, then the solution @KSSV shows will work just fine and with lower cost. This present code is for the case where those constraints do not hold -- this code handles scattered coordinates.
Note: this code does not assume that x and y are to the same scale. If, for example, your x is 0, 0.5, 1, 1.5, 2, and your y is 0, 1, 2, then the code will figure that you have different x and y resolutions, and will not decide that your real intention was to have implied 0's at y = 0.5, y = 1.5
data = [0 0 255
0 0.5 70
0 1 111
0.5 0 26
0.5 0.5 26
0.5 1 255
1 0 108
1 0.5 26
1 1 70];
x = data(:,1);
y = data(:,2);
z = data(:,3);
ux = unique(x); dx = min(diff(ux));
uy = unique(y); dy = min(diff(uy));
xidx = 1 + floor((x - ux(1))/dx);
yidx = 1 + floor((y - uy(1))/dy);
img = uint8(accumarray([yidx,xidx], z, [], @mean))
img = 3×3
255 26 108 70 26 26 111 255 70
imshow(img)

更多回答(1 个)

KSSV
KSSV 2022-8-16
data = [0 0 255
0 0.5 70
0 1 111
0.5 0 26
0.5 0.5 26
0.5 1 255
1 0 108
1 0.5 26
1 1 70] ;
x = data(:,1) ;
y = data(:,2) ;
z = data(:,3) ;
nx = length(unique(x)) ;
ny = length(unique(y)) ;
X = reshape(x,ny,nx) ;
Y = reshape(y,ny,nx) ;
Z = reshape(z,ny,nx) ;

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by