How can I convert a XYZ format to an image format

13 次查看(过去 30 天)
I have a text file with 3 columns; #ith pixel #jth pixel #Height of the (ith,jth) pixel in microns
(its basically a data for plotting the surface height from a camera)
How can I convert this to a gray scale image format so that I could use the Image Processing Toolbox?
Thanks, Shayan

采纳的回答

Walter Roberson
Walter Roberson 2011-7-11
If the spacing of the data is not completely regular, you could use griddata() or possibly interp2() to create a regular mesh grid.
If the spacing of the data is regular, then you can probably read the text file, extract the third column, and reshape() it (with possibly a transposition or two thrown in.)
[Edit: Moving part of answer from a comment to the answer - AU]
In the special case where your I and J are positive integers, you can use the following to build the matrix and fill any unused locations with the given FillValue:
FillValue = nan;
DataMatrix = accumarray(YourData(:,1:2),YourData(:,3),[],[],FillValue);
This will, though, construct the matrix indexed at 1 to max(I) and 1 to max(J); if your I and J span a subrange of that then it is more efficient to offset them when building the matrix. If your I or J can be non-positive then to use accumarray() you would have to offset them; e.g.,
Ioffset = min(YourData(:,1)) - 1;
Joffset = min(YourData(:,2)) - 1;
accumarray([YourData(:,1)-Ioffset, YourData(:,2)-Joffset], ...)
  4 个评论
Chad Greene
Chad Greene 2016-4-26
I turned Walter's excellent solution into a function called xyz2grid.

请先登录,再进行评论。

更多回答(2 个)

Ashish Uthama
Ashish Uthama 2011-7-12
What is the range and resolution of i's and j's? Can you scale these coordinates into integer values?
If you could scale them into a 1 to N and 1 to M range, you could create a matrix of NaN's/zeros/base height with size [N M] and then index into it with the i's and j's to populate the height values.
Pixel locations with no entry in your data file will remain at the initial value.

Shayan
Shayan 2011-7-12
Ashish, the is and js are integers.
I read the documentation for the imgae data format and now I believe I found out what is the problem. My data is in vector form. For the gray scale image data is in form of a matrix. Every element in the matrix represents the gray color code.
So I guess if I reshape my Z axis into gray scale and do a one to one mapping from vector to a matrix I should be able to display the image.
As far as the missing data is concerned I am going to set them to be white.
Now my new question is, "when I do cross correlation of two images can I some how specify to not perform cross correlation for the white cells?"
Thanks!
  1 个评论
Ashish Uthama
Ashish Uthama 2011-7-12
If you have all your pixels in the vector form, as Walter suggested, using reshape might do the trick.
Please consider accepting Walter's answer if it works for you and staring another question for the cross correlation question.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by