Problems with using a rough greyscale algorithm?

前端 未结 7 2104
生来不讨喜
生来不讨喜 2021-02-06 20:43

So I\'m designing a few programs for editing photos in python using PIL and one of them was converting an image to greyscale (I\'m avoiding the use of

7条回答
  •  星月不相逢
    2021-02-06 21:01

    In answer to your main question, there are disadvantages in using any single measure of grey. It depends on what you want from your image. For example, if you have colored text on white background, if you want to make the text stand out you can use the minimum of the r, g, b values as your measure. But if you have black text on a colored background, you can use the maximum of the values for the same result. In my software I offer the option of max, min or median value for the user to choose. The results on continuous tone images are also illuminating. In response to comments asking for more details, the code for a pixel is below (without any defensive measures).

    int Ind0[3] = {0, 1, 2};                 //all equal
    int Ind1[3] = {2, 1, 0};                 // top, mid ,bot from mask...
    int Ind2[3] = {1, 0, 2};
    int Ind3[3] = {1, 2, 0};
    int Ind4[3] = {0, 2, 1};
    int Ind5[3] = {2, 0, 1};
    int Ind6[3] = {0, 1, 2};
    int Ind7[3] = {-1, -1, -1};              // not possible
    int *Inds[8] = {Ind0, Ind1, Ind2, Ind3, Ind4, Ind5, Ind6, Ind7};
    void grecolor(unsigned char *rgb, int bri, unsigned char *grey)
    {                         //pick out bot, mid or top according to bri flag
        int r = rgb[0];
        int g = rgb[1];
        int b = rgb[2];
        int mask = 0;
        mask |= (r > g);
        mask <<= 1;
        mask |= (g > b);
        mask <<= 1;
        mask |= (b > r);
        grey[0] = rgb[Inds[mask][2 - bri]];  // 2, 1, 0 give bot, mid, top
    }
    

提交回复
热议问题