We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › help with converting 2d image to 3d shape
Pages: 1 2 
help with converting 2d image to 3d shape (Read 7620 times)
help with converting 2d image to 3d shape
Jun 15th, 2010, 12:38pm
 
I know that I can use explode or extrude to get height data and can use this data to make 3d pixel sized blocks.  The problem is that then I have 10,000(or whatever) blocks.
Is there a way to merge these blocks into one watertight shape?  Or is there a better way to get a 2-manifold shape from 2d image information?

What I'm doing is writing a sketch which has a white background.  Onto this background, I'll draw in black.  I'll then extrude the black areas into a 3d shape(the white will be voids).  I'll then export the 3d shape as a STL and have it 3D printed.
I greatly appreciate all the help I've gotten here.  Your generosity has helped me to learn a lot.
Re: help with converting 2d image to 3d shape
Reply #1 - Jun 15th, 2010, 3:48pm
 
maybe this is what you are looking for, not sure though.
you can load an image, or create your own PGraphics to extrude the mesh... you need peasycam for 3dview and unlekkerlib for stl export. its useful though, so i left it in there


Code:
//demo creating a simple mesh using a heightmap from an gif image
// change the height by moving the mouse and export pressing a key

import peasy.*;
import unlekker.data.*;
boolean record;
PeasyCam cam;

PImage img ;


int meshSize = 5;
int resX = 100;
int resY = 100;
float[][] val = new float[resX][resY];

void setup() {
size(900,600,P3D);
smooth();
background(255);
img = loadImage("heightmap.gif");
//img = loadImage("heightmap2.gif");
cam = new PeasyCam(this, 0,0,0,600);



}

void draw() {

translate(-resX/2*meshSize,-resY/2*meshSize);



for(int x =0; x<resX; x++){
for(int y =0; y<resY; y++){
val[x][y] = brightness(img.get(x,y))*mouseX/width;
}
}

background(0);
if (record) {
beginRaw("unlekker.data.STL",millis()+".stl");
}


for(int x =0; x<resX-1; x++){
for(int y =0; y<resY-1; y++){
beginShape();
colorMode(HSB,255);
fill( val[x][y],255,255);

vertex(x*meshSize,y*meshSize,val[x][y] );
vertex((x+1)*meshSize,y*meshSize,val[x+1][y] );
vertex((x+1)*meshSize,(y+1)*meshSize,val[x+1][y+1] );
vertex(x*meshSize,(y+1)*meshSize,val[x][y+1] );


endShape(CLOSE);
}

}


if (record) {
endRaw();
record = false;
}

}


void keyPressed() {
record = true;
}








Re: help with converting 2d image to 3d shape
Reply #2 - Jun 15th, 2010, 4:23pm
 
You might also take a look at my TerrainSteering example on OpenProcessing.org, which uses the Terrain class of toxiclibs in order to create a fully water tight 3D mesh (with all sides and bottom filled). You can also export the resulting mesh as binary STL like this:

Code:
mesh.saveAsSTL(sketchPath("terrain.stl")); 



In order to help you populating the terrain with image data you can use the code snippet below (parts of which will be part of the next library release as well):

Code:

// elevation of highest point (white pixels in the image)
float maxElevation=1000;
img=loadImage("test/terrain.png");
terrain=new Terrain(img.width, img.height, 50);
// use blue channel for elevation
// (doesn't matter if bitmap is greyscale)
float[] el=getNormalizedArray(img.pixels, 0, 255, 255,maxElevation);
terrain.setElevation(el);
// turn into water tight mesh
// (0 = ground level, can be set to negative values too)
mesh = terrain.toMesh(0);
mesh.saveAsSTL("terrain.stl");

/**
* Creates a normalized version of the values of the given int[] array.
* Supports packed integers (e.g. ARGB data) by allowing to specify a
* bitshift amount & bitmask.
*
* @param input source data
* @param bits number of bits to right shift each value
* @param mask bitmask to apply after bitshifting
* @param peak peak value (in the source domain) to normalize against
* @param target target peak of normalized values
*/
float[] getNormalizedArray(int[] input, int bits, int mask, int peak, float target) {
 float invPeak = target / peak;
 float[] normalized = new float[input.length];
 for (int i = 0; i < input.length; i++) {
   int val = input[i];
   if (bits > 0) {
     val >>= bits;
   }
   val &= mask;
   normalized[i] = val * invPeak;
 }
 return normalized;
}
Re: help with converting 2d image to 3d shape
Reply #3 - Jun 15th, 2010, 4:40pm
 
I appreciate the help, and so fast.  That is not quite what I need, thought.
Three things:
1 your sketch truncated the image on the bottom and right.
2 your sketch produced a mesh that included the white space, which should be eliminated.
3 the mesh was not a closed solid, just a mesh surface.

thank you for the help, though
Re: help with converting 2d image to 3d shape
Reply #4 - Jun 15th, 2010, 4:43pm
 
Robert, these points were surely only directed to Cedric's solution though, or? Did you try the other one?
Re: help with converting 2d image to 3d shape
Reply #5 - Jun 15th, 2010, 4:56pm
 
Toxi,
Thank you.  Your terrain is great but not what I'm after, I'm afraid.  Mine is harder.
Take a look at this image http://www.itsshameful.com/heightmap.gif
My hope is to make a printable STL utilizing only the black portion.  If it's not black it won't exist, if it's black it will be a watertight, printable solid.  I just don't know how to cut out the white space and cap the black.
I know I'm being a pest, I'm sorry for that.  I do appreciate the help.
Re: help with converting 2d image to 3d shape
Reply #6 - Jun 15th, 2010, 5:38pm
 
I see, then a terrain approach is really not suitable here, but despair not... I'd reckon a bit of volumeutils will sort you out here: Smiley

Code:
/**
* Loads a B&W image and extrudes black pixels into
* a watertight 3D mesh.
* @author toxi
*
* Dependencies: toxiclibscore-0018, volumeutils-0004
* (or newer, available from: http://toxiclibs.org/ )
*/
import toxi.geom.*;
import toxi.geom.mesh.*;
import toxi.volume.*;

int scale=8;
int depth=32;

PImage img=loadImage("heightmap.gif");
Vec3D worldSize = new Vec3D(img.width, img.height, depth).scale(scale);
VolumetricSpace volume = new VolumetricSpace(worldSize, img.width, img.height, depth);
VolumetricBrush brush = new RoundBrush(volume, scale);
for (int y = 0; y < img.height; y ++) {
 for (int x = 0; x < img.width; x ++) {
   if (0 == (img.pixels[y * img.width + x] & 0xff)) {
     for (int z = 0; z < depth; z++) {
       brush.drawAtGridPos(x, y, z, 0.5f);
     }
   }
 }
}
volume.closeSides();
TriangleMesh mesh = new IsoSurface(volume).computeSurfaceMesh(null, 0.1f);
mesh.saveAsSTL(sketchPath("test.stl"), true);
exit();


Btw. You might also like to try the BoxBrush instead of RoundBrush (might give cleaner results). Simply change this one line:

Code:
VolumetricBrush brush = new BoxBrush(volume, scale); 

Re: help with converting 2d image to 3d shape
Reply #7 - Jun 15th, 2010, 5:56pm
 
Ah Toxi,
You're wonderful.  That sketch is great.  I hesitate to say...it didn't quite do it.  It made the shape perfectly, but didn't close the bottom.  I'm wondering if it would work to mirror the shape to close it.  T.aking your valuable time to help a stranger is greatly appreciated
Re: help with converting 2d image to 3d shape
Reply #8 - Jun 15th, 2010, 6:22pm
 
Hmm.. that'd be weird though and doesn't make much sense (sorry to say too)! Using your image above, the exported model in Meshlab looks fine from both sides (top on left, bottom on right):

...
...
Re: help with converting 2d image to 3d shape
Reply #9 - Jun 15th, 2010, 6:41pm
 
Yes, I see the problem.  I looked at it in mimimagics and saw the problem.  Looked ok in meshlab, when I tried that...then I discovered the problem.  The lighting in meshlabs is bad.    You're looking at rendered "smooth" use "flat lines" and you can see that the bottom is open.
Re: help with converting 2d image to 3d shape
Reply #10 - Jun 15th, 2010, 6:52pm
 
We may be getting different resuls.  Here is the file it exported for me:
itsshameful.com/test.stl
and here is a screenshot from magics
itsshameful.com/magics.jpg
Re: help with converting 2d image to 3d shape
Reply #11 - Jun 15th, 2010, 6:55pm
 
I think this needs further inquiry, but maybe better over here as a issue/ticket: http://hg.postspectacular.com/toxiclibs/issues

I don't think it's Meshlab though, the face & vertex normals all look correct and the same result (ok) in Blender...

...

But obviously I'd like to find out why Minimagics sees this differently! A bit worrisome! Any chance to upload your STL file somewhere for me to grab?
Re: help with converting 2d image to 3d shape
Reply #12 - Jun 15th, 2010, 7:00pm
 
another point....Rhino couldn't open it.
Re: help with converting 2d image to 3d shape
Reply #13 - Jun 15th, 2010, 7:06pm
 
Hmm... your file defo is clipped, also in ML. Did you make any changes to the code at all? Please try my exported model:

https://dev.postspectacular.com/forums/imagemesh.stl.zip

Also, are you using the latest versions of my libs?
Re: help with converting 2d image to 3d shape
Reply #14 - Jun 15th, 2010, 7:15pm
 
yeah....
the problem's on my end, that model looks perfect.
no I didn't change the code.  I thought I had the latest release, I'm so new.  I'll download the latest and see what that does.  I'm sorry for the bother.
Pages: 1 2