我有许多黑白图像,并希望将它们转换为一组线,这样我就可以从这些线完全或至少接近完全重建原始图像。换句话说,我正在尝试将图像矢量化为一组线。
我已经看过HoughLinesTransform,但是它并没有覆盖图像的每个部分,而是更多关于在图像中查找线条,而不是将图像完全转换为线条表示。另外,线变换不对线的实际宽度进行编码,这让我猜测如何重建图像(我需要这样做,因为这是训练机器学习算法的前一步)。
到目前为止,我已经使用houghLineTransform尝试了以下代码:
importnumpyasnpimportcv2MetersPerPixel=0.1defloadImageGray(path):img=(cv2.imread(path,0))returnimgdefLineTransform(img):edges=cv2.Canny(img,50,150,apertureSize=3)minLineLength=10maxLineGap=20lines=cv2.HoughLines(edges,1,np.pi/180,100,minLineLength,maxLineGap)returnlines;defsaveLines(liness):img=np.zeros((2000,2000,3),np.uint8)forlinesinliness:forx1,y1,x2,y2inlines:print(x1,y1,x2,y2)img=cv2.line(img,(x1,y1),(x2,y2),(0,255,0),3)cv2.imwrite('houghlines5.jpg',img)defmain():img=loadImageGray("loadtest.png")lines=LineTransform(img)saveLines(lines)main()
但是,当使用以下方法进行测试时
我得到了这张图片:
如您所见,它丢失了未与轴对齐的线,并且如果您仔细观察,即使检测到的线也被分割为2条线,并且它们之间有一定间隔。我还必须以预设的宽度绘制这些图像,而实际宽度未知。
编辑:根据@MarkSetchell的建议,我通过使用以下代码尝试了pypotrace,目前它在很大程度上忽略了贝塞尔曲线,只是试图像它们是直线一样工作,稍后我将重点讨论该问题,但是现在结果不是' t最优:
defTraceLines(img):bmp=potrace.Bitmap(bitmap(img))path=bmp.trace()lines=[]i=0forcurveinpath:forsegmentincurve:print(repr(segment))ifsegment.is_corner:c_x,c_y=segment.c
c2_x,c2_y=segment.end_point
lines.append([[int(c_x),int(c_y),int(c2_x),int(c2_y)]])else:c_x,c_y=segment.c1
c2_x,c2_y=segment.end_point
i=i+1returnlines
这会产生这种图像,这是一种改进,但是,虽然可以在以后解决圆的问题,但正方形的缺失部分和其他直线上的怪异伪像更成问题。有人知道如何解决它们吗?关于如何获得线宽的任何提示?
有人对如何更好地解决此问题有任何建议吗?
编辑编辑:这是另一张测试图像:,它包含多个我要捕获的线宽。
解决方案
OpenCV的
使用OpenCVfindContours,drawContours可以首先对线条进行矢量化处理,然后精确地重新创建原始图像:
importnumpyasnpimportcv2
img=cv2.imread('loadtest.png',0)result_fill=np.ones(img.shape,np.uint8)*255result_borders=np.zeros(img.shape,np.uint8)# the '[:-1]' is used to skip the contour at the outer border of the imagecontours=cv2.findContours(img,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[0][:-1]# fill spaces between contours by setting thickness to -1cv2.drawContours(result_fill,contours,-1,0,-1)cv2.drawContours(result_borders,contours,-1,255,1)# xor the filled result and the borders to recreate the original imageresult=result_fill^result_borders# prints True: the result is now exactly the same as the originalprint(np.array_equal(result,img))cv2.imwrite('contours.png',result)
结果
Scikit图片
使用scikit-image的,find_contours并approximate_polygon允许您通过逼近多边形来减少行数(基于此示例):
importnumpyasnpfromskimage.measureimportapproximate_polygon,find_contoursimportcv2
img=cv2.imread('loadtest.png',0)contours=find_contours(img,0)result_contour=np.zeros(img.shape+(3,),np.uint8)result_polygon1=np.zeros(img.shape+(3,),np.uint8)result_polygon2=np.zeros(img.shape+(3,),np.uint8)forcontourincontours:print('Contour shape:',contour.shape)# reduce the number of lines by approximating polygonspolygon1=approximate_polygon(contour,tolerance=2.5)print('Polygon 1 shape:',polygon1.shape)# increase tolerance to further reduce number of linespolygon2=approximate_polygon(contour,tolerance=15)print('Polygon 2 shape:',polygon2.shape)contour=contour.astype(np.int).tolist()polygon1=polygon1.astype(np.int).tolist()polygon2=polygon2.astype(np.int).tolist()# draw contour linesforidx,coordsinenumerate(contour[:-1]):y1,x1,y2,x2=coords+contour[idx+1]result_contour=cv2.line(result_contour,(x1,y1),(x2,y2),(0,255,0),1)# draw polygon 1 linesforidx,coordsinenumerate(polygon1[:-1]):y1,x1,y2,x2=coords+polygon1[idx+1]result_polygon1=cv2.line(result_polygon1,(x1,y1),(x2,y2),(0,255,0),1)# draw polygon 2 linesforidx,coordsinenumerate(polygon2[:-1]):y1,x1,y2,x2=coords+polygon2[idx+1]result_polygon2=cv2.line(result_polygon2,(x1,y1),(x2,y2),(0,255,0),1)cv2.imwrite('contour_lines.png',result_contour)cv2.imwrite('polygon1_lines.png',result_polygon1)cv2.imwrite('polygon2_lines.png',result_polygon2)
结果
Python输出:
Contourshape:(849,2)Polygon1shape:(28,2)Polygon2shape:(9,2)Contourshape:(825,2)Polygon1shape:(31,2)Polygon2shape:(9,2)Contourshape:(1457,2)Polygon1shape:(9,2)Polygon2shape:(8,2)Contourshape:(879,2)Polygon1shape:(5,2)Polygon2shape:(5,2)Contourshape:(973,2)Polygon1shape:(5,2)Polygon2shape:(5,2)Contourshape:(224,2)Polygon1shape:(4,2)Polygon2shape:(4,2)Contourshape:(825,2)Polygon1shape:(13,2)Polygon2shape:(13,2)Contourshape:(781,2)Polygon1shape:(13,2)Polygon2shape:(13,2)
outline_lines.png:
多边形1_lines.png:
多边形2_lines.png:
The length of the lines can then be calculated by applying Pythagoras' theorem to the coordinates: line_length = math.sqrt(abs(x2 - x1)**2 + abs(y2 - y1)**2). If you want to get the width of the lines as numerical values, take a look at the answers of "How to determine the width of the lines?" for some suggested approaches.