Image How do I convert xyz coordinates into an image?

I have the xyz cordinates of a surface obtained from a focus variation microscope. I want to convert those cordinates into image form using matlab and feed into another imaging processing software.
How do I go about this? I have tried using xyz2rgb() and rgb2gray() which give me a colour map.

2 Comments

Okay you have tried is the problem solved?
I think my question is, is the result of trasnforming x,y,z cordinates a color map as shown?

Sign in to comment.

Answers (1)

Colorimetric xyz has nothing at all to do with spatial xyz coordinates. They're entirely different things. You would definitely not use xyz2rgb().
What you want is to either just make the image from the xyz coordinates where z is a gray level
rows = ceil(max(y(:)))
columns = ceil(max(x(:)))
grayLevels = mat2gray(z(:))
grayImage = zeros(rows, columns, 'uint8')
for k = 1 : length(y)
row = ceil(x(k));
col = ceil(y(k));
grayImage(row, col) = grayLevels(k);
end
imshow(grayImage, []);
or use griddedInterpolant() or scatteredInterpolant() (demo attached).

3 Comments

Thank you, but I have the following erros
Index in position 2 is invalid. Array indices must be positive integers or logical values.
Error in xyz (line 9)
grayImage(row, col) = grayLevels(k);
To solve that error.
rows = ceil(max(y(:)))
columns = ceil(max(x(:)))
grayLevels = mat2gray(z(:))
grayImage = zeros(rows, columns, 'uint8')
for k = 1 : length(y)
row = ceil(x(k));
col = ceil(y(k));
if row<1
row=1;
end
if col<1
col=1;
end
grayImage(row, col) = grayLevels(k);
end
imshow(grayImage, []);
Another way would be to just specify how many rows and columns you want the image to be, and scale your x and y to go from one up to the dimension size:
rows = 1080; % HDTV
columns = 1920; % HDTV
grayLevels = mat2gray(z(:))
% Rescale x and y
% Convert scaled x and y to integers with round() because they are row and column
% indexes, which need to be integers.
x2 = round(rescale(x, 1, columns));
y2 = round(rescale(y, 1, rows));
grayImage = zeros(rows, columns, 'uint8')
for k = 1 : length(y2)
% Get the row and column for this y and x, respectively.
row = x2(k);
col = y2(k);
% Now assign the gray level at that location.
grayImage(row, col) = grayLevels(k);
end
imshow(grayImage, []);

Sign in to comment.

Categories

Asked:

on 26 Jun 2020

Commented:

on 5 May 2021

Community Treasure Hunt

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

Start Hunting!