How can i convert the axis coordinates into pixel coordinates? I have a set of data which include the negative and floating values, i need to put all of the data into the image. But the pixel coordinates are all positive integer. how to solve the negative issue?
You can pass vectors of coordinates to scatter
.
x = [-1.2 -2.4 0.3 7];
y = [2 -1 1 -3];
scatter(x,y,'.');
If you need the image matrix,
h = figure();
scatter(x,y);
F = getframe(h);
img = F.cdata;
You can also use print
to save the plot to a file (or simply export from figure window), then use imread
to read the file.
There is also this set of m-files from the File Exchange, which are already very close to what you need.
Finally, here is a simple way to get what you want, within a specified precision:
precision = 10; %# multiple of 10
mi = min(min(x),min(y));
x = x - mi; %# subtract minimum to get rid of negative numbers
y = y - mi;
x = round(x*precision) + 1; %# "move" decimal point, round to integer,
y = round(y*precision) + 1; %# add 1 to index from 1
img = zeros(max(max(x),max(y))); %# image will be square, doesn't have to be
x = uint32(x);
y = uint32(y);
ind = sub2ind(size(img),y,x); %# use x,y or reverse arrays to flip image
img(ind) = 1; %# could set intensity or RGB values in a loop instead
The "precision" parameter determines how many decimal places of the floating point values will be kept, and thus the resolution and accuracy of the image. The cast to uint32
might be unnecessary.
If you have a Nx3
matrix of RGB values for each of N
points:
img = zeros(max(max(x),max(y)),max(max(x),max(y)),3);
for i=1:length(N) %# N = length(x) = length(y)
img(x(i),y(i),:) = rgb(i,:);
end