使用java进行直方图均衡化 [英] Histogram equalization using java
问题描述
我想对RGB图像执行直方图均衡。为此,首先我使用以下步骤生成直方图:
I want to perform histogram equalization for an RGB image. For this , at first I generate the histogram using the following steps :
1)将灰度值映射为$ b $,将强度映射到[0,255]范围内b val = img.getRGB(j,i)& 0xFF;
1)Map the intensity in the range [0,255] , by taking the gray value as val=img.getRGB(j, i) & 0xFF;
2)计算每个强度值对应的像素数(0-255)
2)count the number of pixels corresponding to each intensity value(0-255)
3 )绘制直方图。
4)执行均衡
5)现在我关心的问题是,映射到对应于均衡直方图的RGB图像。我怎么做?所有都是灰色值。任何解决方案?
5)Now the problem I am concerned with is , to map to the RGB image corresponding to the equalized histogram. How do I do that? All are grayvalues .Any solution?
推荐答案
我用Java做过一次。输入是灰度值缓冲的图像bi。 bi的直方图将被均衡。这是代码。
I did this once in Java. Input is a grayvalue buffered Image bi. Histogram of bi will be equalized. Here is the code.
int width =bi.getWidth();
int height =bi.getHeight();
int anzpixel= width*height;
int[] histogram = new int[255];
int[] iarray = new int[1];
int i =0;
//read pixel intensities into histogram
for (int x = 1; x < width; x++) {
for (int y = 1; y < height; y++) {
int valueBefore=bi.getRaster().getPixel(x, y,iarray)[0];
histogram[valueBefore]++;
}
}
int sum =0;
// build a Lookup table LUT containing scale factor
float[] lut = new float[anzpixel];
for ( i=0; i < 255; ++i )
{
sum += histogram[i];
lut[i] = sum * 255 / anzpixel;
}
// transform image using sum histogram as a Lookup table
for (int x = 1; x < width; x++) {
for (int y = 1; y < height; y++) {
int valueBefore=bi.getRaster().getPixel(x, y,iarray)[0];
int valueAfter= (int) lut[valueBefore];
iarray[0]=valueAfter;
bi.getRaster().setPixel(x, y, iarray);
}
}
这篇关于使用java进行直方图均衡化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!