2011-10-04 44 views
0

我生成一種顏色出現在另一種顏色的概率表。我完成了所有這一切。我將表格存儲在爲每個顏色值創建的對象中。我的問題是,當我生成一個新圖像時,我想創建像素0,然後對顯示在右側的顏色進行加權隨機決定。我認爲我的問題是我試圖從我正在構建的圖像讀取數據,並在同一個循環中寫入它。我不確定如何處理這個問題,而且我似乎正在得到奇怪的錯誤,通常我的許多像素都是黑色的。我相信我所有的問題都是在第三次遍歷所有像素時發生的(第60-78行),並嘗試將像素寫入新圖像。同時讀取和寫入圖像處理

您可以在println語句的輸出中看到應該寫入新圖像的顏色。

有什麼我失蹤了嗎? 這是我第一次使用類和對象進行編碼,所以請原諒任何笨拙。 在此先感謝任何人可以提供的幫助。

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

如果您的目標是創建一個概率表,爲什麼「生成一個新的圖像」?你究竟在做什麼?你的實際問題還不清楚。您也可能想要嘗試重新格式化您的代碼,但閱讀它的方式有點困難。 – misha

+0

我想根據概率表生成一個新圖像。問題是,當我寫這個新圖像時,我也在讀它,查看概率表,然後寫另一個像素。概率不是我的問題,它是同時讀取和寫入圖像。 – evanlivingston

+0

你爲什麼要同時讀寫圖像?對於你正在解決的問題沒有必要。生成的圖像可以是隻寫的。 – misha

回答

0

爲什麼不把所有的像素讀入第二張圖像寫入,然後再寫回原來的圖像? 會不會像這樣工作? (未經測試)

int numPixelsX = 500; 
int numPixelsY = 500; 
PImage captureImage = createImage (numPixelsX,numPixelsY, ARGB); 

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


void draw(){ 

    captureImage.loadPixels(); 
    captureImage = pushPixels(captureImage); 
    captureImage.updatePixels(); 
    image(captureImage, 0, 0); 
} 

PImage pushPixels(PImage readImage){ 
    PImage writeImage = createImage(numPixelsX,numPixelsY,ARGB); 
    writeImage.loadPixels(); 
    writeImage = readImage(); 
    //do your stuff here from the read image to the write image 
    writeImage.updatePixels(); 
    return writeImage; 
}