如何使用Python创建3D模型

哈Ha! 一旦我需要创建底部的3D模型, 本文中的更多内容 。 今天,我想谈谈如何创建Python 3的3D模型。有很多方法可以做到这一点:blender python api,Vpython ...但是我想告诉您如何仅使用Python来创建模型。



链接到Github

STL


为此,您需要了解stl格式(流行的3D文件格式)的工作方式。
此格式的整个模型包含许多三角形,因此文件包含其顶点的3维坐标。

STL文件
实心的
构面正常0 0 0
外循环
顶点0 0 0
顶点1 0 0
顶点1 1 0
端环
端面
构面正常0 0 0
外循环
顶点1 1 0
顶点0 0 0
顶点0 1 0
端环
端面
终固

例子


我想展示如何通过照片中像素的亮度来实现3D模型的创建。 我在下面拍了这张照片。



使用opencv库处理图像(略微模糊,以免亮度出现急剧跳变)。

import cv2 import numpy as np cd_1=['0', '0', '0']#      1  cd_2=['0', '0', '0']#      2  cd_3=['0', '0', '0']#      3  file_stl='new.stl' file_im=r'C:\Users\allex\Pictures\2.jpg'#    op_stl=open(file_stl, 'w') op_im=cv2.imread(file_im) gray = cv2.cvtColor(op_im, cv2.COLOR_BGR2GRAY)#    blur = cv2.GaussianBlur(gray,(0,0),1)#      res=cv2.resize(blur,(320,240))#     320*240 

下面是一个函数,该函数采用3个具有三角形顶点坐标的数组,并将1个三角形面写入文件。

 def face_file_stl(cd_1, cd_2, cd_3): op_stl.write("facet normal 0 0 0") op_stl.write("outer loop") op_stl.write("vertex " + " ".join(cd_1))#    1  op_stl.write("vertex " + " ".join(cd_2))#    2  op_stl.write("vertex " + " ".join(cd_3))#    3  op_stl.write("endloop \n\tendfacet") 

现在最重要的是创建组成3D模型的三角形的顶点的正确坐标。


创建坐标的一段代码。

 for i in range(size.shape[1]):#    for k in range(size.shape[0]-1):#    if i!=size.shape[1]-1: try: #making the first triangls for relief cd_1=[str(i), str(k), str(blur[k, i]) ] cd_2=[str(i + 1), str(k), str(blur[k, i+1]) ] cd_3=[str(i+1), str(k+1), str(blur[k+1,i+1])] except: print('er') face_file_stl(cd_1, cd_2, cd_3) try: #making the second triangls for relief cd_1=[str(i), str(k), str(blur[k, i]) ] cd_2=[str(i+1), str(k+1), str(blur[k+1, i+1])] cd_3=[str(i), str(k+1), str(blur[k+1,i]) ] except: print('er') face_file_stl(cd_1, cd_2, cd_3) 

创建的3D模型
实际上,这就是我想在下一篇文章之前写的全部内容。

整个代码
 import cv2 cd_1=['0', '0', '0']#      1  cd_2=['0', '0', '0']#      2  cd_3=['0', '0', '0']#      3  file_stl='new.stl' file_im=r'C:\Users\allex\Pictures\22.jpg'#    op_stl=open(file_stl, 'w') op_im=cv2.imread(file_im) gray = cv2.cvtColor(op_im, cv2.COLOR_BGR2GRAY)#    blur = cv2.GaussianBlur(gray,(0,0),1)#      blur=cv2.resize(blur,(320,240)) x=0 y=0 file='STL_project-1.stl' o_1="\n\t" o_2="\n\t\t" o_3="\n\t\t\t" op_stl.write("solid") def face_file_stl(cd_1, cd_2, cd_3): op_stl.write(o_1+"facet normal 0 0 0") op_stl.write(o_2 + "outer loop") op_stl.write(o_3 + "vertex " + " ".join(cd_1))#    1  op_stl.write(o_3 + "vertex " + " ".join(cd_2))#    2  op_stl.write(o_3 + "vertex " + " ".join(cd_3))#    3  op_stl.write(o_2 + "endloop \n\tendfacet") #making the first triangls for base for i in range(blur.shape[1]-1): cd_1=[str(i),"0","0"] cd_3=[str(i+1),str(blur.shape[0]-1),"0"] cd_2=[str(i),str(blur.shape[0]-1),"0"] face_file_stl(cd_1, cd_2, cd_3) #making the second triangls for base for i in range(blur.shape[1]-1): cd_1=[str(i+1),str(blur.shape[0]-1),"0"] cd_3=[str(i),"0","0"] cd_2=[str(i+1),"0","0"] face_file_stl(cd_1, cd_2, cd_3) #base has done for i in range(blur.shape[1]): if i%30==0: print(i) for k in range(blur.shape[0]-1):#making the first triangls for relief if i!=blur.shape[1]-1: try: cd_1=[str(i), str(k), str(blur[k, i]) ] cd_2=[str(i + 1), str(k), str(blur[k, i+1]) ] cd_3=[str(i+1), str(k+1), str(blur[k+1,i+1])] except: print('er') face_file_stl(cd_1, cd_2, cd_3) #for j in range(blur.shape[1]-1):#making the second triangls for relief try: cd_1=[str(i), str(k), str(blur[k, i]) ] cd_2=[str(i+1),str(k+1), str(blur[k+1, i+1])] cd_3=[str(i), str(k+1), str(blur[k+1,i]) ] except: print('er') face_file_stl(cd_1, cd_2, cd_3) #relief has done #making the first triangls for right side for i in range(blur.shape[1]): if i!=blur.shape[1]-1: try: cd_1=[str(i),str(blur.shape[0]-1),"0"] cd_3=[str(i+1),str(blur.shape[0]-1),str(blur[blur.shape[0]-1, i+1])] cd_2=[str(i),str(blur.shape[0]-1),str(blur[blur.shape[0]-1, i])] except: print("er") face_file_stl(cd_1, cd_2, cd_3) #making the second triangls for right side try: cd_1=[str(i),str(blur.shape[0]-1),"0"] cd_3=[str(i+1),str(blur.shape[0]-1),"0"] cd_2=[str(i+1),str(blur.shape[0]-1),str(blur[blur.shape[0]-1, i+1])] except: print("er") face_file_stl(cd_1, cd_2, cd_3) if i!=blur.shape[1]-1: try: cd_1=[str(i),'0',"0"] cd_2=[str(i+1),'0',str(blur[0, i+1])] cd_3=[str(i),'0',str(blur[0, i])] except: print("er") face_file_stl(cd_1, cd_2, cd_3) #making the second triangls for right side try: cd_1=[str(i),'0',"0"] cd_2=[str(i+1),'0',"0"] cd_3=[str(i+1),'0',str(blur[0, i+1])] except: print("er") face_file_stl(cd_1, cd_2, cd_3) for i in range(blur.shape[0]): if i!=blur.shape[0]-1: try: cd_1=['0',str(i),"0"] cd_3=['0',str(i+1),str(blur[ i+1,0])] cd_2=['0',str(i),str(blur[i,0])] except: print("er") face_file_stl(cd_1, cd_2, cd_3) #making the second triangls for right side try: cd_1=['0',str(i),"0"] cd_3=['0',str(i+1),"0"] cd_2=['0',str(i+1),str(blur[i+1,0])] except: print("er") face_file_stl(cd_1, cd_2, cd_3) try: cd_2=[str(blur.shape[1]-1),str(i),"0"] cd_3=[str(blur.shape[1]-1),str(i+1),str(blur[ i+1,blur.shape[1]-1])] cd_1=[str(blur.shape[1]-1),str(i),str(blur[i,blur.shape[1]-1])] except: print("er") face_file_stl(cd_1, cd_2, cd_3) #making the second triangls for right side try: cd_2=[str(blur.shape[1]-1),str(i),"0"] cd_3=[str(blur.shape[1]-1),str(i+1),"0"] cd_1=[str(blur.shape[1]-1),str(i+1),str(blur[i+1,blur.shape[1]-1])] except: print("er") face_file_stl(cd_1, cd_2, cd_3) op_stl.write("\nendsolid" ) op_stl.close() print('end') 

Source: https://habr.com/ru/post/zh-CN411899/


All Articles