2016-04-17 50 views
-2

我有黑白图像。当我在图像查看器中查看此图像的特定coordinate (x,y)时,我可以看到它的值为0。但是,当我想从我的脚本中获得(x,y)的价值时,我获得了255。代码看起来如下:在matlab中确定黑色像素为白色

bw = imread('my_map.png'); 
    imshow(bw); 
    hold on 
    % find corners of obstacles 
    corners = detectHarrisFeatures(bw); 
    plot(corners.selectStrongest(50)); 
    cornerPoints = corners.selectStrongest(50); 
    hold on 
    % determine line's equation for two particular corners 
    m = cornerPoints.Location(4,2)-cornerPoints.Location(3,2); 
    n = cornerPoints.Location(4,1)-cornerPoints.Location(3,1); 
    k = (m)/(n); 
    b = cornerPoints.Location(3,2) - k*cornerPoints.Location(3,1); 

    %determine if this line intersects any obstacle 
    black = 0; 
    white = 0; 
    for y=cornerPoints.Location(3,2):1:cornerPoints.Location(4,2) 

     x = (y-b)/k; 
     if (int16(x) == 0) 
      x = cornerPoints.Location(3,1); 
     end 
     plot(int16(x),int16(y),'r*') 
     hold on 
     c = bw(int16(x), int16(y)); 
     if (c==255) 
      white=white+1; 

     else 
      black=black+1; 
     end 
    end 
    if (white == 0) 
     display('valid') 

    else if (black <= 2) 
     display('valid') 
    else 
     display('invalid') 
    end 

形象是这个

this one

可能是什么问题?

+0

我建议你添加一些代码。否则,我不认为你会得到答案。 – Lukasz

+1

您可能需要注意坐标系统的原点。还要记住,MATLAB使用基于1的索引 – Amro

回答

0

在Matlab中,矩阵的第一个坐标表示一个行索引,第二个坐标表示一个列索引。

因此,为了获得一个矩阵M,即用行索引y和列索引x的点(X,Y),你需要写:

M(y,x) 

在你的情况,你应该写:

c = bw(int16(y), int16(x)); 

代替:

c = bw(int16(x), int16(y)); 
+0

[x和y交换后的外观](https://www.dropbox.com/s/ud1wo0o1o8iwg07/%D0%A1%D0%BA%D1%80%D0 %B8%D0%BD%D1%88%D0%BE%D1%82%202016-04-18%2000.48.52.png?DL = 0)。出了点问题。红线必须从蓝星到红星。 –

+0

再次,它是x和y值之间的混合 - 查看线条的(x,y)坐标以及红色和蓝色星形的(x,y)坐标,它们彼此完全相反。 – drorco

相关问题