【发布时间】:2016-02-09 03:45:49
【问题描述】:
我需要用python获取下图轮廓坐标(x,y)的矩阵。
我尝试使用 opencv canny 检测器并找到轮廓,但我得到了很多轮廓,但我不知道如何获得我想要的。
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)
我只需要一个包含轮廓 (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]
但我只有闭合轮廓,而不是从图像左侧到右侧的开放轮廓。
非常感谢您的回答。
【问题讨论】:
-
“轮廓”是什么意思?你的预期输出是什么?具体一点。
-
感谢您的评论@barny。我所说的等高线是指一条从左到右并穿过强度梯度最大的点的线。我需要矩阵中这条线的(x,y)坐标。
标签: python opencv numpy matplotlib