Advanced Video Processing

From Robert-Depot
Jump to: navigation, search

This course covers topics in computer vision, procedural/generative video, high-resolution video, and media presentation through the use of computational tools. It is also a programming course.

histogram / auto-contrast

processing code

PImage b, c;
int x,y,n,w;
int win_width;
int win_height;
int y_corner;

// parameters of auto-contrast window
int ac_center=50;
int ac_width=200;

void setup() {
  size(screen.width, screen.height);//, 800);
  //size(1200, 900);
  //b=loadImage("01_06_western landscape.jpg");
  //b=loadImage("/home/robert/Desktop/Photos/03_washington dc bedroom 2.jpg");
  //b=loadImage("C:\\Swap\\Photos\\03_washington dc bedroom 2.jpg");
  b=loadImage("C:\\Swap\\Photos\\01_06_western landscape.jpg");
  win_height=int(b.height*0.4);
  win_width=int(b.width*0.4);//win_height/height*width);
  y_corner=0;//int((b.height-win_height)/2);
  //noLoop();
  x=0;
  y=0;
  w=0;
  n=0;
  frameRate(24);
}

void auto_contrast(PImage in) {
  // adjust contrast in place on input image
  in.loadPixels();
  // loop over pixels to find min and max Intensity
  int dimension = (in.width*in.height);
  int [] hist = new int[256];
  for(int i=0;i<dimension;i++) {
      color argb=in.pixels[i];

      int bin_a = argb & 0xFF000000;
      int r = (argb >> 16) & 0xFF;  // Faster way of getting red(rgb)
      int g = (argb >> 8) & 0xFF;   // Faster way of getting green(rgb)
      int b = argb & 0xFF;          // Faster way of getting blue(rgb)

      int intensity=int((r+g+b)/3);
      
      hist[intensity]++;
  };
  
  int i_min=0;
  
  int sum=0;
  while(sum<4) {
    i_min++;
    sum+=hist[i_min];
  };
  
  int i_max=256;
  sum=0;
  while(sum<4) {
    i_max--;
    sum+=hist[i_max];
  };
  
  // loop over pixels to scale RGB to min and max Intensity
  for(int i=0;i<dimension;i++) {
      color argb=in.pixels[i];
      
      //int a = (argb >> 24) & 0xFF;
      int bin_a = argb & 0xFF000000;
      int r = (argb >> 16) & 0xFF;  // Faster way of getting red(rgb)
      int g = (argb >> 8) & 0xFF;   // Faster way of getting green(rgb)
      int b = argb & 0xFF;          // Faster way of getting blue(rgb)

//      int ac_min=constrain(ac_center-(ac_width>>1), 0, 255);
//      int ac_max=constrain(ac_center+(ac_width>>1), 0, 255);
//      r=int(constrain(map(r, ac_min, ac_max, 0, 255), 0, 255));
//      g=int(constrain(map(g, ac_min, ac_max, 0, 255), 0, 255));
//      b=int(constrain(map(b, ac_min, ac_max, 0, 255), 0, 255));
      
      r=int(constrain(map(r, i_min, i_max, 0, 255), 0, 255));
      g=int(constrain(map(g, i_min, i_max, 0, 255), 0, 255));
      b=int(constrain(map(b, i_min, i_max, 0, 255), 0, 255));
      
      //a = a << 24;
      r = r << 16;
      g = g << 8;
      b = b;
      color new_rgb = bin_a | r | g | b;
      in.pixels[i]= new_rgb;
  };
  
  in.updatePixels();
};

void draw() {
  
  c = b.get(x, y+y_corner, win_width, win_height);

  auto_contrast(c);

  blend(c, 0, 0, win_width, win_height, 0, 0, width, height, BLEND);
  //blend(b, x, y+y_corner, (win_width), (win_height), 0, 0, width, height, BLEND);
  
  //filter(ERODE);
  //saveFrame("/Volumes/Swimming Pool/Processing/west-#####.tif");
  saveFrame("C:\\Data\\01_06_western landscape/01_06_western landscape-#####.tif");
  //println(x+":"+b.width+" "+y+":"+b.height);
  
  // update motion
  n++;
  x+=int(random(0, 5));
  if(x+win_width>b.width) exit();
  y=int(random(0,10));
  w=int(random(-5,0));
}

void mouseMoved() {
  ac_center = int(255.0*float(mouseX)/float(width));
  ac_width= int(255.0*float(mouseY)/float(height));
}