python画黑白线条_将黑白图像完全转换为一组线(也称为仅使用线进行矢量化)...
我有許多黑白圖像,并希望將它們轉(zhuǎn)換為一組線,這樣我就可以從這些線完全或至少接近完全重建原始圖像。換句話說,我正在嘗試將圖像矢量化為一組線。
我已經(jīng)看過HoughLinesTransform,但是它并沒有覆蓋圖像的每個部分,而是更多關(guān)于在圖像中查找線條,而不是將圖像完全轉(zhuǎn)換為線條表示。另外,線變換不對線的實際寬度進行編碼,這讓我猜測如何重建圖像(我需要這樣做,因為這是訓(xùn)練機器學(xué)習(xí)算法的前一步)。
到目前為止,我已經(jīng)使用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()
但是,當(dāng)使用以下方法進行測試時
我得到了這張圖片:
如您所見,它丟失了未與軸對齊的線,并且如果您仔細觀察,即使檢測到的線也被分割為2條線,并且它們之間有一定間隔。我還必須以預(yù)設(shè)的寬度繪制這些圖像,而實際寬度未知。
編輯:根據(jù)@MarkSetchell的建議,我通過使用以下代碼嘗試了pypotrace,目前它在很大程度上忽略了貝塞爾曲線,只是試圖像它們是直線一樣工作,稍后我將重點討論該問題,但是現(xiàn)在結(jié)果不是' t最優(yōu):
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
這會產(chǎn)生這種圖像,這是一種改進,但是,雖然可以在以后解決圓的問題,但正方形的缺失部分和其他直線上的怪異偽像更成問題。有人知道如何解決它們嗎?關(guān)于如何獲得線寬的任何提示?
有人對如何更好地解決此問題有任何建議嗎?
編輯編輯:這是另一張測試圖像:,它包含多個我要捕獲的線寬。
解決方案
OpenCV的
使用OpenCVfindContours,drawContours可以首先對線條進行矢量化處理,然后精確地重新創(chuàng)建原始圖像:
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)
結(jié)果
Scikit圖片
使用scikit-image的,find_contours并approximate_polygon允許您通過逼近多邊形來減少行數(shù)(基于此示例):
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)
結(jié)果
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.
總結(jié)
以上是生活随笔為你收集整理的python画黑白线条_将黑白图像完全转换为一组线(也称为仅使用线进行矢量化)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用css属性,使div上下左右移动
- 下一篇: CTO离职前悄悄传了我一招,和我说吃透跳