Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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 从图像字节数组中提取并堆叠高字节和低字节_Java_Image_Byte_Preprocessor_Data Extraction - Fatal编程技术网

Java 从图像字节数组中提取并堆叠高字节和低字节

Java 从图像字节数组中提取并堆叠高字节和低字节,java,image,byte,preprocessor,data-extraction,Java,Image,Byte,Preprocessor,Data Extraction,我对字节级的图像压缩比较陌生,目前正在开发一个java图像预处理器,该处理器将获取bmp图像,将其转换为8位无符号灰度,然后在导出和压缩之前根据高低堆叠字节。经过一些广泛的研究和测试各种字节提取方法后,我仍然没有看到我需要的结果。在我继续之前,应该注意的是,所有这些图像最初都是DICOM格式的,我正在使用ij.plugin.DICOM包将像素数据提取为bmp图像 以下描述由下面的代码表示。目前,我正在读取原始图像作为缓冲图像,将其转换为灰度,然后从光栅中获取图像字节。然后我获取这些字节,并使用我

我对字节级的图像压缩比较陌生,目前正在开发一个java图像预处理器,该处理器将获取bmp图像,将其转换为8位无符号灰度,然后在导出和压缩之前根据高低堆叠字节。经过一些广泛的研究和测试各种字节提取方法后,我仍然没有看到我需要的结果。在我继续之前,应该注意的是,所有这些图像最初都是DICOM格式的,我正在使用ij.plugin.DICOM包将像素数据提取为bmp图像

以下描述由下面的代码表示。目前,我正在读取原始图像作为缓冲图像,将其转换为灰度,然后从光栅中获取图像字节。然后我获取这些字节,并使用我在stackoverflow上找到的一些其他代码,将它们“转换”为二进制位的字符串表示形式。然后我将该字符串发送到一个字符数组。下一步可能是无关的,但我想在删除它之前获得您的输入(因为我是新手)。我创建一个位集并遍历“二进制”字符数组。如果字符值为“1”,我将位集中的位置设置为true。然后我将位集发送到另一个字节数组

然后我创建了两个新的字节数组,一个用于高字节,一个用于低字节。使用for循环,我在“bit”数组上迭代,并根据数组中的位置将每4个“bit”存储在高字节或低字节中

最后,我获取DICOM标记数据,从中生成一个字节数组,然后将标记数组、高字节数组和低字节数组堆叠在一起。我的预期结果是将图像矩阵“拆分”,上半部分包含所有高字节,下半部分包含所有低字节。我被告知标记字节将非常小,它们不应该影响最终结果(我测试了没有它们的图像,只是为了确定,没有明显的差异)

下面是代码。请让我知道如果你有任何问题,我会相应地修改我的帖子。我试着把所有相关数据都包括进去。如果你需要更多,请告诉我

        BufferedImage originalImage = getGrayScale(img.getBufferedImage());//returns an 8-bit unsigned grayscale conversion of the original image
        byte[] imageInByte = ((DataBufferByte) originalImage.getRaster().getDataBuffer()).getData();
        String binary = toBinary(imageInByte); //converts to a String representation of the binary bits
        char[] binCharArray = binary.toCharArray();
        BitSet bits = new BitSet(binCharArray.length);
        for (int i = 0; i < binCharArray.length; i++) {
            if (binCharArray[i] == '1') {
                bits.set(i);
            }
        }
        imageInByte = bits.toByteArray();

        byte[] high = new byte[(int) imageInByte.length/2];
        byte[] low = new byte[(int) imageInByte.length/2];

        int highC = 0;
        int lowC = 0;
        boolean change = false; //start out storing in the high bit
        //change will = true on very first run. While true, load in the high byte array. Else low byte
        for(int i = 0; i < imageInByte.length; i++){
            if(i % 4 == 0){
                change = !change;
            }
            if(change){
                high[highC] = imageInByte[i];
                highC++;
            } else {
                low[lowC] = imageInByte[i];
                lowC++;
            }
        }
        //old code from a previous attempt. 
     // for (int j = 0; j < imageInByte.length; j++) {
     //       byte h = (byte) (imageInByte[j] & 0xFF);
     //       byte l = (byte) ((imageInByte[j] >> 8) & 0xFF);
     //       high[j] = h;
     //       low[j] = l;
     // }

        OutputStream out = null;
        //add this array to the image array. It goes at the beginning.
        byte[] tagBytes = dicomTags.getBytes();
        currProcessingImageTagLength = tagBytes.length;
        imageInByte = new byte[high.length + low.length + tagBytes.length];
        System.arraycopy(tagBytes, 0, imageInByte, 0, tagBytes.length);
        System.arraycopy(high, 0, imageInByte, tagBytes.length, high.length);
        System.arraycopy(low, 0, imageInByte, tagBytes.length + high.length, low.length);

        BufferedImage bImageFromConvert = new BufferedImage(dimWidth, dimHeight, BufferedImage.TYPE_BYTE_GRAY);//dimWidth and dimHeight are the image dimensions, stored much earlier in this function
        byte[] bufferHolder = ((DataBufferByte) bImageFromConvert.getRaster().getDataBuffer()).getData();
        System.arraycopy(imageInByte, 0, bufferHolder, 0, bufferHolder.length);
     //This is where I try and write the final image before sending it off to an image compressor
        ImageIO.write(bImageFromConvert, "bmp", new File(
                directory + fileName + "_Compressed.bmp"));
        return new File(directory + fileName + "_Compressed.bmp");
编辑2:我根据要求添加了一些图像

电流输出:


注意,由于我的声誉低于10,我无法发布具有“预期”高字节和低字节结果的图像。

这表示每4个字节更改一次;这不是你想要的:

    for(int i = 0; i < imageInByte.length; i++){
        if(i % 4 == 0){
            change = !change;
        }
        if(change){
            high[highC] = imageInByte[i];
            highC++;
        } else {
            low[lowC] = imageInByte[i];
            lowC++;
        }
    }
for(int i=0;i
我会用你之前尝试的这个来代替它

  for (int j = 0; j < imageInByte.length; j+=2) {
        byte h = (byte) (imageInByte[j] & 0xF0);
        byte h2 = (byte) (imageInByte[j+1] & 0xF0);
        byte l = (byte) (imageInByte[j] & 0x0f);
        byte l2 = (byte) (imageInByte[j+1] & 0x0f);
        high[j/2] = h|(h2>>4);
        low[j/2] = (l<<4)|l2;
  }
for(int j=0;j>4);

低[j/2]=(l这表示每4个字节更改一次;这不是您想要的:

    for(int i = 0; i < imageInByte.length; i++){
        if(i % 4 == 0){
            change = !change;
        }
        if(change){
            high[highC] = imageInByte[i];
            highC++;
        } else {
            low[lowC] = imageInByte[i];
            lowC++;
        }
    }
for(int i=0;i
我会用你之前尝试的这个来代替它

  for (int j = 0; j < imageInByte.length; j+=2) {
        byte h = (byte) (imageInByte[j] & 0xF0);
        byte h2 = (byte) (imageInByte[j+1] & 0xF0);
        byte l = (byte) (imageInByte[j] & 0x0f);
        byte l2 = (byte) (imageInByte[j+1] & 0x0f);
        high[j/2] = h|(h2>>4);
        low[j/2] = (l<<4)|l2;
  }
for(int j=0;j>4);

low[j/2]=(la)“将图像矩阵“拆分”,上半部分包含所有高字节,下半部分包含所有低字节。”:那么您希望将所有字节的高部分集中在图像的上半部分?预期结果是什么?b)也许这一切都与dicom有关,我对dicom一无所知,但被认为是处理dicom的简单步骤,它应该对您的期望和您的需求有意义get@gpascha)是。图像应显示在顶部,所有高字节聚集在顶部,所有低字节聚集在底部。换句话说,最终图像应为原始d尺寸,但图像的上半部分是一个小亮图像,下半部分是一个小暗图像。b)出于所有意图和目的,在处理DICOM时,它应起到正常bmp的作用。a)“分割图像矩阵”“上半部分包含所有高字节,下半部分包含所有低字节。":因此,您希望将所有字节的高位集中在图像的上半部分?预期结果是什么?b)可能这一切都与dicom有关,我不知道这是什么,但被认为是简单的处理步骤。它应该理解您的期望和您的期望get@gpascha)是的。图像应显示所有高字节聚集在顶部,所有低字节聚集在底部。换句话说,最终图像应为原始尺寸,但图像的上半部分是一个小亮图像,下半部分是一个小暗图像。b)出于所有目的和目的,在处理DICOM时,它应像正常的bmp.Hmm一样工作,它看起来更接近我所需要的,但它仍然有点偏离。让我看一下代码,确保它不是我正在做的事情。第一部分是灰色,我不知道它是如何工作的,它是否正确,然后中间有很多的位子,你有二进制,我不知道为什么你需要所有这些。串和回-和最终