繁体   English   中英

从reg-green-blue img到c中的灰度img

[英]from reg-green-blue img to gray scale img in c

我正在编写代码以转换RGB格式的图像

RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB RGB

试图转换为灰度img,但无法正常工作。 我得到一张未着色的图片,但不符合预期。 更像是黑白而不是灰色。

struct Img {
    unsigned long sizeX;
    unsigned long sizeY;
    char *data;
};

void convernt(Img *image)
{ 
int x,y,i, iRow;
    char* p = image->data;
    int width=image->sizeX;
    int height=image->sizeY;                         
    int rowSize = width*3;                // number of bytes in one row
    int iPix;
    for ( y = 0; y < height; ++y)
    {
        iRow = y*rowSize ;                 // index of first pixel in row y
        for (  x = 0; x < rowSize; x+=3)
        {
            iPix = iRow + x*3;               // index of pixel 
double con=0.2989*p[iPix]+ 0.5870*p[iPix+1]+0.1140*p[iPix+2];
                p[iPix] = con;
                p[iPix+1] = con;
                p[iPix+2] = con;

        }
    }
}

我希望代码等于此代码,但要短一些应该做完全一样的东西,我猜我错过了一些东西

#define NUM_OF_COLORS   256

// perform histogram equalization algorithm on an image
void convert(Img *image)
{ 

    int i,j;

    double* orgHist = (double*)malloc(NUM_OF_COLORS*sizeof(double));
    unsigned char* conversionVector = (unsigned char*)malloc(NUM_OF_COLORS);

    //clear the vector
    for (i=0; i<NUM_OF_COLORS; i++) {
        orgHist[i] = 0;
    }

    //get the histogram of the image
    for (j=0; j<image->sizeY; j++) {
            for (i=0; i<image->sizeX; i++) {
            orgHist[(unsigned char)image->data[3*i + 3*j*image->sizeX + 1]]++;
        }
    }

    //calculate the accumulated histogram of the image
        for (i=1; i<NUM_OF_COLORS; i++) {
            orgHist[i] = orgHist[i] + orgHist[i-1];
    }

    //normalize the histogram
        for (i=0; i<NUM_OF_COLORS; i++) {
            orgHist[i] = orgHist[i]/(image->sizeX*image->sizeY);
        conversionVector[i] = 0;
    }


    // preform the histogram equalization algorithm
    i = 0;
        j = 0;
    while (i<NUM_OF_COLORS) {

        if ((((double)j+1)/256) < orgHist[i]) {
            j++;
        } else {
            conversionVector[i] = j;
            i++;
        }
    }

    // apply the conversion vector on the image
        for (i=0; i<image->sizeX; i++) {
        for (j=0; j<image->sizeY; j++) {
            image->data[3*i + 3*j*image->sizeX + 1] = 
                conversionVector[(unsigned char)image->data[3*i + 3*j*image->sizeX + 1]];
        }
    }

    // copy G values to R&B 
    for (i = 0; i < image->sizeX*image->sizeY; i++) {
        image->data[3*i] = image->data[3*i+1];
        image->data[3*i+2]= image->data[3*i+1];
    }

    free(orgHist);
    free(conversionVector);
}

要么你增加x3在你for循环,或添加x*3到索引中。 但是不要同时使用 。:

for (  x = 0; x < rowSize; x+=3)
                         // ^^^  either x++ ...
{
   iPix = iRow + x*3;               // index of pixel
               // ^^ ... or iRow + x;  
   ...
}

因为rowSize是字节数而不是像素数,所以请像这样修改代码:

for (  x = 0; x < rowSize; x+=3 )
{
    iPix = iRow + x; // index of pixel 
    double con = 0.2989*p[iPix] + 0.5870*p[iPix+1] + 0.1140*p[iPix+2];
    p[iPix]   = (char)con;
    p[iPix+1] = (char)con;
    p[iPix+2] = (char)con;
}

这是大图像的解决方案:

#define NUM_OF_COLORS   256

unsigned char rTable[NUM_OF_COLORS];
unsigned char gTable[NUM_OF_COLORS];
unsigned char bTable[NUM_OF_COLORS];

void initTables()
{
    for ( int c = 0; c < NUM_OF_COLORS; c ++ )
    {
        rTable[c] = (unsigned char)(0.2989*c);
        gTable[c] = (unsigned char)(0.5870*c);
        bTable[c] = (unsigned char)(0.1140*c);
    }
}

void convernt(struct Img *image)
{ 
    unsigned char* p = (unsigned char*)image->data;
    int width=image->sizeX;
    int height=image->sizeY;                         
    int rowSize = width*3;                // number of bytes in one row
    for ( int y = 0; y < height; ++y)
    {
        int iRow = y*rowSize ;                 // index of first pixel in row y
        for ( int x = 0; x < rowSize; x+=3 )
        {
            int iPix = iRow + x; // index of pixel
            unsigned char col = rTable[p[iPix]] + gTable[p[iPix+1]] + bTable[p[iPix+2]]; 
            p[iPix]   = col;
            p[iPix+1] = col;
            p[iPix+2] = col;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM