Projets

test traitement image

float[][] matrix1 = {
  { 
    1, 1, 1
  }
  , { 
    1, -9, 1
  }
  , { 
    1, 1, 1
  }
}; 
float[][] matrix2 = {
  { 
    (1.0/9.0), (1.0/9.0), (1.0/9.09)
  }
  , { 
    (1.0/9.0), (1.0/9.0), (1.0/9.0)
  } 
  , { 
    (1.0/9.0), (1.0/9.0), (1.0/9.0)
  }
}; 
float[][] matrix3 = {
  { 
    (1.0/1.0), (1.0/1.0), (1.0/1.0)
  }
  , { 
    (0.0/1.0), (0.0/1.0), (0.0/1.0)
  } 
  , { 
    (-1.0/1.0), (-1.0/1.0), (-1.0/1.0)
  }
}; 

void setup() {
  
  PImage img10, img20, img1, img2, img3, img4, img5, img6, img7, img8, img9;  
  int taille = 150; 
  int loc;
  color c1,c2,c3;
  size(530, 530);
  noLoop();
  background(125);

  img1 = createImage(150, 150, RGB);
  img2 = createImage(150, 150, RGB);
  img3 = createImage(150, 150, RGB);
  img4 = createImage(150, 150, RGB);
  img5 = createImage(150, 150, RGB);
  img6 = createImage(150, 150, RGB);
  img7 = createImage(150, 150, RGB);
  img8 = createImage(150, 150, RGB);
  img9 = createImage(150, 150, RGB);

  //img10 = loadImage("carre.png");
  //img20 = loadImage("cercle.png");
  //image(img10, 20, 20);
  //image(img20, 190, 20);
  
  fill(0);
  rect(20,20,150,150);
  rect(190,20,150,150);
  fill(255);
  rect(40,40,80,80);
  ellipse(270,100,80,80);
  
  for (int x = 0; x < taille ; x++) {
    for (int y = 0; y < taille; y++) {
      c1 = get(x+20, y+20);
      c2 = get(x+190, y+20); 
      //set(x+20,y+20,c1); 
      //set(x+190,y+20,c2);   
      img1.pixels[x+y*taille]=c1;
      img2.pixels[x+y*taille]=c2;
    }
  }
  
  image(img1, 20, 20);
  image(img2, 190, 20);

  for (int x = 0; x < taille; x++) {
    for (int y = 0; y < taille; y++) {
      c3 = traitement(get(x+20, y+20), get(x+190, y+20), 1);
      set(x+360,y+20,c3);   
      img3.pixels[x+y*taille]=c3;
    }
  }
  image(img3, 360, 20);


  for (int x = 0; x < taille; x++) {
    for (int y = 0; y < taille; y++) {
      img6.pixels[x+y*taille]=convolution(x, y, matrix1, 3, img3);
    }
  }
  image(img6, 360, 190);

  for (int x = 0; x < taille; x++) {
    for (int y = 0; y < taille; y++) {
      img4.pixels[x+y*taille]=convolution(x, y, matrix2, 3, img1);
      img5.pixels[x+y*taille]=convolution(x, y, matrix2, 3, img2);
    }
  }
  image(img4, 20, 190);
  image(img5, 190, 190);

  for (int x = 0; x < taille; x++) {
    for (int y = 0; y < taille; y++) {
      img7.pixels[x+y*taille]=convolution(x, y, matrix3, 3, img1);
      img8.pixels[x+y*taille]=convolution(x, y, matrix3, 3, img2);
    }
  }
  image(img7, 20, 360);
  image(img8, 190, 360);  

  for (int x = 0; x < taille; x++) {
    for (int y = 0; y < taille; y++) {
      img9.pixels[x+y*taille]=convolution(x, y, matrix2, 3, img6);
      //img9.pixels[loc]= traitement(get(x+20, y+360), get(x+190, y+360), 2);
    }
  }
  image(img9, 360, 360);
}

void draw() {
}

int traitement(color a, color b, int i)
{
  color c;
  int rc = 0;
  int gc = 0;
  int bc = 0;
  boolean bra,bga,bba;
  boolean brb,bgb,bbb;
  boolean brc,bgc,bbc;
  
  bra = (red(a) > 127) ;
  bga = (green(a) > 127);
  bba = (blue(a) > 127);
  brb = (red(b) > 127) ;
  bgb = (green(b) > 127);
  bbb = (blue(b) > 127);
  if (i==1) {
    brc = operateur1(bra, brb);
    bgc = operateur1(bga, bgb);
    bbc = operateur1(bba, bbb);
    if (brc) rc=255; 
    if (bgc) gc=255; 
    if (bbc) bc=255;
  }
  if (i==2) {
    brc = operateur2(bra, brb);
    bgc = operateur2(bga, bgb);
    bbc = operateur2(bba, bbb);
    if (brc) rc=255; 
    if (bgc) gc=255; 
    if (bbc) bc=255;
  }
  c = color(rc, gc, bc);
  return c;
}

boolean operateur1(boolean a, boolean b)
{
  return(a && b);
}

boolean operateur2(boolean a, boolean b)
{
  return(a || b);
}


color convolution(int x, int y, float[][] matrix, int matrixsize, PImage img)
{
  float rtotal = 0.0;
  float gtotal = 0.0;
  float btotal = 0.0;
  int xloc, yloc, loc, offset;
  
  offset = (matrixsize+1)/ 2;
  for (int i = 0; i < matrixsize; i++) {
    for (int j= 0; j < matrixsize; j++) {
      xloc = x+i-offset;
      yloc = y+j-offset;
      loc = xloc + img.width*yloc;
      loc = constrain(loc, 0, img.pixels.length-1);
      rtotal += (red(img.pixels[loc]) * matrix[i][j]);
      gtotal += (green(img.pixels[loc]) * matrix[i][j]);
      btotal += (blue(img.pixels[loc]) * matrix[i][j]);
    }
  }
  rtotal = constrain(rtotal, 0, 255);
  gtotal = constrain(gtotal, 0, 255);
  btotal = constrain(btotal, 0, 255);
  return color(rtotal, gtotal, btotal);
}