Home>

I'm trying to write code that does error diffusion.
I would like to get the output result when the image is read into img1 and the error in the neighborhood when binarization is moved to the right pixel by the error diffusion method in img2. First of all, GRAY filter is applied to img1.
With the following code, the output screen of img2 becomes black.
I would like some advice on what to do.

PImage img1, img2;

void setup(){
size(1000, 500);

img1=loadImage("fruits.jpg");
img1.filter(GRAY);
image(img1, 0, 0);
loadPixels();

img2= createImage(500, 500, RGB);
}

void draw(){
int x, y, pos, pos2;
int e=0;
color pix=img1.pixels[1];
color pix2=0;

for(y=0;y<499;y++){
for(x=0;x<499;x++){

pos=x+y*500;
pos2=pos+1;

pix=pix2;

if(red(pix)<=128&&green(pix)<=128&&blue(pix)<=128){
e=pix;
pix2=img1.pixels[pos2]+e;

☆print(pix2+" ");

img2.pixels[pos2]=color(0, 0, 0);
}
else{
e=pix-255;
pix2=img1.pixels[pos2]+e;
img2.pixels[pos2]=color(255, 255, 255);
}
}

image(img2, 500, 0);
}
}

By the way, when the pixel value was output to the console on the line marked with a star, a 7- or 8-digit number was output.
I understood that grayscale has the same value of 0-255 for RGB, but why does the output look like this?

  • Answer # 1

    I've only messed with the matter of "becoming black". Changes are marked with /////. Dithering is also strange.

    PImage img1, img2;
    void setup() {
      size(1000, 500);
      ///// Acquire the material appropriately from the result of Googleing with'free illustration fruit'
      img1=loadImage("https://1.bp.blogspot.com/--Z0FQl8l9sU/UbVvQV3OinI/AAAAAAAAUtE/XpDlhdqZxaY/s800/fruits_basket.png");
      img1.resize(500, 500);///// Adjust the size for the time being
      img1.filter(GRAY);
      image(img1, 0, 0);
      img1.loadPixels();///// What you want to read is img1
      img2= createImage(500, 500, RGB);
    }
    void draw() {
      int x, y, pos, pos2;
      int e=0;
      color pix=img1.pixels[1];
      color pix2=0;
      for (y=0;y<499;y++) {
        for (x=0;x<499;x++) {
          pos=x+y*500;
          pos2=pos+1;
          pix=pix2;
          if (red(pix)<=128&&green(pix)<=128&&blue(pix)<=128) {
            e=pix;
            pix2=img1.pixels[pos2]+e;
            print(pix2+" ");//☆
            img2.pixels[pos2]=color(0, 0, 0);
          } else {
            e=pix-255;
            pix2=img1.pixels[pos2]+e;
            img2.pixels[pos2]=color(255, 255, 255);
          }
        }
        //image(img2, 500, 0);///// Show here?
      }
      img2.updatePixels();/////pixels Reflect operation results
      image(img2, 500, 0);///// Display after processing is complete
      noLoop();///// do it once enough
    }


    For questions, please highlight the syntax of the source code. (Besides thebutton, you can enclose the code before and after with a line containing only ```)


    Dither, wrong way of "pixel addition".
    The value of the element read from pixels[] is
    Opacity*0x1000000+R*0x10000+G*0x100+B (Opacity, R, G, and B are integers in the range of 0 to 255)
    For example, if R=G=B=128 (gray), use hexadecimal notation
    0xff808080
    So, if you add the same R=G=B=128
    0xff010100
    That means R=1, G=1, B=0.
    Thus, adding two pixel values ​​as they are does not mean "added" as pixel data.

    Since it is in gray scale, R/G/B should be the same value.For example, take out green() as a representative value, or take out brightness data with the brightness() function and perform an operation on it. , It should be reconstructed as pixel data (convenient for color() function) after it is stored in the range of 0 to 255.

Related articles