日韩性视频-久久久蜜桃-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ò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美一级在线免费 | 亚洲一区二区三区四区五区午夜 | 午夜一二三 | 艳妇乳肉豪妇荡乳av | 久久9999久久免费精品国产 | 久久国产精品无码网站 | 国产伦精品一区二区三区视频孕妇 | 久久激情综合网 | 软萌小仙自慰喷白浆 | 在线观看日韩精品 | 成人在线精品 | 一级欧美视频 | 久久涩 | 奇米888一区二区三区 | 国产精品视频在线播放 | 99日精品 | 久久精品国产亚洲AV无码男同 | 日本视频中文字幕 | 91色吧| 欧美亚洲一区二区在线观看 | 国产一区二区av | wwwjizzzcom| 精精国产 | 国产二级视频 | 欧美色xxxxx 日本精品一区二区三区四区的功能 | 亚洲色欧美另类 | 国产精品自拍偷拍视频 | 中文字幕久久综合 | 成年人黄色在线观看 | 天堂俺去俺来也www 欧美大片在线播放 | 超碰97在线资源站 | www.五月婷婷 | 久久福利精品 | 谁有免费黄色网址 | 蜜臀av88| 五月天在线观看 | 欧美日韩3p | 午夜插插插 | 60分钟| 日韩欧美手机在线 | 欧美bbbbbbbbbbbb18av| 亚洲欧美另类中文字幕 | av网站网址| 男女激情啪啪 | 激情视频在线观看免费 | 国产精品日日摸夜夜爽 | 欧美黄色大片在线观看 | 97在线观视频免费观看 | 日批视频免费看 | 亚洲国产成人精品一区二区三区 | 97超碰人人模人人人爽人人爱 | 国产午夜大地久久 | 在线播放91灌醉迷j高跟美女 | 少妇人妻在线视频 | 99re这里都是精品 | 精品福利一区 | 国产精品不卡 | 成人免费黄色 | 波多野结衣av中文字幕 | 人人澡人人插 | 中文资源在线观看 | 91老师片黄在线观看 | 欧美成人吸奶水做爰 | 日本女人一区二区三区 | 丁香婷婷六月天 | 天堂av资源在线观看 | 成人av手机在线观看 | av黄色网址 | 精品国产无码在线观看 | 久草资源站 | 三女同志亚洲人狂欢 | 精品人妻一区二区三区久久 | 欧美精品国产一区二区 | 日本精品在线看 | 午夜在线视频播放 | 色偷偷av | 亚洲成人网络 | 天堂俺去俺来也www久久婷婷 | 五月天久久久 | 久久精品视频一区二区三区 | 一级黄色免费观看 | 亚洲国产影视 | 亚洲一区国产一区 | 天堂网8| 毛片在线看网站 | 欧美久久久久久久久久久久 | 黑人操中国女人视频 | 久久99精品久久久久久水蜜桃 | 日本理论片中文字幕 | 日本久久综合 | 国产乱码精品一区二区三区忘忧草 | 国产精品--色哟哟 | av动漫在线免费观看 | 色一情一区二 | 欧美视频一区 | 狠狠插影院| 精品一区二区精品 | 欧美日韩影院 | 尤物视频在线 |