跳过像素问题

时间:2012-11-14 16:47:33

标签: java

我正在编写一个程序,它将获取qr代码图像并识别黑/白像素并将它们保存在一个数组中。但我的问题是;我有294x294像素图像和一个固定大小为21x21的矩阵。所以一个方形模块对我来说是294/21 = 4.0像素。如何按4.0像素到4.0像素搜索像素?这是我的代码:

public static void printAllARGBDetails(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();
        System.out.println("Image Dimension: Height-" + height + ", Width-"
                + width);
        System.out.println("Total Pixels: " + (height * width));
        int pixels[][]=new int[21][21];
        int pixel=width/21;
        System.out.println("Pixel=" +pixel);

        for (int i = 20; i>=0 ; i--) {
            for (int j = 20; j>=0 ; j--) {
                if(image.getRGB(i, j)==-1)
                {

                    pixels[i][j]=0;
                }
                else
                {
                    pixels[i][j]=1;
                }

            }
        } 
  }

`

2 个答案:

答案 0 :(得分:0)

下面编辑的代码应该可以工作,但它只测试每个元素中16个像素中的1个

    for (int i = 0; i<20 ; i++) {
        for (int j = 0; j<20 ; j++) {
            if(image.getRGB(i*4, j*4)==-1)
            {

                pixels[i][j]=0;
            }
            else
            {
                pixels[i][j]=1;
            }

        }
    } 

答案 1 :(得分:0)

你应该引入一种方法,以某种方式将4x4像素数组评估为01,例如基于平均光度:

public static void printAllARGBDetails(BufferedImage image) {
    int width = image.getWidth();
    int height = image.getHeight();
    System.out.println("Image Dimension: Height-" + height + ", Width-"
            + width);
    System.out.println("Total Pixels: " + (height * width));
    int pixels[][]=new int[21][21];
    int pixel=width/21;
    System.out.println("Pixel=" +pixel);

    for (int i = 20; i>=0 ; i--) {
        for (int j = 20; j>=0 ; j--) {
                pixels[i][j] = getPixel (image, i, j);
                System.out.println(String.format ("Pixel[%d,%d]=%d", i, j, pixels[i][j]));
        }
    } 
}

private static final long LUMINOSITY_THRESHOLD = 23000;

private static int getPixel (BufferedImage image, int i, int j) {
   long totalLuminosity = 0;
   for (int k = 0; k < 4; k++) {
      for (int m = 0; m < 4; m++) {
          long luminosity = getLuminosity (image.getRGB (i*4 + k, j*4 + m));
          totalLuminosity += luminosity;
      }
   }

   return totalLuminosity/16 > LUMINOSITY_THRESHOLD ? 0 : 1;
}

private static int getLuminosity (int rgb) {
    int r = rgb >> 16;
    int g = (rgb >> 8) & 0xFF;
    int b = rgb & 0xFF;

    int luminosity = 21*r + 71*g + 7*b;

    return luminosity;
}