Create an image from x and y locations with greyscale value
조회 수: 6 (최근 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
댓글 수: 0
채택된 답변
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))
imshow(img)
댓글 수: 0
추가 답변 (1개)
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) ;
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!