发布于2019-11-14 13:02 阅读(5355) 评论(0) 点赞(30) 收藏(1)
我需要用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]
但是我只有闭合的轮廓,而没有从图像的左到右的开放轮廓。
非常感谢你的回答。
您几乎找到了问题的答案。首先,边缘检测和轮廓检测之间存在差异。从根本上说,边缘检测会导致您所谓的(不正确)“开放轮廓”(即边缘),轮廓检测会导致您所谓的“封闭轮廓”(即轮廓)。
的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
像素上用白色绘制的图像:
我希望这能帮到您。
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/153601/5842c8b18bccd534287c/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!