使用OpenCV的Canny+霍夫线变换提取图形轮廓遇问题求方案
图像转几何表示(WKT):Canny+霍夫线变换的优化问题
我希望将图像转换为几何表示形式(最终目标为WKT字符串,支持任何几何编码格式)。目前采用OpenCV的Canny边缘检测方法提取边缘,再通过概率霍夫线变换(Hough Line Transform)检测直线。尽管尝试调整Canny和霍夫变换的各类参数,仍无法得到满意结果。
实现代码
Canny边缘检测部分
import numpy as np import cv2 as cv import requests from matplotlib import pyplot as plt img_link = 'https://i.ibb.co/pwTwWfg/13581.jpg' resp = requests.get(img_link) arr = np.asarray(bytearray(resp.content), dtype=np.uint8) img = cv.imdecode(arr, -1) img = cv.GaussianBlur(img, (15, 15), 0) assert img is not None, "file could not be read, check with os.path.exists()" edges = cv.Canny(img,100,120, apertureSize=3) plt.subplot(121),plt.imshow(img,cmap = 'gray') plt.title('Original Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(edges,cmap = 'gray') plt.title('After Canny'), plt.xticks([]), plt.yticks([]) plt.show()
原始图像与Canny边缘检测结果对比图
概率霍夫线变换部分
linesP = cv.HoughLinesP(edges, 1, np.pi / 360, 3, None, 50, 10) cdstP = cv.cvtColor(edges, cv.COLOR_GRAY2BGR) if linesP is not None: for i in range(0, len(linesP)): l = linesP[i][0] cv.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0,0,255), 3, cv.LINE_AA) plt.imshow(cdstP) plt.show()
霍夫线变换应用于边缘图的结果图
当前存在的问题
- 霍夫线变换未能检测到全部边缘;
- 图形内部的细节区域也被识别为边缘。
已尝试的调整
调整Canny的孔径尺寸、高斯核大小,以及霍夫变换的投票阈值、分辨率等参数,但均无法完美解决问题,且不同图像的最优参数差异较大。
请问是否遗漏了关键处理环节?是否存在更优的实现方法?
内容的提问来源于stack exchange,提问作者DecafTron
相关产品推荐
相关产品推荐

