2015-11-08 6923 views
5

我需要使用python獲得下面圖像的輪廓座標(x,y)的矩陣。使用python獲取圖像中形狀的輪廓(x,y)座標

enter image description here

我嘗試用OpenCV的精明探測器和發現的輪廓,但我得到了很多的輪廓,我不知道如何讓一個我想要的。

import numpy as np 
from matplotlib import pyplot as plt 
import cv2 
#from skimage import measure, feature, io 
#from skimage import img_as_ubyte 

x1 = 330 
xf = 690 
y1 = 0 
yf = 400 

img = cv2.imread('test.tif') 
img = img[y1:yf, x1:xf] 
edge = cv2.Canny(img, 100, 200) 

image, contours, hierarchy = cv2.findContours(edge, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 

enter image description here

我只需要與(X,Y)座標的數組輪廓。我認爲這是在cv2.findContours()輪廓輸出的,但我沒有找到我想要的輪廓......

我也試圖與matplotlib.pyplot.contour功能:

import cv2 
import numpy as np 
from matplotlib import pyplot as plt 

img = cv2.imread('test.tif', 0) # read image 
img = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU)[1] # threshold image 
img = cv2.medianBlur(img, 15) # remove noise 

# skeletonize 
size = np.size(img) # get number of pixels 
skel = np.zeros(img.shape, np.uint8) # create an array of zeros with the same shape as the image and 256 gray levels 

element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) # create a structurant element (cross) 
done = False 

while(not done): 
    eroded = cv2.erode(img, element) 
    temp = cv2.dilate(eroded, element) 
    temp = cv2.subtract(img, temp) 
    skel = cv2.bitwise_or(skel, temp) 
    img = eroded.copy() 
    zeros = size - cv2.countNonZero(img) 
    if zeros == size: 
     done = True 

cs = plt.contour(skel, 1) 
p = cs.collections[0].get_paths()[0] 
v = p.vertices 
x = v[:, 0] 
y = v[:, 1] 

enter image description here

但我一直封閉的輪廓,而不是從圖像的左側到右側的開放輪廓。

非常感謝您的回答。

+0

「輪廓」是什麼意思?你的預期產出是多少?請明確點。 – barny

+0

感謝您的評論@ barny。通過輪廓我的意思是一條線從左到右,並通過強度梯度最大的點。我需要矩陣中該行的(x,y)座標。 – razoool

回答

2

你幾乎找到了你的問題的答案。首先,邊緣檢測輪廓檢測是有區別的。基本上,邊緣檢測會導致您稱之爲(不正確)「開放輪廓」(即邊緣)和輪廓檢測導致您稱之爲「閉合輪廓」(即輪廓)。

The Canny邊緣檢測是一種流行的邊緣檢測算法。由於您想要以(x,y)座標從圖像的左側到右側的陣列形式檢測邊緣,因此Canny邊緣檢測是個不錯的主意。

答案是edge它不是想要的格式。

import numpy as np 
import matplotlib.pyplot as plt 
import cv2 

img = cv2.imread('test.tif') 
edge = cv2.Canny(img, 100, 200) 

ans = [] 
for y in range(0, edge.shape[0]): 
    for x in range(0, edge.shape[1]): 
     if edge[y, x] != 0: 
      ans = ans + [[x, y]] 
ans = np.array(ans) 

print(ans.shape) 
print(ans[0:10, :]) 

陣列ans(形狀等於(n, 2))存儲n像素構成,其檢測到的邊緣的(X,Y) - 協調。這是你正在尋找的結果。

這裏就是我在白這些n像素已經繪製的圖像:

enter image description here

我希望這會幫助你。

+0

非常感謝這個詳細的解釋baptiste,這正是我想要的! – razoool

+0

不客氣!快樂的編碼。 – Flabetvibes