convert xyz data to 2-d bw image, no interpolation

4 次查看(过去 30 天)
Hi --
I've been reading lots of somewhat similar questions/answers but most seem to deal with interpolating scattered data (i.e. creating surfaces).
I simply would like to convert a set of points (x,y) each with a value of 1 to a bw image. So just a point vector to raster conversion more or less where the resolution is just 1.
Also, I would like to set the dimensions of the output matrix manually such that it can be arbitrarily larger than the extents of the x,y data.
This seems like it should be straightforward but I'm stuck... Any help is appreciated.
Thanks, Mike

采纳的回答

Image Analyst
Image Analyst 2011-11-15
Use zeros() to preallocate an array of a desired size. Then you can always just use a nested for loop (two for's) to assign the values for each x and y. Be aware that x=column and y = row so it's array(y, x), not array(x,y).

更多回答(2 个)

Walter Roberson
Walter Roberson 2011-11-16
xmin = min(x(:));
ymin = min(y(:));
binarray = accumarray([x(:) - xmin + 1, y(:) - ymin + 1], 1);
binarray = logical(binarray);
The logical() step is there because accumarray() in that form will total the number of hits within each matrix entry, so logical() will convert it to 0 for no hits and 1 for 1 or more hits. You could do the calculation without that conversion step if you used the proper preparation but this way is simple and probably faster.
This technique relies upon x-xmin and y-ymin being integral, so if your resolution is not really exactly 1, then you will want to put in appropriate round() or the like.
The subtraction of xmin and ymin will align the bounding box with the stored matrix, which could be important if (for example) your x started at several thousand.
Padding around the bounding box is probably easiest done after the fact, but you could also subtract off less than xmin and ymin to pad at the top and left, and you could pass accumarray a matrix size parameter as its third input.
  1 个评论
Walter Roberson
Walter Roberson 2011-11-16
And if anyone reading this is wondering: Yes, the accumarray technique can be used nicely for non-integral data as well, provided that you are willing to define the grid resolution ahead of time; I have posted the code in other Answers.

请先登录,再进行评论。


Mike Alonzo
Mike Alonzo 2011-11-16
Thanks Walter and Image Analyst. My matlab newness only allowed me to understand the nested loop option for now... Here's what I did:
Where xc,yc,zc is my original xyz data:
FullMat=zeros(272,268);
for i=1:length(xc)
for i=1:length(yc)
FullMat((yc(i)),(xc(i)))=(zc(i));
end
end
  1 个评论
Image Analyst
Image Analyst 2011-11-16
Yeah, that's what I figured. By the way, you can omit some parentheses there. You have extra pairs of parentheses around your coordinate vectors that are not necessary and make it look more confusing that it really is. And like Walter said make sure your xc and yc are integers. Your FullMat is double so to see it you'd have to do this
imshow(FullMat, []);
Note use of the empty brackets. If zc is also integer, then you can do
FullMat = zeros(272, 268, 'uint8');
for the example of an 8 bit gray scale image. Then you wouldn't need the empty brackets to show something unless you want to do some extra scaling to increase the contrast.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by