Android直方图均衡化算法给了我一个非常明亮或红色的图像
我正在对图像进行直方图均衡化。我首先得到RGB图像并将其转换为YUV。我在YUV的Y'上运行直方图均衡化算法,然后转换回RGB。是我,还是图像看起来很奇怪?我做得对吗?这张图片很亮,其他图片有点红 以下是之前/之后的图像: 算法(注释值是我之前用于转换的值。两者产生的结果几乎相同):Android直方图均衡化算法给了我一个非常明亮或红色的图像,android,image,algorithm,Android,Image,Algorithm,我正在对图像进行直方图均衡化。我首先得到RGB图像并将其转换为YUV。我在YUV的Y'上运行直方图均衡化算法,然后转换回RGB。是我,还是图像看起来很奇怪?我做得对吗?这张图片很亮,其他图片有点红 以下是之前/之后的图像: 算法(注释值是我之前用于转换的值。两者产生的结果几乎相同): publicstaticvoidcreatecontrast(位图src){ int width=src.getWidth(); int height=src.getHeight(); Bitmap proce
publicstaticvoidcreatecontrast(位图src){
int width=src.getWidth();
int height=src.getHeight();
Bitmap processeImage=Bitmap.createBitmap(宽度、高度,src.getConfig());
int A=0,R,G,B;
整数像素;
浮动[]Y=新浮动[宽度][高度];
浮动[]U=新浮动[宽度][高度];
浮动[]V=新浮动[宽度][高度];
int[]直方图=新int[256];
数组。填充(直方图,0);
int[]cdf=新int[256];
数组。填充(cdf,0);
浮动最小值=257;
浮动最大值=0;
对于(int x=0;x最大值){
max=Y[x][Y];
}
}
}
cdf[0]=直方图[0];
对于(inti=1;i这个问题有点老了,但让我来回答
原因是直方图均衡化的工作方式。该算法尝试使用所有0-255范围,而不是给定图像的范围
所以,如果你给它一个深色的图像,它会把相对较亮的像素变成白色,把相对较暗的像素变成黑色
如果你给它一个明亮的图像,出于同样的原因,它会变暗。我会先创建几个纯色块的测试图像来测试这个。我觉得这个图像看起来很好。我希望你的算法能将图像的某个部分变亮到纯白色。如果你得到一些红色,那就不好了。确保您的RGB->YUV->RGB路径(无均衡)真的是身份功能。谢谢你们的评论。首先,我将使用纯色查看结果,其次,在纯色图像上,我将确保在运行没有histeq的转换时返回相同的图像。@JuanAcevedo hi你明白了吗?我正在做一个类似的项目,我想知道是否可以有一个lo好的,看看你在这个Hi Riga上更新的代码,上面的代码是正确的。对于minMaxCalc,我在我的项目中找不到它。我会再看一下,看看我是否在另一台计算机上有它。
public static void createContrast(Bitmap src) {
int width = src.getWidth();
int height = src.getHeight();
Bitmap processedImage = Bitmap.createBitmap(width, height, src.getConfig());
int A = 0,R,G,B;
int pixel;
float[][] Y = new float[width][height];
float[][] U = new float[width][height];
float[][] V = new float [width][height];
int [] histogram = new int[256];
Arrays.fill(histogram, 0);
int [] cdf = new int[256];
Arrays.fill(cdf, 0);
float min = 257;
float max = 0;
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
pixel = src.getPixel(x, y);
//Log.i("TEST","("+x+","+y+")");
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
/*Log.i("TESTEST","R: "+R);
Log.i("TESTEST","G: "+G);
Log.i("TESTEST","B: "+B);*/
// convert to YUV
/*Y[x][y] = 0.299f * R + 0.587f * G + 0.114f * B;
U[x][y] = 0.492f * (B-Y[x][y]);
V[x][y] = 0.877f * (R-Y[x][y]);*/
Y[x][y] = 0.299f * R + 0.587f * G + 0.114f * B;
U[x][y] = 0.565f * (B-Y[x][y]);
V[x][y] = 0.713f * (R-Y[x][y]);
// create a histogram
histogram[(int) Y[x][y]]+=1;
// get min and max values
if (Y[x][y] < min){
min = Y[x][y];
}
if (Y[x][y] > max){
max = Y[x][y];
}
}
}
cdf[0] = histogram[0];
for (int i=1;i<=255;i++){
cdf[i] = cdf[i-1] + histogram[i];
//Log.i("TESTEST","cdf of: "+i+" = "+cdf[i]);
}
float minCDF = cdf[(int)min];
float denominator = width*height - minCDF;
//Log.i("TEST","Histeq Histeq Histeq Histeq Histeq Histeq");
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
//Log.i("TEST","("+x+","+y+")");
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
Y[x][y] = ((cdf[ (int) Y[x][y]] - minCDF)/(denominator)) * 255;
/*R = minMaxCalc(Y[x][y] + 1.140f * V[x][y]);
G = minMaxCalc (Y[x][y] - 0.395f * U[x][y] - 0.581f * V[x][y]);
B = minMaxCalc (Y[x][y] + 2.032f * U[x][y]);*/
R = minMaxCalc(Y[x][y] + 1.140f * V[x][y]);
G = minMaxCalc (Y[x][y] - 0.344f * U[x][y] - 0.714f * V[x][y]);
B = minMaxCalc (Y[x][y] + 1.77f * U[x][y]);
//Log.i("TESTEST","A: "+A);
/*Log.i("TESTEST","R: "+R);
Log.i("TESTEST","G: "+G);
Log.i("TESTEST","B: "+B);*/
processedImage.setPixel(x, y, Color.argb(A, R, G, B));
}
}