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.
Page Index Toggle Pages: 1
Extrusin (Read 1979 times)
Extrusin
Nov 30th, 2009, 8:09am
 
I want to make say extrusions from a white picture image with different shapes. I figured to make things easier I would use certain colors on the backgrounds to indicate which shape to use. From those shapes the parameters for the height and length or the diameter can be found and I can extrude them all from the white background

I just need to find the different heights and widths of the different shapes so that I can add them to a parameter and extrude them from the picture in another library.
Re: Extrusin
Reply #1 - Nov 30th, 2009, 9:32am
 
Do you really mean extrusion (making a 3D shape out of a 2D one) or do you mean extraction (making a sub-image from a larger one)?
Re: Extrusin
Reply #2 - Dec 2nd, 2009, 4:29pm
 
I'm learning the Processing library for several uses one of them is to use with a separate 3D library. What I was planning on doing is creating a BG and behind tht BG i would create an equal sized white background in which I would draw black rectangles to represent plat-forms and then extrude them from the white background. What I need to know is both the position of the black box on the picture as well as the size of box(widthxheight)
Re: Extrusin
Reply #3 - Dec 3rd, 2009, 2:47am
 
Well, then that's really extrusion, so it is more a 3D question than a syntax one...
Re: Extrusin
Reply #4 - Dec 3rd, 2009, 5:16am
 
so even though i'm attempting to find the position and parameters of a picture of a black rectangle drawn to a white background I should still look into the 3D library even though i'm not using the actual extrusion feature in the processing library?
Re: Extrusin
Reply #5 - Dec 3rd, 2009, 5:35am
 
Well, if all you need is to detect coordinates of a rectangular shape, I think you confused us (well, me, at least) by making the goal a bit fuzzy (or so is my own feeling).

Note that's still not a syntax question, more an algorithmic question.
But well, looks like people read the section description (if they do) as "Generic Questions about the Processing language" instead of "Questions about the Processing Language" Smiley
That or the fact that's the first section with "Questions" in the description...

Anyway, back to your question.
Should I have to do that, I think I would scan the pixels[] array by increasing index. As soon as I find a pixel of the wanted color, I have the top-left corner. So I can continue to scan to find the first next white pixel, to find the right coordinate, then go down along the black side up to the next white pixel (or end of image, of course) to find the bottom side.

Not sure if I am clear enough...
Re: Extrusin
Reply #6 - Dec 3rd, 2009, 5:57am
 
lol my apologies for being unclear

o.0 uhm its somewhat clear lol. I assume I would start by looking into the  Pixel Array Demo and attempt to derive what you said to what the demo explains in code

thank you
Re: Extrusin
Reply #7 - Dec 31st, 2009, 9:54pm
 
hmmm i have looked at the pixel array demo code and slightly modified it i'm not exactly sure of the best logical algorithmic approach to implement i've tried a couple but haven't received the desired results

Code:

 if (signal > width*height-1) {
System.out.println("Image Successfully Scanned");
System.exit(0);  
 } else if(signal>newWidth){
 System.out.println("Row "+row+" Done");
 row++;
 signal=newWidth;
 newWidth+=width;
 }
 
//To detect first pixel of picture without error
if(signal==0){
 if(getCol()!=-1){

 }
 }
//Is the scanner currently on a square
else if(getCol()!=-1){
//is this the first black of the square
if(getPrevCol()==-1){
 System.out.println("This is the first black");
}
//Is the scanner currently in the square
else{ System.out.println("this is black");}
 }  
 //Is the last pixel of the width of the box
 else if(getPrevCol()!=-1){
 System.out.println("This is first white");
 }
//During
 else{System.out.println("spacer");}
Re: Extrusin
Reply #8 - Jan 4th, 2010, 10:00am
 
if it helps basically i'm trying to use processing to get the position, height and length of the rectangles in the picture to use as parameters

...
Re: Extrusin
Reply #9 - Jan 4th, 2010, 11:13am
 
You probably can use one of the blob decetion libraries to do that .

http://v3ga.net/processing/BlobDetection/

http://s373.net/code/flob/flob.html
Re: Extrusin
Reply #10 - Jan 5th, 2010, 9:25am
 
Blob detection might be an overkill, detecting rectangles is quite easy, much easier than generic shape contour finding.
I successfully detected the given rectangles, once I cleaned up the image: don't use Jpeg format when you need precision! I used PNG instead, so I avoid artifacts (gray pixels).
Here is the code:
Code:
PImage rects;
ArrayList foundRectangles = new ArrayList();
int MARGIN = 50;
boolean bShowAlgo = false;
float depth = 400;
float volume;

void setup()
{
if (bShowAlgo)
{
size(550, 300);
}
else
{
size(800, 800, P3D);
}
rects = loadImage("testpicture1.png");
volume = rects.width * rects.height * 5;
FindRectangles(rects, #FFFFFF, #000000);

if (bShowAlgo)
{
image(rects, MARGIN, MARGIN);
rects.updatePixels();
image(rects, 300, MARGIN);
noLoop();
}
}

void draw()
{
if (bShowAlgo)
{
background(#ABCDEF);
noFill();
stroke(#00FF00);
for (int i = 0; i < foundRectangles.size(); i++)
{
Rectangle r = (Rectangle) foundRectangles.get(i);
rect(MARGIN + r.x, MARGIN + r.y, r.width, r.height);
}
}
else
{
// Inspired by CubicGrid example
background(255);
image(rects, 0, 0);

// Center and spin result
translate(width/2, height/2, -depth);
rotateY(frameCount * 0.01);
rotateX(frameCount * 0.01);

fill(200, 90);
stroke(#0000FF);
DrawBox(0, 0, MARGIN, rects.width, rects.height, MARGIN);

fill(128, 80);
stroke(#008800, 70);
for (int i = 0; i < foundRectangles.size(); i++)
{
Rectangle r = (Rectangle) foundRectangles.get(i);
float boxHeight = volume / r.width / r.height;
DrawBox(r.x, r.y, 0, r.width, r.height, boxHeight);
}
}
}

// It changes img, pass a copy if you need it intact...
void FindRectangles(PImage img, color backColor, color rectColor)
{
int iw = img.width;
int ih = img.height;
println("Image: " + iw + "x" + ih);

color markColor = #FF0000;

// Top-left coordinate
int left, top;
// Botttom-right coordinate
int right, bottom;
// Current pos
int posX = 0, posY = 0;
while (posY < ih)
{
while (posX < iw)
{
if (img.pixels[posX + iw * posY] == rectColor)
{
// Top-left corner
left = posX;
top = posY;
// Walk the upper side
while (posX < iw && img.pixels[posX + iw * posY] == rectColor)
{
posX++;
}
right = posX - 1;
// Walk the right side, marking it (and the left side too)
while (posY < ih && img.pixels[right + iw * posY] == rectColor)
{
img.pixels[left + iw * posY] = img.pixels[right + iw * posY] = markColor;
posY++;
}
bottom = posY - 1;
posY = top;
Rectangle r = new Rectangle(left, top, right - left, bottom - top);
foundRectangles.add(r);
println(r);
}
else if (img.pixels[posX + iw * posY] == markColor)
{
// A known rectangle, skip it
do
{
posX++;
} while (img.pixels[posX + iw * posY] != markColor);
posX++;
}
else
{
posX++;
}
}
posX = 0;
posY++;
}
}

// Boxes seems to be drawn centered (not documented?)
// Draw a box with reference on a corner
void DrawBox(float x, float y, float z, float w, float h, float d)
{
pushMatrix();
translate(-x - w / 2, -y - h / 2, -(z - d / 2));
box(w, h, d);
popMatrix();
}

I first wrote the detection algorithm (quite "brute force", but it works), you can see that with bShowAlgo to true.
Then I tried to do my first own 3D Processing sketch... I had some trouble before finding out that boxes are drawn in center mode, which isn't documented...
For varied effects, I made boxes of constant volume...
Re: Extrusin
Reply #11 - Jan 5th, 2010, 2:04pm
 
Nice solution!
Re: Extrusin
Reply #12 - Jan 5th, 2010, 8:49pm
 
yeah this is great thx a lot PhiLho this is exactly what i'm looking for!!
Page Index Toggle Pages: 1