processing 画像とヒストグラムの表示がうまくできない。

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
サンダー

processing 画像とヒストグラムの表示がうまくできない。

#1

投稿記事 by サンダー » 7年前

(1) pic0, pic1を読み込み、画像サイズを640x480ピクセルにする。また、R、G、Bそれぞれのチャンネルの画像を出力する(出力画像はpic0red, pic0green, pic0blue、 pic1red, pic1green, pic1blueとする)。



(2) 画像pic0red, pic0green, pic0blueに対して、それぞれのヒストグラムを表示する。また、ヒストグラムの線の色をそれぞれ白、黄色、空色に設定する。

一応赤色の画像と赤のヒストグラムしか表示します。そこで、青と緑とヒストグラムの線の色
を表示するにはどうすればよいですか?

コード:

PImage img;
PImage img_r;


int[] hist = new int[256];
int histMax = max(hist);

void setup() 
{
  size(640, 480);
  frameRate(30);
  img = loadImage("Drone.jpg");
  img.loadPixels();
  loadPixels();
  
  
  img_r = createImage(img.width, img.height,RGB);
  
  
  

  for (int x = 0; x < img.width; x++) {
    for (int y = 0; y < img.height; y++ ) {
      // Calculate the 1D location from a 2D grid
      int loc = x + y*img.width;
      // Get the R,G,B values from image
      float r,g,b;
      r = red (img.pixels[loc]);
     
      color c = color(r, 0, 0);
      img_r.pixels[loc] = c;
    }
  }
  
  
  for (int i = 0; i < img_r.width; i++) 
  for (int j = 0; j < img_r.height; j++) 
  {
    int bright = int(brightness(get(i, j)));
    
    int loc = i + j*img_r.width;
    
    float r;
    r = red (img_r.pixels[loc]);
    hist[(int)(r)]++; 

  }
  histMax = max(hist);
}




void draw() 
{
  image(img, 0, 0, img.width/2, img.height/2);
  
  image(img_r, img.width/2, 0, img_r.width/2, img_r.height/2);
  
  
  
  
 

  stroke(255, 0, 0, 128);
  pushMatrix();
  translate(width/3, height/3);
  scale(0.5);
  
  for (int i = 0; i < img_r.width; i += 2) 
  {
  int which = int(map(i, 0, img_r.width, 0, 255));
  int y = int(map(hist[which], 0, histMax, img_r.height, 0));
  line(i, img_r.height, i, y);
  }
  
  popMatrix();
}

かずま

Re: processing 画像とヒストグラムの表示がうまくできない。

#2

投稿記事 by かずま » 7年前

Processing をインストールして、適当に書いてみました。
こんなもんでいいんでしょうか?

コード:

PImage img;
PImage img_r, img_g, img_b;
int[] hist_r = new int[256];
int[] hist_g = new int[256];
int[] hist_b = new int[256];
int histMax;

void setup()
{
  size(640, 480);
  img = loadImage("C:/Users/tomoh/OneDrive/Pictures/Drone.jpg");
  img_r = createImage(img.width, img.height, RGB);
  img_g = createImage(img.width, img.height, RGB);
  img_b = createImage(img.width, img.height, RGB);
  int sz = img.width * img.height;
  for (int loc = 0; loc < sz; loc++) {
    int r = (int)red(img.pixels[loc]);
    int g = (int)green(img.pixels[loc]);
    int b = (int)blue(img.pixels[loc]);
    img_r.pixels[loc] = color(r, 0, 0);  hist_r[r]++;
    img_g.pixels[loc] = color(0, g, 0);  hist_g[g]++;
    img_b.pixels[loc] = color(0, 0, b);  hist_b[b]++;
  }
  histMax = max(max(hist_r), max(hist_g), max(hist_b));
}

void draw()
{
  int w = img.width / 3, h = img.height / 3;
  image(img, w, 0, w, h);
  image(img_r, 0, h, w, h);
  image(img_g, w, h, w, h);
  image(img_b, 2*w, h, w, h);
  int y, b = (int)(img.height * 0.95);
  for (int i = 0; i < 256; i++) {
    int j = (int)map(i, 0, 256, 0, w);
    y = (int)map(hist_r[j], 0, histMax, 0, h);
    stroke(255, 255, 255, 128); line(j, b, j , b - y);
    y = (int)map(hist_g[j], 0, histMax, 0, h);
    stroke(255, 255, 0, 128); line(w + j, b, w + j , b - y);
    y = (int)map(hist_b[j], 0, histMax, 0, h);
    stroke(0, 255, 255, 128); line(2*w + j, b, 2*w + j , b - y);
  }
}
このプログラムは、画像を 1枚しか読み込んでいないので、
2枚読み込むようにしたもの提示してください。

かずま

Re: processing 画像とヒストグラムの表示がうまくできない。

#3

投稿記事 by かずま » 7年前

訂正です。
draw の中の map の第1引数 hist_r, hist_g, hist_b の
添字は、j ではなく、i でした。

返信

“C言語何でも質問掲示板” へ戻る