轴坐标到像素坐标? (Matlab的)
如何将轴坐标转换为像素坐标?我有一组包含负值和浮点值的数据,我需要将所有数据都放入图像中。但像素坐标都是正整数。如何解决负面问题?
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?
您可以将坐标向量传递给 scatter
。
You can pass vectors of coordinates to scatter
.
x = [-1.2 -2.4 0.3 7];
y = [2 -1 1 -3];
scatter(x,y,'.');
如果您需要图像矩阵,
h = figure();
scatter(x,y);
F = getframe(h);
img = F.cdata;
您还可以使用 print
来保存绘制到文件(或只是从图窗口导出),然后使用 imread
来读取文件。
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.
那里也是这一组来自文件交换的m文件已经非常接近你所需要的。
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
precision参数确定多少小数将保持浮点值的位置,从而保持图像的分辨率和精度。可能不需要转换为 uint32
。
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.
如果您有 Nx3
每个 N
点的RGB值矩阵:
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