如何在轴坐标中获取x和y值而不是矩阵位置?

时间:2016-05-28 22:40:27

标签: matlab matrix coordinates

我需要找到二进制图像的x和y坐标。这是代码的示例图片(方形)      fig1     fig2     fig3

And use the codes as below:


 a=imread('square.png');
 b=im2bw(a);
 figure(1)
 imshow(b) %%show image in normal axis
 sz=size(b);
 RI=imref2d(size(b));

 xmin=-sz(2)/2;
 xmax=sz(2)/2;
 ymin=-sz(1)/2;
 ymax=sz(1)/2;
 RI.XWorldLimits=[xmin xmax];
 RI.YWorldLimits=[ymin ymax];
 figure(2) %%show image is desired axes where zero is centered of axes
 imshow(b,RI);
 [x,y]=find(b==0); 
 figure(3)
 scatter(x,y) 

图(1)是原始图像。图(2)是相同的图像,但是在不同的轴上示出(即,零以x和y轴为中心)。然后我寻找感兴趣区域的x和y坐标(在这个例子中,方框)。但是我得到的x和y坐标与图(2)中的轴不一致。根据图(2),方形的x和y坐标应该是-ve值和+ ve值。但是所有x和y坐标都是+ ve值。我看是什么问题然后我注意到[x,y]=find(b==0)的x和y值与它们在矩阵中的位置一致,而不是轴坐标。那么,如果我想在轴坐标中显示x和y,我该怎么办?韩国社交协会。

1 个答案:

答案 0 :(得分:0)

首先,您将use strict; use warnings; # ^- start always your code like this to see errors and what is ambiguous # declare your variables using "my" to specify the scope my $filename = 'seq-at.txt'; # use the 3 parameters open syntax to avoid to overwrite the file: open my $fh, '<', $filename or die "unable to open '$filename' $!"; my %hash; my $hkey = ''; my $hval = ''; while (<$fh>) { chomp; # remove the newline \n (or \r\n) if (/^>/) { # when the line start with ">" # store the key/value in the hash if the key isn't empty # (the key is empty when the first ">" is encountered) $hash{$hkey} = $hval if ($hkey); # store the line in $hval and clear $hkey ($hval, $hkey) = $_; } elsif (/\S/) { # when the line isn't empty (or blank) # append the line to the key $hkey .= $_; } } # store the last key/val in the hash if any $hash{$hkey} = $hval if ($hkey); # display the hash foreach (keys %hash) { print "key: $_\nvalue: $hash{$_}\n\n"; } 的输出反转。它会返回您编写的行而不是 findx的行和列。因此,您实际上应该将输出指定为y

[y,x]

然后,您还要确保展开散点图轴的y方向(使用[y,x] = find(b == 0); ),因为这是axis image(您的其他数字)默认使用的内容。

imshow

<强>更新

如果您想将这些坐标转换为您显示的居中坐标,则需要在调用散点图之前将相同的变换应用于scatter(x,y) axis image x

y
相关问题