Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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
Arrays java图像字节数组将灰度恢复到图像_Arrays_Image Processing - Fatal编程技术网

Arrays java图像字节数组将灰度恢复到图像

Arrays java图像字节数组将灰度恢复到图像,arrays,image-processing,Arrays,Image Processing,我正在尝试对图像处理进行编码,因此我创建了一个方法,该方法接收buffereImage,并将其放入字节数组(方法名称converttobyterarray(buffereImage inImg)) 然后我从每个像素的字节中获取ARGB或RGB值(每个8位,使用逐位转换)。然后将ARGB或RGB的int值追加回字节数组(方法名convertToGrayscale(byte[]inImg)) 然后将字节数组反馈给一个方法以转换回图像(方法名convertToBufferedImage(byte[]i

我正在尝试对图像处理进行编码,因此我创建了一个方法,该方法接收
buffereImage
,并将其放入字节数组(方法名称
converttobyterarray(buffereImage inImg)

然后我从每个像素的字节中获取ARGB或RGB值(每个8位,使用逐位转换)。然后将ARGB或RGB的int值追加回字节数组(方法名
convertToGrayscale(byte[]inImg)

然后将字节数组反馈给一个方法以转换回图像(方法名
convertToBufferedImage(byte[]inImg)

出于某种原因,当我将
BufferedImage
传递到将其转换为字节数组的方法中时,然后在不做任何更改的情况下,将字节数组传递到将其转换回
BufferedImage
的方法中,它就起作用了

这就是我遇到的问题。当我将
buffereImage
传递到方法中以转换为字节数组,然后传递到方法中以将其更改为灰度,并在同一方法中将灰度int值更改为字节数组。问题是,当我将修改后的字节数组传递给方法以转换回
buffereImage
。。。结果是“img为空”“请帮助,要么我的转换错误,要么我不知道哪里或哪里出了问题

public byte[] convertToByteArray(BufferedImage inImg) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        ImageIO.write(inImg, "jpg", baos);
    } catch (IOException e) {
        e.printStackTrace();
    }
    outImgByte = baos.toByteArray();

    System.out.println("img to byte array length: " + outImgByte.length);
    System.out.println("convert to byte array complete...");
    return outImgByte;
}

public BufferedImage convertToBufferedImage(byte[] inImg) {
    try {
        outImg = ImageIO.read(new ByteArrayInputStream(inImg));
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("convert byte array to image complete...");
    return outImg;
}

public void convertToGrayscale(byte[] inImg) {
    int r, g, b;
    int grayscale;
    // for loop to change each byte value into gray scale
    for(int i = 0; i < inImg.length; i++) {
        // first must get the value of RGB out from byte 
        // array and get the average
        r = (inImg[i] >> 16) & 0xff;
        g = (inImg[i] >> 8) & 0xff;
        b = (inImg[i]) & 0xff;

        grayscale = (r + g + b) / 3;

        outImgByte[i] = (byte) (grayscale << 16 | grayscale << 8 |   grayscale);
    }
    System.out.println("byte to grayscale byte array length: " + outImgByte.length);
    System.out.println("convert to grayscale from byte array complete...");
}

buffereImage
后面是一个数组,很可能是一个字节数组(有时是短的)。因此,如果您的
buffereImage
类型为
type_BYTE_GRAY
type_3BYTE_BGR
type_4BYTE_ABGR
,那么您可以使用以下命令直接访问
DataBuffer
BYTE[]DataBuffer=((DataBufferByte)image.getRaster().getDataBuffer()).getData())。这是对像素值的直接访问。但是,要执行此操作,您必须自行管理图像编码,因此,如果
缓冲图像
的类型为
类型_USHORT\u GRAY
,则此操作将不起作用

如果希望以更简单(但速度较慢)的方式访问像素值,请使用光栅:

BufferedImage image = ...
for (int y=0 ; y < image.getHeight() ; y++)
    for (int x=0 ; x < image.getWidth() ; x++)
        for (int c=0 ; c < image.getRaster().getNumBands() ; c++)
            final int value = image.getRaster().getSample(x, y, c) ; // Returns the value of the channel C of the pixel (x,y)
[编辑2]

public BufferedImage ByteArrayToBufferedImage(byte[] src, final int width, final int height)
    {
    // Check first that width*height*3 == src.length
    BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR) ;
    System.arraycopy(src, ((DataBufferByte)result.getRaster().getDataBuffer()).getData(), src.length) ;
    return result ;
    }

ByteArrayToBufferedImage的函数
工作正常。
但是,这里是我稍微改变函数的地方

    public BufferedImage ByteArrayToBufferedImage(byte[] src, final int width, final int height) {
        //create empty bufferedImage
        BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
        //remove System.arraycopy (fill empty image with input byte array)
        result.getRaster().setDataElements(0, 0, width, height, src);
        return result;
    }
我没有理由使用
result.getRaster().setDataElements
而不是
System.arraycopy


如果有人知道任何不同的方法,请比较这两种方法。谢谢

convertToByteArray
not
convertToBufferedImage
hi FiReTiTi,非常感谢您的帮助和信息。我尝试使用DataBuffer和.getRaster()从图像中获取字节数组。现在我已经使用了这种方法,从字节数组到图像的转换甚至不起作用(图像->字节数组->图像)。在(图像->字节数组->图像)工作之前,它是(图像->字节数组->灰度->图像)不工作的。我从Chris那里得到一条评论,说我的灰度方法似乎出了问题。我似乎找不到或找不到哪里出了问题。你的图像是什么类型的?图像是没有alpha光栅的“jpg”,不确定这是否是你要问的。我问的是BuffereImage类型(type_BYTE_GRAY,type_BYTE_BGR,等等),你可以用getType()得到。我得到的返回值是5,它等于type_3BYTE_BGR
public BufferedImage ByteArrayToBufferedImage(byte[] src, final int width, final int height)
    {
    // Check first that width*height*3 == src.length
    BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR) ;
    System.arraycopy(src, ((DataBufferByte)result.getRaster().getDataBuffer()).getData(), src.length) ;
    return result ;
    }
    public BufferedImage ByteArrayToBufferedImage(byte[] src, final int width, final int height) {
        //create empty bufferedImage
        BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
        //remove System.arraycopy (fill empty image with input byte array)
        result.getRaster().setDataElements(0, 0, width, height, src);
        return result;
    }