图像上的直方图均衡

时间:2018-06-12 16:06:56

标签: c image-processing

我有这个问题吗?如何在C中对图像进行直方图均衡?我写了这段代码,但是我得不到正确的结果。

void histogram(unsigned char image_in [64][64],unsigned char image_out [64][64], unsigned long hist[256],unsigned long eHist[256],float cdf[256]) {
    #define lines 64
    #define columns 64

    int i,j;

    int pixels = lines*columns;

    // original histogram

    for (i = 1; i < 256; i++) {
        hist[i]=0;
    }

    for (i = 0; i < lines; i++) {
        for (j = 0; j < columns; j++) {
              hist[image_in[i][j]]++;
        }
    }

    // Cumulative Distribution Function
    float cdfmax=256, cdfmin=1;

    for (i = 1; i < 256; i++) {
        cdf[i] = 0;
        for (i = 1; i < 256; i++) {
            cdf[i] += hist[i];
        }
    }

    // Equalized Histogram

    for (i = 1; i < 256; i++) {
        eHist[i] = ((cdf[i]-cdfmin)/((lines*columns)-cdfmin))*255;
    }

    // Final Image

    for (i = 0; i < lines; i++) {
    for (j = 0; j < columns; j++) {
        image_out[i][j] = cdf[image_in[i][j]]*255;
    }
}
}   

这是我的主要功能:

void main(void) {
    FILE *fp;
    fp = fopen("../lena_eye.raw","rb");
    int i,j;
    for (i = 0; i < 64; i++) {
        for (j = 0; j < 64; j++) {
            image_in[i][j] = getc(fp);
        }   
    }

    fclose(fp);
    histogram(image_in, image_out, hist, eHist,cdf);
}

我收到的结果可以在图片中看到。

[1]:https://i.stack.imgur.com/zjhp9.png -image_in
  [2]:https://i.stack.imgur.com/itDNE.png -Hist
  [3]:https://i.stack.imgur.com/74Ulm.png -eHist
  [4]:https://i.stack.imgur.com/qFwUw.png -image_out

2 个答案:

答案 0 :(得分:3)

这段代码错了:

for (i = 1; i < 256; i++) {
    cdf[i] = 0;
    for (i = 1; i < 256; i++) {
        cdf[i] += hist[i];
    }
}

内部循环改变了i的值,弄乱了外部循环。你想写的是这样的:

for (i = 0; i < 256; i++) {
    cdf[i] = 0;
    for (j = 0; j <= i; j++) {
        cdf[i] += hist[j];
    }
}

......但写起来会更简单:

cdf[0] = hist[0];
for (i = i; i < 256; i++) {
    cdf[i] = cdf[i-1] + hist[i];
}

此外,当你计算直方图和均衡直方图时,你有从1开始的循环,它们应该从0开始。

答案 1 :(得分:0)

我在代码中发现了错误,并且分享了我写的内容。

 void Histogramm(unsigned char *image_in, unsigned char *image_out)

{

int i, j;
const unsigned long pixels = lines * columns;       
unsigned long cdf_min;
const unsigned char* limit = image_in + pixels;     
unsigned char* img;     
//    original histogram

for (i = 0; i < 256; i++)
{
    hist[i] = 0;
}


for (img = image_in; img < limit; img++)
{
    hist[*img]++;
}

//    Cumulative Distribution Function

unsigned long count = 0;
for (i = 0; i < 256; i++)
{
    count += hist[i];
    cdf[i] = count;
}
for (i = 0; i < 256; i++)
{
    if (cdf[i] != 0)
    {
        cdf_min = cdf[i];
        break;
    }
}
//                  Equalized Histogram

//eHist[i]=round((cdf[i]-cdf_min)/(pixels-cdf_min)*255)=round(k*(cdf[i]-cdf_min))
const double k = 255.0 / (pixels - cdf_min);

for (i = 0; i < 256; i++)
{
    eHist[i] = (unsigned long)(k*(cdf[i] - cdf_min));
}

//      Final Image

for (img = image_in; img < limit; img++)    
{
    *image_out = eHist[*img];
    image_out++;
}

}