Openframeworks中的Ordered Bayer Dithering

时间:2015-04-14 19:47:15

标签: c++ openframeworks

我正在尝试对 Openframeworks 中的图像应用有序的拜耳抖动算法,但我无法获得结果。

维基百科链接:http://en.wikipedia.org/wiki/Ordered_dithering

算法是: -

foreach y
    foreach x
       oldpixel := pixel[x][y] + (pixel[x][y] * threshold_map_4x4[x mod 4][y mod 4])
       newpixel := find_closest_palette_color(oldpixel)
       pixel[x][y] := newpixel

我的代码: -

void testApp::setup(){



     int factor = 0;
        float array[4][4] = {{1,9,3,11},{13,5,15,7},{4,12,2,10},{16,8,14,6}};

        for(int i = 0 ; i<4;i++){
            for(int j = 0 ; j<4;j++){
                array[i][j] = array[i][j]/17;
            }
        }

        img1.loadImage("hello.jpg");
        img1.resize(999,999);
        img1.update();

        img2 = img1;

        pix = img1.getPixels();
        pix2 = img2.getPixels();

        for(int k  = 0 ; k < 999 ; k++){
            for(int j = 0 ; j < 999*3 ; j+=3){

           factor = array[k%4][j%4];
            pix2[k*999 + j] +=  ( pix2[k*999 + j]  * factor );
            pix2[k*999 + j + 1] +=  ( pix2[k*999 + j + 1]  * factor );
            pix2[k*999 + j + 2] +=  ( pix2[k*999 + j + 2]  * factor );

                pix[k*999 + j] = ( pix2[k*999 +j] + 128 ) / 256 ;
                pix[k*999 + j+1] = ( pix2[k*999 +j+1] + 128 ) / 256 ;
                pix[k*999 + j+2] = ( pix2[k*999 +j+2] + 128 ) / 256 ;

             }
          } 

       img1.update();

        }


    //--------------------------------------------------------------
    void testApp::update(){




    }

    //--------------------------------------------------------------
    void testApp::draw(){

        img1.draw(0,0);

    }

我在哪里做错了?

1 个答案:

答案 0 :(得分:0)

也许是一个四舍五入的问题?我把代码片段放在一起:

int factor = 0;
float array[4][4] = {{1,9,3,11},{13,5,15,7},{4,12,2,10},{16,8,14,6}};
array[i][j] = array[i][j]/17;  // values are now all betwenn 0.0 and 1.0

factor = array[k%4][j%4]; // here factor will become 0