Create an image from x and y locations with greyscale value
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Walter Roberson
on 16 Aug 2022
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))
imshow(img)
0 Comments
More Answers (1)
KSSV
on 16 Aug 2022
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) ;
0 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!