java中的灰度图像日志转换

时间:2016-04-25 06:33:39

标签: java image-processing transform bufferedimage

我正试图从灰色图像中获取日志转换。但无论c值如何,我都会得到一张黑色图像。有什么想法吗?

这是我的方法:

/// -------------------------------------------- -------

public static BufferedImage log_trans(int [] [] imageData,int c){

        BufferedImage LogImage = new BufferedImage(imageData.length, imageData[0].length, BufferedImage.TYPE_BYTE_GRAY);
        double temp;
        for (int i =0 ; i<imageData.length ; i ++){
        for (int j=0 ; j<imageData[0].length ; j++){
                int rgb = imageData[i][j];
                rgb = (rgb<<16)|(rgb<<8)|(rgb);
                temp = Math.log10(rgb+1);
                rgb = (int) (c * temp); 
                LogImage.setRGB(i, j, rgb);
        }}

        return LogImage;
    }

--------------------------------------------------------------
public static int[][] readimage(File filename){
    BufferedImage img;

    try {
        img = ImageIO.read(filename);

        // Gray_scaled Image output

        int width = img.getWidth();
        int height = img.getHeight();
        ImagePro.fw=width;
        ImagePro.fh = height;
        int [][] readimageVal = new int [width][height];
        for (int i = 0; i<height ; i++){
            for (int j =0  ; j<width ; j++){

                Color c = new Color(img.getRGB(j, i));
                   int r= (int)(c.getRed() * 0.299)&0xff;
                   int g = (int)(c.getGreen() * 0.587)&0xff;
                   int b = (int)(c.getBlue() *0.114)&0xff;
                   int avg = ((r+b+g));

                readimageVal[j][i] = avg;
                    }
                    }
        return readimageVal;

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

1 个答案:

答案 0 :(得分:1)

似乎“rgb”总是负面的,所以temp总是Nan,所以最后“rgb”之后:

rgb = (int) (c * temp);

始终为0,这就是为什么你总是得到黑色图片。

将第8行更改为:

rgb =(((byte)rgb&amp; 0xFF)&lt;&lt; 16)|(((byte)rgb&amp; 0xFF)&lt;&lt; 8)|(((byte)rgb&amp; 0xFF)) ;

我得到一些非常暗的输出,但它不是很好。我测试了“c”的值为1,18000,180000和0x00FFFFFF。