gpt4 book ai didi

java - java中的直方图均衡

转载 作者:行者123 更新时间:2023-12-02 10:17:50 25 4
gpt4 key购买 nike

我尝试编译以下代码,但发现仅对于我输入用于测试的所有图像,图像才会变成全黑。

附注我是一个Java新手,除了内置包之外,我不允许使用任何包

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;

public class ImageProcessor
{
public static BufferedImage convert(Image img)
{
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics bg = bi.getGraphics();
bg.drawImage(img, 0, 0, null);
bg.dispose();
return bi;
}

public static BufferedImage toGrayScale(Image img)
{
// Convert image from type Image to BufferedImage
BufferedImage bufImg = convert(img);

// Scan through each row of the image
for(int j=0; j < bufImg.getHeight(); j++)
{
// Scan through each columns of the image
for(int i=0; i < bufImg.getWidth(); i++)
{
// Returns an integer pixel in the default RGB color model
int values=bufImg.getRGB(i,j);
// Convert the single integer pixel value to RGB color
Color oldColor = new Color(values);

int red = oldColor.getRed(); // get red value
int green = oldColor.getGreen(); // get green value
int blue = oldColor.getBlue(); // get blue value

// Convert RGB to gray scale using formula
// gray = 0.299 * R + 0.587 * G + 0.114 * B
double grayVal = 0.299*red + 0.587*green + 0.114*blue;

// Assign each channel of RGB with the same value
Color newColor = new Color((int)grayVal, (int)grayVal, (int)grayVal);

// Get back the integer representation of RGB color
// and assign it back to the original position
bufImg.setRGB(i, j, newColor.getRGB());
}
}
// return back the resulting image in BufferedImage type
return bufImg;
}

public static BufferedImage histEqualization(Image img)
{
//Convert image to BufferedImage
img = ImageProcessor.toGrayScale(img);
BufferedImage bufImg = convert(img);


//Getting information of each pixel;
int[][] intensity = new int[bufImg.getWidth()][ bufImg.getHeight()];
int[] counter = new int[256];
for(int j=0; j < bufImg.getHeight();j++)
for(int i=0; i < bufImg.getWidth();i++)
{
int values=bufImg.getRGB(i,j);
Color oldColor = new Color(values);
intensity[i][j] = oldColor.getBlue();
counter[intensity[i][j]]++;
}

//BEGIN OF Histogram Equalization

//find out how many rows the table have
int row=0;

for(int i=0;i<256;i++)
if(counter[i]!=0)
row++;

//Find out the v column of the table
//table[row][0] = v column
//table[row][1] = c column
int temp=0;
int[][] table = new int[row][2];


for(int i=0;i<256;i++)
if(counter[i]!=0)
{
table[temp][0] = i;
temp++;
}

//Find out the c column of the table
for(int i=0;i<row;i++)
table[i][1] = counter[table[i][0]];

//C-> CS

int sum = 0;

for(int i=0;i<row;i++)
{
sum += table[i][1];
table[i][1] = sum;
}

//CS->NCS
int min = table[0][1], max = table[row-1][1];

for(int i=0;i<row;i++)
table[i][1] = Math.round((table[i][1]-min)/(max-min));

//Mapping
for(int j=0;j<bufImg.getHeight();j++)
for(int i=0;i<bufImg.getWidth();i++)
{
for(int k=0;k<row;k++)
if(intensity[i][j]==table[k][0])
intensity[i][j] = table[k][1];

Color newColor = new Color(intensity[i][j], intensity[i][j], intensity[i][j]);

bufImg.setRGB(i, j, newColor.getRGB());
}


return bufImg;
}

}

最佳答案

convert 方法可以简化为:

return new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY) ;

因为您只想要一个灰度(单 channel )图像。那么toGrayScale的方法是:

 BufferedImage bufImg = convert(img);
byte[] bufferbyte = ((DataBufferByte) bufImg.getRaster().getDataBuffer()).getData() ;
for(int j=0, pos=0; j < bufImg.getHeight(); j++)
for(int i=0; i < bufImg.getWidth(); i++, pos++)
{
int red = bufImg().getSample(i, j, 0) ;
int green = bufImg().getSample(i, j, 1) ;
int blue = bufImg().getSample(i, j, 2) ;
// gray = 0.299 * R + 0.587 * G + 0.114 * B
bufferbyte[pos] = (byte)(int)(0.299*red + 0.587*green + 0.114*blue) ;
}
return bufImg ;

我使用 Raster() 来获取 RBG 值,但直接访问 DataBuffer 会快得多(就像缓冲区字节一样)。

然后就可以做直方图均衡了,但是第一行是错误的,应该删除。

关于java - java中的直方图均衡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13997996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com