brightness(pixel) doesnt work

resres
edited June 2017 in Questions about Code

Hello 2gether,

i want to generate objects (ellipse) on a typo of an Image (pic.png).

Sadly, the code doesnt work and the objects on the entire screen. Thanks for HELP!!!!
Heres the Code:

    ArrayList <PVector> circles = new ArrayList <PVector> ();
    float diameter;
    PImage typo;

    void setup() {
      background(255);
      size(500, 500);
      colorMode(RGB, 100, 100, 100);
      noStroke();
      smooth();
      typo = loadImage("pic.png");
    }

    void draw() {

      addCircle();
      for (int i=0; i<circles.size(); i++) {
        PVector p = circles.get(i);
        fill(i % 360, 100, 100);
        ellipse(p.x, p.y, p.z, p.z);
      }
    }

    void addCircle() {
      diameter=random(10, 40);
      PVector c = randomVector();
      int tries = 1000000;
      color pixel;

      pixel = typo.get((int)c.x, (int)c.y);

      while (overlap(c) && tries > 0) {
        c = randomVector();
        tries--;
      }

      if (!overlap(c) && brightness(pixel)>0) {
        circles.add(c);
      } else {
        addCircle();
      }
    }

    PVector randomVector() {
      return new PVector(random(width), random(height), diameter);
    }

    boolean overlap(PVector c) {
      for (PVector p : circles) {
        if ((dist(c.x, c.y, p.x, p.y) < (c.z + p.z)*0.5)) {
          return true;
        }
      }
      return false;
    }
Tagged:

Answers

  • edited June 2017

    What doesn't work about it? You are generating circles that cover the majority of the screen... Oh, but you can't see them because they get white very fast, and you are drawing white circles on a white background.

    Try changing line 19 to this:

    fill(i % 360, 0, 100);
    

    And you will see all your circles.

  • resres
    edited June 2017

    No, i want to generate a typo like this

    ScreenHunter_01 Jun. 17 11.05

    But the circles on the complete Screen.

  • Sadly, the code doesnt work and the objects on the entire screen


    But the circles on the complete Screen.

    Those are two incomplete sentences... I can't tell what is not working, what the program should be doing. Provide more details about your input image and what the algorithm suppose to do with your image. Also, consider sharing your PNG file.

    I think things could be more clear if you explain more about this:

    i want to generate objects (ellipse) on a typo of an Image

    I think you meant to say that image contains text and you want to generate random circles on top of the text (?)

    Kf

  • resres
    edited June 2017

    Yes, youre right. Sorry for my bad explaination. I have an image and i want to create random circles on the top of the letters. But this Code:

    if (!overlap(c) && brightness(pixel)>0) {

    doesnt work and the circles over the complete screen. The letters are black and the background white (RGB).

  • !overlap(c)
    

    Currently you are adding a new circle if it doesnt overlap. Surely you meant if it does...?

  • edited June 2017 Answer ✓

    In this next sketch, you will need to load your text graphics into the pg object within the draw() function or in setup. The text must be white in black background. If your text is black on white background, you need to apply an invert filter. In the demo below, click on the image to toggle the masking effect on and off.

    When you toggle between masking action, you will see that when the mask is not being applied, there is an edge effect. In your case, it should not matter as you want the mask active. I will be opening another post to address that issue.

    Kf

    //REFERENCES: https:// forum.processing.org/two/discussion/23093/brightness-pixel-doesnt-work#latest
    
    //INSTRUCTIONS:
    //         *--   Click to enable or disable the masking effect
    
    
    //===========================================================================
    // FINAL FIELDS:
    final int maxTries=9000;
    final int nCircles=2000;
    final String REN=P2D;
    
    
    //===========================================================================
    // GLOBAL VARIABLES:
    
    ArrayList<CircleStruct> c;
    PGraphics pg;
    PGraphics gg;
    boolean maskImageNow=false;
    
    //===========================================================================
    // PROCESSING DEFAULT FUNCTIONS:
    
    void settings() {
      size(400, 600, REN);
      smooth(8);
    }
    
    void setup() {
    
      textAlign(CENTER, CENTER);
      rectMode(CENTER);
      ellipseMode(RADIUS);
    
      fill(255);
      strokeWeight(2);
    
      pg = createGraphics(width, height, REN);
      //pg.smooth(4);
    
      c=new ArrayList<CircleStruct>();
      gg=createGraphics(width, height, REN);  
      //gg.smooth(4);
      fillWithCircles(nCircles);
      gg.beginDraw();  
      gg.noFill();
      gg.stroke(255);
      gg.strokeWeight(2);
      gg.clear();
      for (CircleStruct pc : c) {
        pc.draw(gg);
      }
      gg.endDraw();
    }
    
    
    
    void draw() {
      background(0);
    
      pg.beginDraw();
      pg.clear();
      pg.ellipse(mouseX, mouseY, 100, 100);
      pg.endDraw();
    
      image(pg, 0, 0);
    
      if (maskImageNow) 
        gg.mask(pg);
    
      image(gg, 0, 0);
    }
    
    void keyReleased() {
    }
    
    void mouseReleased() {
      maskImageNow=!maskImageNow;  //Toggle action
    
      if (maskImageNow==false) {
        gg.beginDraw();
        gg.noFill();
        gg.stroke(255); 
        gg.strokeWeight(2);
        gg.clear();
        for (CircleStruct pc : c) {
          pc.draw(gg);
        }
        gg.endDraw();
      }
    }
    
    
    
    //===========================================================================
    // OTHER FUNCTIONS:
    
    void fillWithCircles(int n) {
      noFill();
      stroke(255);
      c.clear();  
    
      for (int i=0; i<n; i++) {
        CircleStruct cir=new CircleStruct();
        boolean overlap=true;
        int ntries=0;
        while (overlap && ntries<maxTries) {
          overlap=false;
          ntries++;
          for (CircleStruct pc : c) {
            if (cir.overlaps(pc)) {
              overlap=true;
              cir.reGenerateCircle();
              break;
            }
          }
        }
    
        if (ntries>=maxTries) {
          println("Stop trying placing circles at counter " + i + " out of "+n);
          break;
        }
    
        c.add(cir);
      }
    }
    
    
    class CircleStruct {
    
      final static int minRad=3;
      final static int maxRad=12;
    
      float w;
      float h;
      float rad;
    
      CircleStruct() {
        generateCircle();
      }
    
      void generateCircle() {
        w=random(width);
        h=random(height);
        rad=random(minRad, maxRad);
      }
    
      void reGenerateCircle() {
        generateCircle();
      }
    
      void draw() {
        ellipse(w, h, rad, rad);
      }
    
      void draw(PGraphics gg) {
        gg.ellipse(w, h, rad, rad);
      }
    
      boolean overlaps(CircleStruct ptrC) {
        return overlaps(ptrC.w, ptrC.h, ptrC.rad);
      }
    
      boolean overlaps(float _w, float _h, float _r) {
        return dist(w, h, _w, _h)<rad+_r;
      }
    

    }

Sign In or Register to comment.