【问题标题】:java how to get a boolean array from a black and white imagejava如何从黑白图像中获取布尔数组
【发布时间】:2015-04-12 20:20:25
【问题描述】:

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

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

【问题讨论】:

  • 你的意思是二维码扫描仪之类的东西吗?
  • 如果你使用的是 BufferedImage,你可以在上面调用getRGB 函数,然后使用val = image.getRGB(x,y) > 0 ? true : false,我猜。
  • @BenWin 我不确定 QR 码扫描仪是如何工作的,但我想这个概念是完全相同的。

标签: java image bitmap boolean


【解决方案1】:

如果您想要一些非常简单的黑白图像,也许您可​​以将图像导出为 .pbm 格式并将其作为文本文件使用,或者如果您将其导出为 .ppm,您甚至可能不需要处理它,具体取决于你想要什么。
看看these 格式

【讨论】:

    【解决方案2】:

    位图已经是一个数字。只需打开它并阅读它。

    image = ImageIO.read(getClass().getResourceAsStream("path/to/your/file.bmp"));
    int color = image.getRGB(x, y);
    BigInteger biColor = BigInteger.valueOf(color); 
    

    【讨论】:

      【解决方案3】:
      BufferedImage bi = yourImage;
      
      //the number of bytes required to store all bits
      double size = ((double) bi.getWidth()) * ((double) bi.getHeight()) / 8;
      int tmp = (int) size;
      if(tmp < size)
          tmp += 1;
      
      byte[] b = new byte[tmp];
      int bitPos = 7;
      int ind = 0;
      
      for(int i = 0 ; i < bi.getHeight() ; i++)
           for(int j = 0 ; j < bi.getWidth() ; j++){
               //add a 1 at the matching position in b, if this pixel isn't black
               b[ind] |= (bi.getRgb(j , i) > 0 ? 0 : (1 << bitPos));
      
               //next pixel -> next bit
               bitPos -= 1;
               if(bitPos == -1){//the current byte is filled with continue with the next byte
                   bitPos = 7;
                   ind++;
               }
           }
      
      BigInteger result = new BigInteger(b);
      

      【讨论】:

      • 这个算法不起作用,我在我的图像上运行它,返回的值是-64...我建议完全摆脱byte[]并在@上执行位操作987654324@ 本身我尝试自己调整它,但老实说,当涉及到按位运算,尤其是位图时,我已经脱离了我的元素......我尝试自己调整代码但没有运气,如果我得到,我会在这里发布它。
      • 你可以这样做。但我想有几个问题:我的算法生成一个二进制补码表示。这意味着,如果第一个像素不是黑色,您将收到一个负值 - 您应该指定应该如何解释图像(我猜您毕竟想要一个无符号值)。图像的黑色像素不是完全黑色(RGB == 0),因此该算法仅产生 1 位。只需替换 1bits 的条件。
      【解决方案4】:

      有专门用于图像处理的Java API ImageJ,这里是你可以下载必要Jar的链接,http://imagej.nih.gov/ij/download.html和文档链接http://imagej.nih.gov/ij/docs/index.html

      有几个教程和示例可用于使用此 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()]);
      

      这里是更多示例的另一个链接,http://albert.rierol.net/imagej_programming_tutorials.html#ImageJ

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-15
        • 2015-09-19
        • 1970-01-01
        • 1970-01-01
        • 2011-09-25
        • 2022-12-15
        相关资源
        最近更新 更多