2014年4月20日 星期日

SKY2的影像轉換程式


將影像的最亮點找出來,其他的點均刪除,最後轉為ASC檔,利用MESHLAB為PLY檔



 //objects
 PFont f;

 PrintWriter output;

 //colors
 color black=color(0);
 color white=color(255);

 //variables
 int itr;             //iteration
 float pixBright;
 float maxBright=0;
 int maxBrightPos=0;
 int prevMaxBrightPos;
 int row;
 int col;
 int FileCount=1;
 int brightThHold=250;  //ignore the bright point below brightThHold
 int LaserSum=0;
 int LaserNum=0;

 //scanner parameters
 float NumSteps = 271;  //number of steps per revolution
 float AngleLaserCam = 30*PI/180;  //angle between laser and camera [radian]
 float AngleTwoSteps=2*PI/NumSteps;  //angle between 2 steps [radian]

 //coordinates
 float x, y, z;  //cartesian cords., [milimeter]
 float ro;  //first of polar coordinate, [milimeter]
 float fi; //second of polar coordinate, [radian]
 float distance; //distance between brightest pixel and middle of photo [pixel]
 float PixelHor = 5; //pixels per milimeter horizontally 1px=0.2mm
 float PixelVer = 5; //pixels per milimeter vertically 1px=0.2mm

 //================= CONFIG ===================

 void setup() {
   size(640,480);
   strokeWeight(1);
   smooth();
   background(0);

   //fonts
   f=createFont("Arial",16,true);
   //output file
   output=createWriter("RealImage.asc");

 }

 //============== MAIN PROGRAM =================

 void draw() {
    for (itr=0; itr<NumSteps; itr++){
 
     String RealImageFileName="RealImage-"+nf(itr+1, 3)+".png";
 
     PImage RealImage=loadImage(RealImageFileName);
     String LineImageFileName="LineImage-"+nf(itr+1, 3)+".png";
     PImage LineImage=createImage(RealImage.width, RealImage.height, RGB);
     RealImage.loadPixels();
     LineImage.loadPixels();
     int currentPos;
     fi=itr*AngleTwoSteps;
     println(fi);

     for(row=0; row<RealImage.height; row++){  //starting row analysis
     maxBrightPos=0;
     maxBright=0;

       for(col=0; col<RealImage.width; col++){
         currentPos = row * RealImage.width + col;
         pixBright=brightness(RealImage.pixels[currentPos]);
         if(pixBright>maxBright){
           maxBright=pixBright;
           maxBrightPos=currentPos;
           LaserSum=currentPos;
           LaserNum=1;
         }
         if(pixBright==maxBright){
           LaserSum=LaserSum+currentPos;
           LaserNum=LaserNum+1;
         }
         LineImage.pixels[currentPos]=black; //setting all pixels black
       }  // end od a row analysis

      if (maxBright > brightThHold) {
       maxBrightPos=LaserSum/LaserNum;
       LineImage.pixels[maxBrightPos]=white; //setting brightest pixel white
      }
       distance=((maxBrightPos+1-row*RealImage.width)-RealImage.width/2)/PixelHor;
       ro=distance/sin(AngleLaserCam);
 
   
       x=ro * cos(fi);  //changing polar coords to kartesian
       y=ro * sin(fi);
       z=row/PixelVer;
   
       if( (ro>=-30) && (ro<=60) ){              //printing coordinates
         output.println(x + "," + y + "," + z);
       }
   
     }//end of all  row analysis
 
     LineImage.updatePixels();
     LineImage.save(LineImageFileName);
 
   }
   output.flush();
   output.close();

   println("End processing.........");
     noLoop();
 }









2014年4月6日 星期日

SKY2的影像截取程式

一、SKY1僅需步進馬達驅動程式,其他都由免費軟體程行。SKY2從影像截取、分析到3D坐標轉換都要己來,下面是影像截取程式。

二、開發工具:PROCESSING 2.1
                           下載:https://processing.org/download/
        CAM控制:JMyron 0025
                           下載: http://webcamxtra.sourceforge.net/download.shtml

三、影像截取程式

import JMyron.*;

 //objects
 PFont f;

 JMyron cam;

 PrintWriter output;
 int FileCount=1;

 //colors
 color black=color(0);
 color white=color(255);

 //scanner parameters
 float Steps = 271;  //number steps of motor per revolution
 float AngleLaserCamera = 30*PI/180;  //angle between laser and camera [radian]
 float AngleTwoStep=2*PI/Steps;  //angle between 2 step [radian]


 //================= CONFIG ===================

 void setup() {
   size(640,480);
   strokeWeight(1);
   smooth();
   background(0);
 
   //fonts
  f=createFont("Arial",16,true);
  cam = new JMyron();
  cam.start(width, height);
  cam.findGlobs(0);
   
 }

 //============== MAIN PROGRAM =================

 void draw() {
 
   PImage RealImage=createImage(width,height,RGB);
 
   cam.update();
 
   delay(1000);
     cam.update();
     RealImage.loadPixels();
     int[] currFrame = cam.image();
     loadPixels();
     for (int n=0;n<RealImage.width*RealImage.height;n++){
       RealImage.pixels[n]=currFrame[n];
       pixels[n] = currFrame[n];
     }
     RealImage.updatePixels();
     updatePixels();
     String RealImageFileName="RealImage-"+nf(FileCount, 3)+".png";
     RealImage.save(RealImageFileName);

     println(FileCount);
  if (FileCount == Steps ) {
    println("End getting image......");

    //println("End processing.........");
     noLoop();
    }  else {
    FileCount=FileCount+1;
    }

 }

四、部分結果







(準備統測中,其他部分有空再補)