2016-04-17 54 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

相關問題