read and write image simultaneously
in
Programming Questions
•
2 years ago
I'm generating probability tables for one color appearing to the right of another. I have accomplished all of this. I store the tables in objects which are created for each color value. My problem is, that when I generate a new image, I'd like to create pixel 0, then make a weighted random decision for the color that will appear to the right. I think my problem is that I'm trying to read data from image I'm constructing, and write to it in the same loop. I'm not sure how processing deals with this, and I seem to be getting strange errors, often, many of my pixels are black. I believe all of my problems are occurring the third time I loop through all of the pixels (lines 60-78), and try to write pixels to the new image.
you can see in the output of println statement the colors that should be written to the new image.
Is there something I'm missing?
This is the first time I've used classes and objects to code, so please forgive any clunkiness.
Thanks in advance for any help anyone can offer.
- PImage src;
- PImage dstn;
- HashMap library;
- int counter;
- color d = (0);
- color seed = (0);
- color ds = (0);
- void setup() {
- library = new HashMap<Integer, Object>();
- size(200, 200);
- src = loadImage("sunflower.jpg");
- dstn = createImage(src.width, src.height, RGB);
- src.loadPixels();
- int acc = 0;
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- int loc = x + y*width;
- color d = src.get(x,y); // get pixel color at desired location
- if (library.containsKey(d)) {
- // Get the AColor object and increase the count
- // We access objects from the library via its key, the String
- AColor c = (AColor) library.get(d);
- c.count(); // touch the counter everytime the a color is read
- c.the_color(d); // add the color to the object
- //c.output();
- } else {
- // Otherwise make a new entry in library
- AColor c = new AColor(d);
- // And add to the library
- // put() takes two arguments, "key" and "value"
- // The key for us is the String and the value is the AColor object
- library.put(d, c);
- } // all colors are in library now
- AColor c = (AColor) library.get(d);
- if (x < width - 1 ) { //If statement to ensure null pixles are not added to transition matrix
- color z = src.get(x+1,y);
- c.access_matrix_right(z);
- } else { // this is a nasty shortcut that wraps the probability of the rightmost pixel to the leftmost pixel
- color z = src.get(x,y);
- c.access_matrix_right(z);
- }
- }
- }
- }
- void draw() {
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- color d = src.get(x,y);
- AColor c = (AColor) library.get(d);
- c.sort_matrix(); // add and construct all of the ArrayLists for each object
- println("first loop");
- }
- }
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- int loc1 = ((x + y*width));
- color seed = src.get(x,y);
- dstn.pixels[0] = seed;
- color ds = src.get(x,y); // copy pixel 0 from src to dstn image
- AColor c = (AColor) library.get(ds);
- float chance;
- int acc = 0;
- chance = random(1);
- float probAccum = (c.probs.get(acc));
- while (chance > probAccum) {
- acc++;
- probAccum = probAccum + (c.probs.get(acc));
- int colorToTheRight = c.colors.get(acc);
- dstn.pixels[loc1] = colorToTheRight; // <-If I put this outside of the while lopp, the image is more or less normal looking.
- }
- println(acc + " " + c.colors.get(acc) + " , " + c.colors + " - " + c.probs + " Chance = " + chance +" -color should be" + (c.colors.get(acc)));
- dstn.updatePixels();
- }
- }
- dstn.updatePixels();
- image(dstn,0,0);
- noLoop();
- }
- class AColor {
- float count;
- int theColor;
- int colorRight;
- int acc = 0;
- int z;
- HashMap<Object, Integer> matrix = new HashMap<Object, Integer>();
- ArrayList<Float> probs;
- ArrayList<Integer> colors = new ArrayList<Integer>(); //an ArrayList is used here. Perhaps it would be better to use an Array and iterate over the hashmap to set the length
- AColor(int theColorTemp) {
- theColor = theColorTemp;
- count = 1;
- }
- void the_color(int theColorTemp) {
- theColor = theColorTemp;
- }
- void count() {
- count++;
- }
- void access_matrix_right(int colorRightTemp) {
- colorRight = colorRightTemp;
- if (matrix.containsKey(colorRight)) { // if library has entry for current pixel
- int val = ((Integer) matrix.get(colorRight)).intValue(); //accumulator
- matrix.put(colorRight, new Integer(val + 1)); // add 1 to
- }
- else {
- matrix.put(colorRight,1); //adds entry & a value of 1 if entry does not exist
- colors.add(colorRight);
- }
- }
- void sort_matrix() {
- probs = new ArrayList<Float>();
- for (int i = 0; i <= colors.size()-1; i++) { //for number elements in list
- probs.add((matrix.get(colors.get(i))) / count); // add element in array probs (number of occurrances of a color on the right/ total pixels on right )
- }
- }
- }
1