使用代码将任何 2D 图像变成可 3D 打印的雕塑
Turn any 2D image into 3D printable sculpture with code
我正在尝试仅使用代码将 2D 图像转换为 3D 可打印雕塑。首先我想知道是否可以只用一个脚本来完成?我已经知道 Python 和 C,如果我可以使用其中之一来做我想做的事,当然会很棒。
这里有两个链接让你明白我所说的 "Turn any 2D image into 3D printable sculpture" 是什么意思(但这些都是使用软件):
https://www.youtube.com/watch?v=ngZwibfaysc
https://www.youtube.com/watch?v=-fe2zxcKSic
更具体地说,我想插入一张图片,然后等待获得 3D 雕塑的结果。
有点好奇所以我编码了一个照明表面编码的小例子
- 对于输入图像的每个像素
height = (color_intensity)*scale
这是我用测试的输入图像(Google搜索中的第一张不错的油画):
这是结果(点云3D预览)
左边是 gif 动画,所以 reload/refresh 页面可以查看动画是否已经停止,或者下载 gif 并以比 brownser 更合适的方式打开以进行 gif 预览...右边是彩色的点云预览(静态图片)
这是用于计算的 C++ 代码:
OpenGLtexture zed,nx,ny,nz; // height map,normal maps (just 2D images)
picture pic; // source image
int x,y,a;
// resize textures to source image size
zed.resize(pic.xs,pic.ys);
nx.resize(pic.xs,pic.ys); float *pnx=(float*) nx.txr;
ny.resize(pic.xs,pic.ys); float *pny=(float*) ny.txr;
nz.resize(pic.xs,pic.ys); float *pnz=(float*) nz.txr;
// prepare tmp image for height map extraction
picture pic0;
pic0=pic; // copy
pic0.rgb2i(); // grayscale
// this computes the point cloud (this is the only important stuff from this code)
// as you can see there are just 3 lines of code important from all of this
for (a=0,y=0;y<pic.ys;y++)
for (x=0;x<pic.xs;x++,a++)
zed.txr[a]=pic0.p[y][x].dd>>3; // height = intensity/(2^3)
// compute normals (for OpenGL rendering only)
double n[3],p0[3],px[3],py[3];
int zedx,zedy,picx,picy;
for (a=zed.xs,zedy=-(pic.ys>>1),picy=1;picy<pic.ys;picy++,zedy++)
for (a++, zedx=-(pic.xs>>1),picx=1;picx<pic.xs;picx++,zedx++,a++)
{
vector_ld(p0,zedx-1,zedy ,-zed.txr[a -1]); // 3 neighboring points
vector_ld(py,zedx ,zedy-1,-zed.txr[a+zed.xs ]);
vector_ld(px,zedx ,zedy ,-zed.txr[a ]);
vector_sub(px,p0,px); // 2 vectors (latices of quad/triangle)
vector_sub(py,p0,py);
vector_mul(n,px,py); // cross product
vector_one(n,n); // unit vector normalization
pnx[a]=n[0]; // store vector components to textures
pny[a]=n[1];
pnz[a]=n[2];
}
此处OpenGL预览代码(C++):
scr.cls(); // clear buffers
scr.set_perspective(); // set camera matrix
glMatrixMode(GL_MODELVIEW); // set object matrix
rep.use_rep();
glLoadMatrixd(rep.rep);
// directional (normal shading)
float lightAmbient [4]={0.20,0.20,0.20,1.00};
float lightDiffuse [4]={1.00,1.00,1.00,1.00};
float lightDirection[4]={0.00,0.00,+1.0,0.00};
glLightfv(GL_LIGHT1,GL_AMBIENT ,lightAmbient );
glLightfv(GL_LIGHT1,GL_DIFFUSE ,lightDiffuse );
glLightfv(GL_LIGHT1,GL_POSITION,lightDirection);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glEnable(GL_COLOR_MATERIAL);
// render point cloud
int zedx,zedy,picx,picy,a;
glColor3f(0.7,0.7,0.7);
float *pnx=(float*)nx.txr;
float *pny=(float*)ny.txr;
float *pnz=(float*)nz.txr;
glBegin(GL_POINTS);
for (a=zed.xs,zedy=-(pic.ys>>1),picy=1;picy<pic.ys;picy++,zedy++)
for (a++, zedx=-(pic.xs>>1),picx=1;picx<pic.xs;picx++,zedx++,a++)
{
//glColor4ubv((BYTE*)&pic.p[picy][picx].dd); // this is coloring with original image colors but it hides the 3D effect
glNormal3f(pnx[a],pny[a],pnz[a]); // normal for lighting
glVertex3i(zedx ,zedy ,-zed.txr[a]); // this is the point cloud surface point coordinate
}
glEnd();
scr.exe(); // finalize OpenGL calls and swap buffers ...
scr.rfs();
矩阵设置如下:
// gluProjection parameters
double f=100; //[pixels] focus
scr.views[0].znear= f; //[pixels]
scr.views[0].zfar =1000.0+f; //[pixels]
scr.views[0].zang = 60.0; //[deg] view projection angle
scr.init(this); // this compute the Projection matrix and init OpenGL
// place the painting surface in the middle of frustrum
rep.reset();
rep.gpos_set(vector_ld(0.0,0.0,-0.5*(scr.views[0].zfar+scr.views[0].znear)));
rep.lrotx(180.0*deg); // rotate it to match original image
[备注]
我用的是自己的照片class 所以这里有一些会员:
xs,ys
图像大小(以像素为单位)
p[y][x].dd
是在(x,y)位置的像素为32位整数类型
p[y][x].db[4]
是色带 (r,g,b,a) 的像素访问
我还使用自定义 OpenGl scr
和纹理类:
xs,ys
缓冲区大小(以像素为单位)
Texture::txr
为32位像素指针(图像分配为线性一维数组)
- 高度图用于存储int值
- normal maps用于存储float法向量分量
唯一要做的就是:
- 根据自己的喜好过滤点云
- triangulate/export 到您的打印机支持的网格
还有其他方法可以将光照编码到表面:
你可以做类似菲涅尔透镜表面
- 所以将网格划分为段
- 并分别偏移,使其从同一参考平面开始(z 偏移)
那需要的少得多volume/material
前半段动画是正常的高度编码然后切换到菲涅耳表面encoding/packing进行比较
不是将光照编码为高度图,而是编码为粗糙度图
- 每个像素点都会被映射成小的子高度图
- 平面高illumination/intensity颜色
- 粗糙的表面是黑色的
- 中间是灰色阴影
从一定角度也能看到它,而且相对较薄,因此需要很少的 material(比上一个项目符号少得多)
真实高度图(真实3D网格表示)
这是非常棘手的,你需要标准化颜色、阴影和照明伪影,所以只剩下正常的阴影(因为表面来自单一 material、颜色、光泽度、粗糙度......)然后才提取高度图。为此你需要很多东西,比如分割、自适应阈值处理、过滤等等……最后添加空的内部并添加支撑墙,这样网格就可以保持在一起 while/after 打印。
我正在尝试仅使用代码将 2D 图像转换为 3D 可打印雕塑。首先我想知道是否可以只用一个脚本来完成?我已经知道 Python 和 C,如果我可以使用其中之一来做我想做的事,当然会很棒。
这里有两个链接让你明白我所说的 "Turn any 2D image into 3D printable sculpture" 是什么意思(但这些都是使用软件):
https://www.youtube.com/watch?v=ngZwibfaysc
https://www.youtube.com/watch?v=-fe2zxcKSic
更具体地说,我想插入一张图片,然后等待获得 3D 雕塑的结果。
有点好奇所以我编码了一个照明表面编码的小例子
- 对于输入图像的每个像素
height = (color_intensity)*scale
这是我用测试的输入图像(Google搜索中的第一张不错的油画):
这是结果(点云3D预览)
左边是 gif 动画,所以 reload/refresh 页面可以查看动画是否已经停止,或者下载 gif 并以比 brownser 更合适的方式打开以进行 gif 预览...右边是彩色的点云预览(静态图片)
这是用于计算的 C++ 代码:
OpenGLtexture zed,nx,ny,nz; // height map,normal maps (just 2D images)
picture pic; // source image
int x,y,a;
// resize textures to source image size
zed.resize(pic.xs,pic.ys);
nx.resize(pic.xs,pic.ys); float *pnx=(float*) nx.txr;
ny.resize(pic.xs,pic.ys); float *pny=(float*) ny.txr;
nz.resize(pic.xs,pic.ys); float *pnz=(float*) nz.txr;
// prepare tmp image for height map extraction
picture pic0;
pic0=pic; // copy
pic0.rgb2i(); // grayscale
// this computes the point cloud (this is the only important stuff from this code)
// as you can see there are just 3 lines of code important from all of this
for (a=0,y=0;y<pic.ys;y++)
for (x=0;x<pic.xs;x++,a++)
zed.txr[a]=pic0.p[y][x].dd>>3; // height = intensity/(2^3)
// compute normals (for OpenGL rendering only)
double n[3],p0[3],px[3],py[3];
int zedx,zedy,picx,picy;
for (a=zed.xs,zedy=-(pic.ys>>1),picy=1;picy<pic.ys;picy++,zedy++)
for (a++, zedx=-(pic.xs>>1),picx=1;picx<pic.xs;picx++,zedx++,a++)
{
vector_ld(p0,zedx-1,zedy ,-zed.txr[a -1]); // 3 neighboring points
vector_ld(py,zedx ,zedy-1,-zed.txr[a+zed.xs ]);
vector_ld(px,zedx ,zedy ,-zed.txr[a ]);
vector_sub(px,p0,px); // 2 vectors (latices of quad/triangle)
vector_sub(py,p0,py);
vector_mul(n,px,py); // cross product
vector_one(n,n); // unit vector normalization
pnx[a]=n[0]; // store vector components to textures
pny[a]=n[1];
pnz[a]=n[2];
}
此处OpenGL预览代码(C++):
scr.cls(); // clear buffers
scr.set_perspective(); // set camera matrix
glMatrixMode(GL_MODELVIEW); // set object matrix
rep.use_rep();
glLoadMatrixd(rep.rep);
// directional (normal shading)
float lightAmbient [4]={0.20,0.20,0.20,1.00};
float lightDiffuse [4]={1.00,1.00,1.00,1.00};
float lightDirection[4]={0.00,0.00,+1.0,0.00};
glLightfv(GL_LIGHT1,GL_AMBIENT ,lightAmbient );
glLightfv(GL_LIGHT1,GL_DIFFUSE ,lightDiffuse );
glLightfv(GL_LIGHT1,GL_POSITION,lightDirection);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glEnable(GL_COLOR_MATERIAL);
// render point cloud
int zedx,zedy,picx,picy,a;
glColor3f(0.7,0.7,0.7);
float *pnx=(float*)nx.txr;
float *pny=(float*)ny.txr;
float *pnz=(float*)nz.txr;
glBegin(GL_POINTS);
for (a=zed.xs,zedy=-(pic.ys>>1),picy=1;picy<pic.ys;picy++,zedy++)
for (a++, zedx=-(pic.xs>>1),picx=1;picx<pic.xs;picx++,zedx++,a++)
{
//glColor4ubv((BYTE*)&pic.p[picy][picx].dd); // this is coloring with original image colors but it hides the 3D effect
glNormal3f(pnx[a],pny[a],pnz[a]); // normal for lighting
glVertex3i(zedx ,zedy ,-zed.txr[a]); // this is the point cloud surface point coordinate
}
glEnd();
scr.exe(); // finalize OpenGL calls and swap buffers ...
scr.rfs();
矩阵设置如下:
// gluProjection parameters
double f=100; //[pixels] focus
scr.views[0].znear= f; //[pixels]
scr.views[0].zfar =1000.0+f; //[pixels]
scr.views[0].zang = 60.0; //[deg] view projection angle
scr.init(this); // this compute the Projection matrix and init OpenGL
// place the painting surface in the middle of frustrum
rep.reset();
rep.gpos_set(vector_ld(0.0,0.0,-0.5*(scr.views[0].zfar+scr.views[0].znear)));
rep.lrotx(180.0*deg); // rotate it to match original image
[备注]
我用的是自己的照片class 所以这里有一些会员:
xs,ys
图像大小(以像素为单位)p[y][x].dd
是在(x,y)位置的像素为32位整数类型p[y][x].db[4]
是色带 (r,g,b,a) 的像素访问
我还使用自定义 OpenGl scr
和纹理类:
xs,ys
缓冲区大小(以像素为单位)Texture::txr
为32位像素指针(图像分配为线性一维数组)- 高度图用于存储int值
- normal maps用于存储float法向量分量
唯一要做的就是:
- 根据自己的喜好过滤点云
- triangulate/export 到您的打印机支持的网格
还有其他方法可以将光照编码到表面:
你可以做类似菲涅尔透镜表面
- 所以将网格划分为段
- 并分别偏移,使其从同一参考平面开始(z 偏移)
那需要的少得多volume/material
前半段动画是正常的高度编码然后切换到菲涅耳表面encoding/packing进行比较
不是将光照编码为高度图,而是编码为粗糙度图
- 每个像素点都会被映射成小的子高度图
- 平面高illumination/intensity颜色
- 粗糙的表面是黑色的
- 中间是灰色阴影
从一定角度也能看到它,而且相对较薄,因此需要很少的 material(比上一个项目符号少得多)
真实高度图(真实3D网格表示)
这是非常棘手的,你需要标准化颜色、阴影和照明伪影,所以只剩下正常的阴影(因为表面来自单一 material、颜色、光泽度、粗糙度......)然后才提取高度图。为此你需要很多东西,比如分割、自适应阈值处理、过滤等等……最后添加空的内部并添加支撑墙,这样网格就可以保持在一起 while/after 打印。