Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

画像処理の基礎1

画像を表示する

f:id:aa_debdeb:20151022015109j:plain
/**
* Display Image
*
* @author aa_debdeb
* @date 2015/10/23
*/
size(512, 512);
PImage image = loadImage("lena.jpg");
image(image, 0, 0);  

画像に色をつける

f:id:aa_debdeb:20151022015436j:plain
/**
* Tint Image
*
* @author aa_debdeb
* @date 2015/10/23
*/
PImage image;

void setup(){
  size(512, 512);
  image = loadImage("lena.jpg");
  image(image, 0, 0);
}

void draw(){
  tint((float(mouseY) / height) * 255);
  image(image, 0, 0);
}

画像の色を取得する

f:id:aa_debdeb:20151022015633j:plain
/**
* Get Color in Image
*
* @author aa_debdeb
* @date 2015/10/23
*/
PImage image;

void setup(){
  size(512, 512);
  noStroke();
  image = loadImage("lena.jpg");
}

void draw(){
  image(image, 0, 0);
  fill(255, 255, 255);
  rect(width/2, 0, width, height);
  fill(get(mouseX, mouseY));
  rect(width/2, 0, width, height);
}

モザイクにする

f:id:aa_debdeb:20151022015817j:plain
/**
* Sampled And Quntized Image
*
* @author aa_debdeb
* @date 2015/10/23
*/
PImage image;
int samplingWidth = 10;

void setup(){
  size(512, 512);
  noStroke();
  image = loadImage("lena.jpg");
  image(image, 0, 0);

  loadPixels();
  for(int x = 0; x < width; x += samplingWidth){
    for(int y = 0; y < height; y += samplingWidth){
      color c = pixels[y * width + x];
      fill(c);
      rect(x, y, samplingWidth, samplingWidth);   
    }
  }
}

白黒化する

f:id:aa_debdeb:20151022015941j:plain
/**
* Convert to Monochrome Image
*
* @author aa_debdeb
* @date 2015/10/23
*/
PImage image;

void setup(){
  size(512, 512);
  noStroke();
  image = loadImage("lena.jpg");
  image(image, 0, 0);

  loadPixels();
  for(int x = 0; x < width; x += 1){
    for(int y = 0; y < height; y += 1){
      color c = pixels[y * width + x];
      float grey = 0.30 * red(c) + 0.59 * green(c) + 0.11 * blue(c);
      pixels[y * width + x] = color(grey, grey, grey);
    }
  }
  updatePixels();
}

2値化する

f:id:aa_debdeb:20151022020039j:plain
/**
* Binarization of Image
*
* @author aa_debdeb
* @date 2015/10/23
*/
PImage image;

void setup(){
  size(512, 512);
  noStroke();
  image = loadImage("lena.jpg");
}

void draw(){
  image(image, 0, 0);
  loadPixels();
  for(int x = 0; x < width; x += 1){
    for(int y = 0; y < height; y += 1){
      color c = pixels[y * width + x];
      float grey = 0.30 * red(c) + 0.59 * green(c) + 0.11 * blue(c);
      if(grey > (float(mouseY) / height) * 255){
        pixels[y * width + x] = color(255);
      } else {
        pixels[y * width + x] = color(0);      
      }
    }
  }
  updatePixels();
}