如何将轴坐标转换为像素坐标?我有一组数据,其中包括负值和浮点值,我需要将所有数据放入图像中。但是像素坐标都是正整数。如何解决负面问题?
最佳答案
据我了解,您有一组代表椭圆的点,您可以将它们直接绘制在图像矩阵内(而不只是在屏幕上显示)。
为此,您可以使用 POLY2MASK 函数将椭圆转换为二进制掩码。然后通过计算它的 perimeter ,这将为我们提供一个仅表示构成椭圆的像素的二进制掩码,将其应用于图像以设置像素的颜色。
考虑下面的例子。我正在使用上一个问题中的函数 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)
与简单地舍入 x
和 y
的坐标相比,这应该会给您带来更好的结果(此外,它还为我们处理了越界点)。请务必阅读 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)
关于image - 轴坐标到像素坐标? (Matlab),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7721255/