轴坐标到像素坐标? (Matlab的)

时间:2011-10-11 04:02:18

标签: image matlab pixel coordinate

如何将轴坐标转换为像素坐标?我有一组包含负值和浮点值的数据,我需要将所有数据都放入图像中。但像素坐标都是正整数。如何解决负面问题?

2 个答案:

答案 0 :(得分:4)

根据我的理解,你有一组代表椭圆的点,你可以直接在图像矩阵中绘制它们(不只是在屏幕上显示它们)。

为此,您可以使用POLY2MASK函数将椭圆转换为二进制掩码。然后通过计算它的perimeter,这将给我们一个二进制掩码,它只代表构成椭圆的像素,应用于图像以设置像素的颜色。

考虑以下示例。我在SO:

上使用上一个问题中的函数calculateEllipse.m
%# some image
I = imread('pout.tif');
sz = size(I);

%# ellipse we would like to draw directly on image matrix
[x,y] = calculateEllipse(100,50, 150,50, 30, 100);

%# lets show the image, and plot the ellipse (overlayed).
%# note how ellipse have floating point coordinates, 
%# and also have points outside the image boundary
figure, imshow(I)
hold on, plot(x,y, 'LineWidth',2)
axis([-50 250 -50 300]), axis on

%# create mask for image pixels inside the ellipse polygon
BW = poly2mask(x,y,sz(1),sz(2));

%# get the perimter of this mask
BW = bwperim(BW,8);

%# use the mask to index into image
II = I;
II(BW) = 255;
figure, imshow(II)

ellipse_drawn_on_screen ellipse_drawn_on_image

这可以为您提供更好的结果,只需舍入xy的坐标(加上它为我们处理超出边界的点)。请务必阅读POLY2MASK的算法部分,了解它在亚像素级别的工作原理。


编辑:

如果您正在处理RGB图像(3D矩阵),同样适用,您只需要更改我们使用二进制掩码的最后一部分:

%# color of the ellipse (red)
clr = [255 0 0];                %# assuming UINT8 image data type

%# use the mask to index into image
II = I;
z = false(size(BW));
II( cat(3,BW,z,z) ) = clr(1);   %# R channel
II( cat(3,z,BW,z) ) = clr(2);   %# G channel
II( cat(3,z,z,BW) ) = clr(3);   %# B channel
figure, imshow(II)

这是另一种方式:

%# use the mask to index into image
II = I;
BW_ind = bsxfun(@plus, find(BW), prod(sz(1:2)).*(0:2));
II(BW_ind) = repmat(clr, [size(BW_ind,1) 1]);
figure, imshow(II)

pillsetc.png

答案 1 :(得分:3)

您可以将坐标向量传递给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来读取文件。

文件交换中还有this组m文件,这些文件已经非常接近你所需要的。

最后,这是一种在指定精度内获得所需内容的简单方法:

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的演员可能是不必要的。

如果每个Nx3点都有N个RGB值矩阵:

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
相关问题