如何在matlab中将图像的像素坐标x和y转换为图像?

时间:2015-01-21 05:54:38

标签: matlab image-processing coordinates pixel

我有一组向量x和y,它包含图像的像素坐标。我需要使用Matlab将这些值转换为图像。我该如何使用这些值?哪个功能最适合它?我使用的代码是:

I = imread('D:\majorproject\image\characters\ee.jpg');

imshow(I)

BW =im2bw(I);

BW=imcomplement(BW);

imshow(BW)

dim = size(BW);

col = round(dim(2)/2)-90;

row = find(BW(:,col), 1 );

boundary = bwtraceboundary(BW,[row, col],'N');

imshow(I)

hold on;

plot(boundary(:,2),boundary(:,1),'0','LineWidth',3);

BW_filled = imfill(BW,'holes');

boundaries = bwboundaries(BW_filled);

for k=1:10

   b = boundaries{k};

   plot(b(:,2),b(:,1),'g','LineWidth',3);
end

我从中得到了坐标值。 谢谢。

2 个答案:

答案 0 :(得分:5)

避免使用循环,并考虑使用sub2ind来索引输出图像。 sub2ind会将(x,y)坐标转换为线性索引,以便您可以使用单个命令索引所需内容:

img = false(size(I));
img(sub2ind(size(I), y, x)) = true;
imshow(img);

此处xy表示坐标,假设它们从1开始。如果x和{ {1}}是坐标,只需交换输入参数:

y

此外,img = false(size(I)); img(sub2ind(size(I), x, y)) = true; imshow(img); 是您使用I读入的图片。由于您希望拥有与imread尺寸相同的图像,我们当然可以利用这一事实来创建输出图像。


或者,您可以使用sparse直接索引到矩阵并将位置设置为1,然后转换回I full矩阵:

logical

答案 1 :(得分:2)

因为你没有提供像素的强度,我假设你想创建一个你可以轻松做到的二进制图像 -

 img=[];
 for k=1:length(x) //assuming the length of x and y are same
     img(x(k),y(k))=1;
 end
 imshow(img);