Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中读取黑白图像,类型为_USHORT_GRAY_Java_Image_Bufferedimage - Fatal编程技术网

在java中读取黑白图像,类型为_USHORT_GRAY

在java中读取黑白图像,类型为_USHORT_GRAY,java,image,bufferedimage,Java,Image,Bufferedimage,我有以下代码来阅读java中的黑白图片 imageg = ImageIO.read(new File(path)); BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_USHORT_GRAY); Graphics g = bufferedImage.createGraphics();

我有以下代码来阅读java中的黑白图片

imageg = ImageIO.read(new File(path));    
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),  BufferedImage.TYPE_USHORT_GRAY);
        Graphics g = bufferedImage.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        int w = img.getWidth();
        int h = img.getHeight();
        int[][] array = new int[w][h];
        for (int j = 0; j < w; j++) {
            for (int k = 0; k < h; k++) {
                array[j][k] = img.getRGB(j, k);
                System.out.print(array[j][k]);
            }
        }
imageg=ImageIO.read(新文件(路径));
BuffereImage BuffereImage=新的BuffereImage(image.getWidth(null)、image.getHeight(null)、BuffereImage.TYPE\u USHORT\u GRAY);
Graphics g=BuffereImage.createGraphics();
g、 drawImage(image,0,0,null);
g、 处置();
int w=img.getWidth();
inth=img.getHeight();
int[][]数组=新的int[w][h];
对于(int j=0;j

如您所见,我已将BuffereImage的类型设置为type_USHORT_GRAY,我希望在两个D数组mattrix中看到0到255之间的数字。但我会看到“-1”和另一个大整数。有人能突出显示我的错误吗?

一个
类型的
缓冲图像
,它的名字说它使用16位存储像素(
short
的大小是16位)。范围
0..255
仅为8位,因此颜色可能远远超过255。
buffereImage.getRGB()
不返回这16个像素的数据位,而是引用其:

返回默认RGB颜色模型中的整数像素(键入\u INT \u ARGB)和默认sRGB颜色空间


getRGB()
将始终以RGB格式返回像素,而不管
buffereImage

的类型如何。正如评论和回答中已经提到的,错误在于使用
getRGB()
方法,该方法在默认sRGB颜色空间中将像素值转换为压缩
int
格式(
TYPE\u INT\u ARGB
)。在此格式中,
-1
与“0xffffffff”相同,表示纯白色

要直接访问未签名的
short
像素数据,请尝试:

int w = img.getWidth();
int h = img.getHeight();

DataBufferUShort buffer = (DataBufferUShort) img.getRaster().getDataBuffer(); // Safe cast as img is of type TYPE_USHORT_GRAY 

// Conveniently, the buffer already contains the data array
short[] arrayUShort = buffer.getData();

// Access it like:
int grayPixel = arrayUShort[x + y * w] & 0xffff;

// ...or alternatively, if you like to re-arrange the data to a 2-dimensional array:
int[][] array = new int[w][h];

// Note: I switched the loop order to access pixels in more natural order
for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
        array[x][y] = buffer.getElem(x + y * w);
        System.out.print(array[x][y]);
    }
}

// Access it like:
grayPixel = array[x][y];
intw=img.getWidth();
inth=img.getHeight();
DataBufferUShort buffer=(DataBufferUShort)img.getRaster().getDataBuffer();//安全强制转换,因为img的类型为\u USHORT\u GRAY
//很方便,缓冲区已经包含了数据数组
short[]arrayUShort=buffer.getData();
//访问它的方式如下:
int grayPixel=ArrayShort[x+y*w]&0xffff;
//…或者,如果要将数据重新排列为二维数组:
int[][]数组=新的int[w][h];
//注意:我切换了循环顺序,以更自然的顺序访问像素
对于(int y=0;y

PS:看一下@blackSmith提供的第二个链接可能仍然是一个好主意,以获得正确的颜色到灰色的转换。-

这是由于使用了
getRGB
方法。如果您对二进制(纯黑白)图像感兴趣,请看一看,如果您打算读取灰度,则需要一个转换公式。Icza,也感谢您对“短灰色”类型的解释。我认为它是8位的。您不能将
DataBuffer
强制转换为
DataBufferUShort
@DanielMårtenson,因此您可以在编译时始终强制转换为更特定的类型。考虑到OPs图像是
类型\u USHORT\u GRAY
,这也是运行时的安全强制转换。如果它不适合你,你的图像可能有不同的类型。@DanielMårtensson我从你的一个问题中看到,你可能有一个
类型的字节灰
图像。在这种情况下,强制转换必须是
DataBufferByte
,数组将是
byte[]
。值在0…255范围内。