Image Processing Code

Here is the Processing code used for converting an image into a continuous line segment:

import processing.dxf.*;

PImage img; // Initial image
int res = 2; // Resolution
//PImage imgPost; // Image that has been modified
int[][] index; // 2D array that lists all of the indices of the numbers in the picture
int count;
double[][] pixelVals;
Vertex[] vertices;
PrintWriter gCodeFile; // File the GCode is written to. 
boolean recordDXF;
void setup(){
 size(512, 512, P2D);
 
 background(255);
 img = loadImage("C:/Users/Jack/Documents/Research/HangBot/ImageProcessing/ImgProcessingApp/Lenna/Lenna_Black.png"); // Load the image
 stroke(0); // Set the stroke to black
 // Set up the window 
 image(img, 0, 0, 512, 512); // Display the image
 filter(POSTERIZE, 10); // Posterize the image into the number of levels that matches the set resolution
 img = get();
 // Sets up the array to make it the width and height of the image to be processed. 
 index = new int[img.height][img.width];
 count = 0; // Sets the starting pixel to zero
 
 simplifyImage();
 vertices = new Vertex[(img.height)*(img.width)]; // Initializes the size of the list of vertices
 createVerts(); // Creates the sequence that the lines will be recorded in
 displayLines(); // Draws the lines on the screen
 
 gCodeFile = createWriter("C:/Users/Jack/Documents/Research/HangBot/ImageProcessing/ImgProcessingApp/Lenna/Lenna_Black.txt");
 writeGCode(); // Outputs the file of GCode commands 
 recordDXF = true;
}

void draw(){
 background(255);
 //image(imgPost, 0, 0, 550, 750);
 displayLines(); // Draws the lines on the screen
}
// Establishes the 2D index array 
void initIndex(){
 for(int i = 0; i < img.height; i++){
 for(int j = 0; j < img.width; j++){
 index[i][j] = count;
 //println(count);
 count++;
 } 
 }
}
// This method turns the array of pixels into a more simplified version 
// where the 
void simplifyImage(){ 
 for(int i = 0; i < img.width; i++){
 for(int j = 0; j < img.height; j++){
 
 color setColor = get(i, j);
 
 for(int row = 0; row <= res; row++){
 for(int col = 0; col <= res; col++){
 set(i+row, j+col, setColor);
 //println((red(setColor) + green(setColor) + blue(setColor))/3);
 }
 }
 j = j + res;
 }
 i = i + res;
 }
}
void createVerts(){
 boolean fwd = true;
 int pos = 0;
 int dirY = 1;
 //println(pixVal(-100, -100));
 for(int row = 0; row < img.height; row++){
 if(fwd){ 
 for(int col = 0; col < img.width; col ++){
 double dY = pixVal(col, row);
 vertices[pos] = new Vertex(col, (row + dY*dirY));
 //println(pixVal(col, row));
 dirY = dirY * -1;
 col += res;
 pos++;
 }
 row += res;
 fwd = false;
 }
 else{
 for(int col = (img.width -1); col >= 0; col --){
 double dY = pixVal(col, row);
 vertices[pos] = new Vertex(col, (row + dY*dirY));
 //println(pixVal(col, row));
 dirY = dirY * -1;
 col -= res;
 pos++;
 }
 row += res;
 fwd = true;
 }
 }
}
// Returns the vertical displacement of the pixel passed into the function 
double pixVal(int xCoord, int yCoord){
 /*
 double blueCol = 255 - blue(img.get(xCoord, yCoord));
 double greenCol = 255 - green(img.get(xCoord, yCoord));
 double redCol = 255 - red(img.get(xCoord, yCoord));
 println (blueCol + " " + greenCol + " " + redCol);
 
 double colorVal = (redCol + greenCol + blueCol)/3; // The color of the requested pixel
 */
 double colorVal = 255 - brightness(color(img.get(xCoord, yCoord)));
 double ratio = colorVal/255; // The ratio of the color to the total possible
 double vertDisp = ratio * res * 3; 
 
 return vertDisp; 
 
}
void displayLines(){
 // Export DXF
 if(recordDXF){
 beginRaw(DXF, "C:/Users/Jack/Documents/Research/HangBot/ImageProcessing/ImgProcessingApp/Lenna/Lenna_Black.dxf");
 }
 
 strokeWeight(1);
 stroke(0);
 beginShape(LINES);
 vertex((float)vertices[0].getX(), (float)vertices[0].getY());
 for(int i = 0; i < vertices.length; i++){
 Vertex temp = vertices[i];
 
 if(temp != null){
 //line((float)temp.xVal, (float)temp.yVal, (float)temp2.xVal, (float)temp2.yVal);
 
 vertex((float)temp.getX(), (float)temp.getY());
 
 vertex((float)temp.getX(), (float)temp.getY());
 }
 }
 vertex(width, height);
 endShape();
 
 if(recordDXF){
 endRaw();
 recordDXF = false;
 }
 
}
void writeGCode(){
 for(int i = 0; i < vertices.length; i++){
 Vertex temp = vertices[i];
 if(temp != null){
 
 double realX = temp.getX() - (img.width/2);
 double realY = -(temp.getY() - (img.height/2));
 String xCom = "X" + String.valueOf(realX);
 String yCom = "Y" + String.valueOf(realY);
 
 gCodeFile.println(xCom + " " + yCom);
 
 }
 }
 
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s