2011-10-04 4 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; 
}