How do I create an image from random unsorted x, y, intensity values?

4 views (last 30 days)
I have a very large vector of x values, a very large vector of y values, and a corresponding vector of intensity values. That is, intensity(i) will be the intensity of the x(i),y(i) location.
I want to create an intensity "image", that is, some sort of image that just shows the intensity values at their various x,y locations. MS paint drawing below, where the image is inside the black square.
What is the most efficient way of doing this? I understand there will be black spots from the pixels that have no intensity values, although maybe I could spread out each of the intensity values so that they cover each other.
Edit:
I have a separate but related question, that I will attach just in case someone gets to it. Given an image of intensity values that I will imread in, and my original x and y vectors, I also would like to know how to extract a corresponding vector of intensity values from the x and y.

Answers (1)

Rik
Rik on 29 Dec 2018
In the code below I'm assuming x and y are valid sub-indices.
figure(1),clf(1)
%set a function we'll need later
rot90CW=@(IM) permute(IM(end:-1:1,:,:),[2 1 3]);
IM=reshape(1:12,3,4);
% %read image to 8-bit intensity map
% IM=imread('example.png');IM=uint8(mean(IM,3));
% %generate example image
% IM=uint8(randi(255,400,200));
subplot(1,2,1)
imshow(IM,[]),title('original image')
%convert to x-y-intensity array
%in Matlab the y as you indicate is the first dimension (down is +)
[x,y]=ndgrid(1:size(IM,2),size(IM,1):-1:1);
matrix=[x(:),y(:),double(IM(:))];
%now we can go the reverse direction: from matrix to image
sz=[max(double(matrix(:,2))) max(double(matrix(:,1)))];%output image size
subs=sub2ind(sz,matrix(:,2),matrix(:,1));%convert x-y to linear index
val=matrix(:,3);
fun=@mean;%or fun=@sum;
fillval=cast(mean(val),'like',fun(val(1)));%fill empty spots with the this value
A = accumarray(subs,val,[prod(sz) 1],fun,fillval);
A=reshape(A,sz);
A=rot90CW(A);
A=reshape(A,sz);
subplot(1,2,2)
imshow(A,[]),title('reconstructed image')
  1 Comment
Rik
Rik on 7 Jan 2019
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!