【问题标题】:Axis coordinates to pixel coordinates? (Matlab)轴坐标到像素坐标? (Matlab)
【发布时间】:2011-12-04 23:53:28
【问题描述】:

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

【问题讨论】:

  • 你需要生成什么样的情节?散点图、线等。另外,你的数据是什么形式的? X 和 & 向量、Nx2 矩阵等
  • 数据集[X,Y]是可以组成椭圆的点坐标。我可以把椭圆画出来,但是现在我很难把所有的坐标都放进去图像(像素坐标)。我应该在图像中绘制椭圆。
  • @Ivy:我根据您的评论发布了答案..

标签: image matlab pixel coordinate


【解决方案1】:

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

还有来自 File Exchange 的 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

“精度”参数决定了浮点值的小数位数,从而决定了图像的分辨率和精度。 uint32 的强制转换可能是不必要的。

如果每个N 点都有一个Nx3 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

【讨论】:

  • 非常感谢。我认为您的资源对我很有帮助。
  • 我已经使用上面的代码测试了我的椭圆。但我不理解'round(x*precision) + 1'。这段代码有什么作用?什么是“+1”?是某种转换吗?
  • 减去最小值后,新的最小值为零。一旦我们准备好使用xy 作为索引,我们需要添加1,因为MATLAB 索引来自1
  • 我注释了代码块,希望它更清楚。如有必要,查找并替换哈希字符。
【解决方案2】:

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

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

考虑下面的例子。我正在使用来自上一个问题的函数calculateEllipse.m SO:

%# 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)

这应该会给您提供比简单地舍入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)

【讨论】:

  • 如何修改上面的'BW'来控制蒙版的颜色,我打算使用RGB值。
  • @Ivy:这只是索引的问题,见我上面的编辑(一个使用逻辑索引,其他线性索引)
猜你喜欢
  • 1970-01-01
  • 2020-02-29
  • 2014-06-18
  • 2014-06-18
  • 1970-01-01
  • 2023-04-06
  • 2015-12-31
  • 2020-06-10
  • 1970-01-01
相关资源
最近更新 更多