1
votes

I'm new to image processing.When I tried the histogram equalization algorithm,I got an error which I can't explain,just as the picture shows below.

I'm sorry that I can't upload a picture right now.I'll use picasa instead. https://picasaweb.google.com/lh/photo/xoxhWR7waVp50uLh-Ko78C_rLSYVGhhn6l5Yer6Tngc?feat=directlink

The original picture is on the left.

My algorithm is turn RGB into YCbCr and equalize Y,leave Cb and Cr be.Then convert YCbCr into RGB again to show the picture after equalization.

Here's the conversion code.

void MainWindow::rGBToYCbCr(uchar *bmpArray,uchar *lumaArray,uchar *cBCrArray,int startPoint)
{
    for(int i = 0,m = 0,n = 0; i < bmpWidth * bmpHeight; i++,m+=3,n+=2)
    {
        lumaArray[i] = (uchar)(0.299 * bmpArray[startPoint + m + 2] + 0.587 *bmpArray[startPoint + m + 1] + 0.115 * bmpArray[startPoint + m]);
        cBCrArray[n] = (uchar)(-0.169 * bmpArray[startPoint + m + 2] - 0.331 * bmpArray[startPoint + m + 1] + 0.5 * bmpArray[startPoint + m]) + 128;//cb
        cBCrArray[n+1] = (uchar)(0.5 * bmpArray[startPoint + m + 2] - 0.419 * bmpArray[startPoint + m + 1] - 0.081 * bmpArray[startPoint + m]) + 128;//cr
    }
}

void MainWindow::yCbCrToRGB(uchar *lumaArray,uchar *targetArray,uchar *CbCrArray,int startPoint)
{
    for(int i = 0,m = 0,n=0; i < 3 * bmpWidth * bmpHeight; i+=3,m++,n+=2)
    {
        targetArray[startPoint + i + 2] = lumaArray[m] + (uchar)(1.402 * (CbCrArray[n + 1] - 128));
        targetArray[startPoint + i + 1] = lumaArray[m] - (uchar)(0.344 * (CbCrArray[n] - 128)) - (uchar)(0.714 * (CbCrArray[n + 1] - 128));
        targetArray[startPoint + i] = lumaArray[m] + (uchar)(1.772 * (CbCrArray[n] - 128));
    }
}

And here is the equlization algorithm.

void MainWindow::histogramEqulizeGrayScale(uchar *bmpArray,int startPoint)
{
    int hisTimes[256]={0};
    for(int i = 0; i < bmpWidth * bmpHeight; i++)
        hisTimes[(int)bmpArray[startPoint + i]]++;
    double pixmapProbability[256];
    for(int i = 0; i < 256; i++)
    {
        int sum = 0;
        for(int j = 0; j <= i; j++)
            sum += hisTimes[j];
        pixmapProbability[i] = (double)sum / (double)(bmpWidth * bmpHeight);
    }
    for(int i = 0; i < 256; i++)
        hisTimes[i] = pixmapProbability[i] * 256;
    for(int i = 0; i < bmpWidth * bmpHeight; i++)
        bmpArray[startPoint + i] = hisTimes[bmpArray[startPoint + i]];
}

Why would the color become so terrible?

1

1 Answers

1
votes

could it be that your color values are overflowing?

You have to clamp the results of your calculations for each color to [0,255]