Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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_Bitmap_Boolean - Fatal编程技术网

java如何从黑白图像中获取布尔数组

java如何从黑白图像中获取布尔数组,java,image,bitmap,boolean,Java,Image,Bitmap,Boolean,我有一张用ms paint制作的图像,它是106x17,我想把整个位图变成一个数字。图像本身存储为.png,我需要一种方法来读取图像,并将每个像素作为位存储在BigInteger中。我需要的图像阅读的方式是相当具体的,有点奇怪。。。图像需要从上到下从右到左逐行读取。。。因此,右上角的像素应该是数字中的第一位,最左下角的像素应该是数字中的最后一位 编辑:我可能需要澄清一点,因为文件存储为.png我不能将其作为一个数字来读取,我会在发布此更新后立即尝试将其导出为位图图像。此外,我将其存储在bigin

我有一张用ms paint制作的图像,它是
106x17
,我想把整个位图变成一个数字。图像本身存储为
.png
,我需要一种方法来读取图像,并将每个像素作为位存储在
BigInteger
中。我需要的图像阅读的方式是相当具体的,有点奇怪。。。图像需要从上到下从右到左逐行读取。。。因此,右上角的像素应该是数字中的第一位,最左下角的像素应该是数字中的最后一位


编辑:我可能需要澄清一点,因为文件存储为
.png
我不能将其作为一个数字来读取,我会在发布此更新后立即尝试将其导出为位图图像。此外,我将其存储在
biginger
中,因为该数字的长度应为
106x17=1802
位,因此该数字不能首先通过int或long传递,因为它会丢失大部分信息。最后,在这个上下文中,黑色像素代表1,白色像素代表0。。。很抱歉这些奇怪的约定,但这或多或少是我正在使用的。

如果您想要非常简单的黑白图像,也许您可以将图像导出为.pbm格式并将其作为文本文件使用,或者如果您将其导出为.ppm,您甚至可能不需要根据需要对其进行处理。

看看格式

位图已经是一个数字了。只要打开它读一读

image = ImageIO.read(getClass().getResourceAsStream("path/to/your/file.bmp"));
int color = image.getRGB(x, y);
BigInteger biColor = BigInteger.valueOf(color); 
buffereImage bi=yourImage;
//存储所有位所需的字节数
double size=((double)bi.getWidth())*((double)bi.getHeight())/8;
int tmp=(int)大小;
如果(tmp<尺寸)
tmp+=1;
字节[]b=新字节[tmp];
int-bitPos=7;
int ind=0;
对于(int i=0;i0?0:(1下一位
bitPos-=1;
如果(bitPos==-1){//当前字节中填充了continue和next字节
bitPos=7;
ind++;
}
}
BigInteger结果=新的BigInteger(b);

有专门用于图像处理的Java API
ImageJ
,这里是您可以下载必要Jar的链接,以及文档链接

有几个教程和示例可用于使用此API对图像执行基本操作,我将尝试为您的任务编写基本代码

    // list of points
    List<Point> pointList = new ArrayList<>();

    ImagePlus imp = IJ.openImage("/path/to/image.tif"); 
    ImageProcessor imageProcessor = imp.getProcessor(); 

    // width and height of image
    int width = imageProcessor.getWidth();
    int height = imageProcessor.getHeight();

    // iterate through width and then through height
    for (int u = 0; u < width; u++) {
        for (int v = 0; v < height; v++) {
            int valuePixel = imageProcessor.getPixel(u, v);
            if (valuePixel > 0) {
                pointList.add(new Point(u, v));
            }
        }
    }

    // convert list to array
    pointList.toArray(new Point[pointList.size()]);
//点列表
List pointList=new ArrayList();
ImagePlus imp=IJ.openImage(“/path/to/image.tif”);
ImageProcessor ImageProcessor=imp.getProcessor();
//图像的宽度和高度
int width=imageProcessor.getWidth();
int height=imageProcessor.getHeight();
//迭代宽度,然后迭代高度
用于(int u=0;u0){
添加(新点(u,v));
}
}
}
//将列表转换为数组
toArray(新点[pointList.size()]);

这里还有一个链接提供更多示例,

您是指类似二维码扫描仪的东西吗?如果您使用的是BuffereImage,您可以在其上调用getRGB函数,然后只需使用val=image.getRGB(x,y)>0?对:错,我猜。@BenWin我不确定二维码扫描仪是如何工作的,但我想这个概念是完全相同的。这个算法不起作用,我在我的图像上运行了它,返回的值是
-64
…我建议去掉
字节[]
总之,在
biginger
本身上执行位操作,我曾尝试过自己调整它,但老实说,在按位操作方面,尤其是位图方面,我不太在行……我尝试过自己调整代码,但运气不好,如果我得到它,我会把它发布在这里。你可以这样做。但我想有几个问题:我的算法生成两个补码表示。这意味着,如果第一个像素不是黑色的,您将收到一个负值-您应该指定如何解释图像(我猜您毕竟想要一个无符号值)。图像的黑色像素不是完全黑色的(RGB==0),因此该算法只生成1位。只需替换1位的条件即可。
    // list of points
    List<Point> pointList = new ArrayList<>();

    ImagePlus imp = IJ.openImage("/path/to/image.tif"); 
    ImageProcessor imageProcessor = imp.getProcessor(); 

    // width and height of image
    int width = imageProcessor.getWidth();
    int height = imageProcessor.getHeight();

    // iterate through width and then through height
    for (int u = 0; u < width; u++) {
        for (int v = 0; v < height; v++) {
            int valuePixel = imageProcessor.getPixel(u, v);
            if (valuePixel > 0) {
                pointList.add(new Point(u, v));
            }
        }
    }

    // convert list to array
    pointList.toArray(new Point[pointList.size()]);