Java에서 이미지 처리를 위해 하나의 응용 프로그램을 작성했습니다. 버퍼링 된 이미지가있는 이미지를 처리했으며 이제 이진화 된 이미지의 바이트 배열을 얻어야하는 해당 처리 된 이미지에 대해 byte[]
을 반환하려고합니다.버퍼링 된 이미지를 동일한 데이터가있는 2D 바이트 배열로 변환
여기 내 코드입니다 :
public static byte[][] binarizeImage(BufferedImage bfImage){
int red;
int newPixel;
int h ;
int w ;
int threshold = otsuTreshold(bfImage);
// this function returns the threshold value 199
BufferedImage binarized = new BufferedImage(bfImage.getWidth(), bfImage.getHeight(), bfImage.getType());
for(int i=0; i<bfImage.getWidth(); i++) {
for(int j=0; j<bfImage.getHeight(); j++) {
// Get pixels
red = new Color(bfImage.getRGB(i, j)).getRed();
int alpha = new Color(bfImage.getRGB(i, j)).getAlpha();
if(red > threshold) {
newPixel = 255;
}
else {
newPixel = 0;
}
newPixel = colorToRGB(alpha, newPixel, newPixel, newPixel);
binarized.setRGB(i, j, newPixel);
}
}
Raster raster = binarized.getData();
h = raster.getHeight();
w = raster.getWidth();
byte[][] binarize_image = new byte[w][h];
for(int i=0 ; i<w ; i++)
{
for(int j=0; j<h ; j++)
{
binarize_image[i][j]=raster.getSampleModel(); //error at this line
}
}
return binarize_image;
}
// Convert R, G, B, Alpha to standard 8 bit
private static int colorToRGB(int alpha, int red, int green, int blue) {
int newPixel = 0;
newPixel += alpha;
newPixel = newPixel << 8;
newPixel += red; newPixel = newPixel << 8;
newPixel += green; newPixel = newPixel << 8;
newPixel += blue;
return newPixel;
}
그러나 그것은 작동하지 않습니다. 버퍼링 된 이미지를 동일한 이미지 데이터의 바이트 배열로 변환하려면 어떻게해야합니까?
? 8 비트 흑백/8 비트 알파/픽셀? 그렇다면 픽셀 당 8 비트로 어떻게 저장할 것입니까? 이것은 내가 바이너리로 생각하는 것이 아니라 예술적 효과를 창출하는 것일까? 어쨌든 임시 "binarized"이미지없이 빠져 나와 첫 번째 루프의 값을'binarize_image'에 직접 설정하면됩니다. – haraldK
코드를 작성하는 주된 목적은 회색 이미지를 이진화 된 이미지로 변환하고 싶습니다. 그리고 위의 코드로 시도하고 있습니다. – rachana
"이진화 된"이미지 란 무엇입니까? 흑백과 같이 이진을 의미합니까? 입력 이미지가 항상 회색입니까? 8 비트? – haraldK