日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python画黑白线条_将黑白图像完全转换为一组线(也称为仅使用线进行矢量化)...

發(fā)布時(shí)間:2024/1/18 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python画黑白线条_将黑白图像完全转换为一组线(也称为仅使用线进行矢量化)... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我有許多黑白圖像,并希望將它們轉(zhuǎn)換為一組線,這樣我就可以從這些線完全或至少接近完全重建原始圖像。換句話說,我正在嘗試將圖像矢量化為一組線。

我已經(jīng)看過HoughLinesTransform,但是它并沒有覆蓋圖像的每個(gè)部分,而是更多關(guān)于在圖像中查找線條,而不是將圖像完全轉(zhuǎn)換為線條表示。另外,線變換不對(duì)線的實(shí)際寬度進(jìn)行編碼,這讓我猜測(cè)如何重建圖像(我需要這樣做,因?yàn)檫@是訓(xùn)練機(jī)器學(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)使用以下方法進(jìn)行測(cè)試時(shí)

我得到了這張圖片:

如您所見,它丟失了未與軸對(duì)齊的線,并且如果您仔細(xì)觀察,即使檢測(cè)到的線也被分割為2條線,并且它們之間有一定間隔。我還必須以預(yù)設(shè)的寬度繪制這些圖像,而實(shí)際寬度未知。

編輯:根據(jù)@MarkSetchell的建議,我通過使用以下代碼嘗試了pypotrace,目前它在很大程度上忽略了貝塞爾曲線,只是試圖像它們是直線一樣工作,稍后我將重點(diǎn)討論該問題,但是現(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

這會(huì)產(chǎn)生這種圖像,這是一種改進(jìn),但是,雖然可以在以后解決圓的問題,但正方形的缺失部分和其他直線上的怪異偽像更成問題。有人知道如何解決它們嗎?關(guān)于如何獲得線寬的任何提示?

有人對(duì)如何更好地解決此問題有任何建議嗎?

編輯編輯:這是另一張測(cè)試圖像:,它包含多個(gè)我要捕獲的線寬。

解決方案

OpenCV的

使用OpenCVfindContours,drawContours可以首先對(duì)線條進(jìn)行矢量化處理,然后精確地重新創(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画黑白线条_将黑白图像完全转换为一组线(也称为仅使用线进行矢量化)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。