這是一張jpg的位圖等龙,所謂位圖督笆,就是由像素組成的原始圖片芦昔,而CAD是由點(diǎn)線面組成的矢量圖。
一娃肿、設(shè)定繪圖環(huán)境咕缎,導(dǎo)入所需的包:
import os
os.chdir(r'C:\Users\super.000\Desktop')
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import skimage
from skimage import io,feature,color,measure
二、定義CAD繪圖函數(shù):
def plotLine(line):
s = 'pline\n'
for i in line:
x,y = i
s +='%s,%s\n'%(y,-x)
s += ' \n'
return s
三料扰、定義位圖轉(zhuǎn)矢量函數(shù):
def imContours(im,level):
im_gray = color.rgb2gray(im)
im_contours = measure.find_contours(im_gray,level)
plt.figure(figsize=(12,8))
plt.title('level is :%s'%level)
for line in im_contours:
x,y = line[:,0],line[:,1]
plt.plot(y,-x)
return im_contours
四凭豪、手工刪選出最合適的level匹配值:
im = io.imread('dog.jpg')
for level in np.arange(0,1,0.1):
imContours(im,level)
五、打印輸出轉(zhuǎn)換的CAD位圖:
with open('1.txt','w') as f:
for line in imContours(im,0.3):
f.write(plotLine(line))
效果如下: