2014-01-07 14 views
-4

我是Java的初學者。目前我想分析兩個相似的圖像,並檢查圖像在位置2和4處是否具有不同的像素值。我已經開發了一些代碼,但是在運行時,代碼會產生錯誤,並且不會循環並檢查所有像素值兩個圖像。分析兩個相似圖像上的像素值時發生錯誤

例如,在第9個像素中,位置2和4處的圖像B像素值與圖像A像素值不同。然後,只要代碼注意到兩個圖像像素值之間的差異,它就會輸出說明像素不相同的語句。

下面的代碼:

public class getPixelRGB1 
{ 

private static String[][] img_hex2; 
private static String[][] img_hex4; 
private static String[][] img2_hex2; 
private static String[][] img2_hex4; 


public static void main(String[] args) throws IOException 
{ 
    FileInputStream image = null; 
    FileInputStream image2 = null; 

    getPixelData1 newPD = new getPixelData1(); 
    compareHexaRGB hexRGB = new compareHexaRGB(); 


    try { 
     BufferedImage img, img2; 

     File file = new File("eye1.jpg"); 
     File file2 = new File("eye2.jpg"); 
     image = new FileInputStream(file); 
     image2 = new FileInputStream(file2); 
     img = ImageIO.read(image); 
     img2 = ImageIO.read(image2); 

     int rowcol; 
     int width = img.getWidth(); 
     int height = img.getHeight(); 
     hexRGB.compareHexaRGB(width, height); 
     System.out.println("Image's Width: " + width); 
     System.out.println("Image's Height: " + height); 
     //hexRGB.check(); 
     int[][] pixelData = new int[width * height][3]; 

     System.out.println("Pixel Data: " + pixelData); 

     int[] rgb; 
     int count = 0; 

     img_hex2 = new String[width][height]; 
     img_hex4 = new String[width][height]; 

     for(int i=0; i<width; i++) 
     { 
      for(int j=0; j<height; j++) 
      { 
       rgb = newPD.getPixelData(img, i, j); 

       for(int k = 0; k < rgb.length; k++) 
       { 
        pixelData[count][k] = rgb[k]; 
        //img_hex2[i][j] = newPD.getHexa2(); 
        //img_hex4[i][j] = newPD.getHexa4(); 
       } 
       img_hex2[width][height] = newPD.getHexa2(); 
       img_hex4[width][height] = newPD.getHexa4(); 
       System.out.println("Output: " + img_hex2[i][j]); 
       System.out.println("Output: " + img_hex4[i][j]); 
       count++; 
       System.out.println("\nRGB Counts: " + count); 
      } 
     } 

     int width2 = img2.getWidth(); 
     int height2 = img2.getHeight(); 
     System.out.println("Image's Width: " + width2); 
     System.out.println("Image's Height: " + height2); 

     int[][] pixelData2 = new int[width2 * height2][3]; 

     System.out.println("Pixel Data: " + pixelData2); 

     int[] rgb2; 
     int counter = 0; 

     img_hex2 = new String[width2][height2]; 
     img_hex4 = new String[width2][height2]; 

     for(int i=0; i<width2; i++) 
     { 
      for(int j=0; j<height2; j++) 
      { 
      rgb2 = newPD.getPixelData(img2, i, j); 

      for(int k = 0; k < rgb2.length; k++) 
      { 
      pixelData2[counter][k] = rgb2[k]; 

      } 
      img2_hex2[width2][height2] = newPD.getHexa2(); 
      img2_hex4[width2][height2] = newPD.getHexa4(); 
      counter++; 
      System.out.println("\nRGB2 Counts: " + counter); 
      } 
     } 

    } 
    catch (FileNotFoundException ex) { 
     Logger.getLogger(getPixelRGB1.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    finally 
    { 
     try { 
      image.close(); 
     } 
     catch (IOException ex) { 
      Logger.getLogger(getPixelRGB1.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
    hexRGB.check(); 
} 

public String[][] display_imgHex2() 
{ 
    return img_hex2; 
} 

public String[][] display_imgHex4() 
{ 
    return img_hex4; 
} 

public String[][] display_img2Hex2() 
{ 
    return img2_hex2; 
} 

public String[][] display_img2Hex4() 
{ 
    return img2_hex4; 
} 
} 

//獲取像素RGB過程

public class getPixelData1 
{ 
private static final double bitPerColor = 4.0; 
private static int red; 
private static int green; 
private static int blue; 
private static String hexa2; 
private static String hexa4; 

public static int[] getPixelData(BufferedImage img, int w, int h) throws IOException 
{ 
    int argb = img.getRGB(w, h); 
    int rgb[] = new int[] 
    { 
     (argb >> 16) & 0xff, //red 
     (argb >> 8) & 0xff, //green 
     (argb  ) & 0xff //blue 
    }; 

    red = rgb[0]; 
    green = rgb[1]; //RGB Value in Decimal 
    blue = rgb[2]; 

    System.out.println("\nRGBValue in Decimal --> " + "\nRed: " + red + " Green: " + green + " Blue: " + blue); 

    //Convert each channel RGB to Hexadecimal value 
    String rHex = Integer.toHexString((int)(red)); 
    String gHex = Integer.toHexString((int)(green)); 
    String bHex = Integer.toHexString((int)(blue)); 

    System.out.println("\nRGBValue in Hexa --> " + "\nRed Green Blue " + rHex + gHex + bHex); 

    return rgb; 
} 

public String getHexa2() 
{ 
    //Check position 2 of hexa value for any changes 

    String hex = String.format("%02X%02X%02X", red, green, blue); 
    System.out.println("\nString RGB Hexa: " + hex); 

    hexa2 = hex.substring(1,2); 
    System.out.println("\nSubstring at position 2: " + hexa2); 

    return hexa2; 
} 

public String getHexa4() 
{ 
    //Check position 4 of hexa value for any changes 

    String hex = String.format("%02X%02X%02X", red, green, blue); 
    System.out.println("\nString RGB Hexa: " + hex); 

    hexa4 = hex.substring(3,4); 
    System.out.println("\nSubstring at position 4: " + hexa4); 

    return hexa4; 
} 

} 

//比較2個圖像處理

public class compareHexaRGB 
{ 
private static int w; 
private static int h; 

public static void compareHexaRGB(int width, int height) throws IOException 
{ 
    w = width; 
    h = height; 

} 

public void check() 
    { 
     getPixelRGB1 newPD = new getPixelRGB1(); 

     for(int i = 0; i < w; i++) 
     { 
      for(int j = 0; j < h; j++) 
      { 
       if(newPD.display_imgHex2().equals(newPD.display_img2Hex2()) && (newPD.display_imgHex4().equals(newPD.display_img2Hex4()))) 
       { 
        System.out.println("Pixel values at position 2 and 4 are the same."); 
       } 

       else if(!newPD.display_imgHex2().equals(newPD.display_img2Hex2()) || (!newPD.display_imgHex4().equals(newPD.display_img2Hex4()))) 
       { 
        System.out.println("Pixel values at position 2 are not the same."); 
       } 
       else if(!newPD.display_imgHex2().equals(newPD.display_img2Hex2()) || (!newPD.display_imgHex4().equals(newPD.display_img2Hex4()))) 
       { 
        System.out.println("Pixel values at position 4 are not the same."); 
       } 
      } 
     } 
    } 
} 

錯誤:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 
at getPixelRGB1.main(getPixelRGB1.java:79) 
+0

所以這是一個NullPointerException!順便說一句,你有一個空值,使用調試器是好東西!你可以輕鬆找到問題的根源! –

回答

0

您所做的更改根本上是不正確的。請參閱更新的課程。

import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

import javax.imageio.ImageIO; 

public class getPixelRGB1 { 

    private static String[][] img_hex2; 
    private static String[][] img_hex4; 
    private static String[][] img2_hex2; 
    private static String[][] img2_hex4; 

    public static void main(String[] args) throws IOException { 
     FileInputStream image = null; 
     FileInputStream image2 = null; 

     getPixelData1 newPD = new getPixelData1(); 
     compareHexaRGB hexRGB = new compareHexaRGB(); 

     try { 
      BufferedImage img, img2; 

      File file = new File("eye1.jpg"); 
      File file2 = new File("eye2.jpg"); 
      image = new FileInputStream(file); 
      image2 = new FileInputStream(file2); 
      img = ImageIO.read(image); 
      img2 = ImageIO.read(image2); 

      int rowcol; 
      int width = img.getWidth(); 
      int height = img.getHeight(); 
      hexRGB.compareHexaRGB(width, height); 
      System.out.println("Image's Width: " + width); 
      System.out.println("Image's Height: " + height); 
      // hexRGB.check(); 
      int[][] pixelData = new int[width * height][3]; 

      System.out.println("Pixel Data: " + pixelData); 

      int[] rgb; 
      int count = 0; 

      img_hex2 = new String[width][height]; 
      img_hex4 = new String[width][height]; 
      for (int i = 0; i < width; i++) { 
       for (int j = 0; j < height; j++) { 
        rgb = newPD.getPixelData(img, i, j); 

        for (int k = 0; k < rgb.length; k++) { 
         pixelData[count][k] = rgb[k]; 
         // img_hex2[i][j] = newPD.getHexa2(); 
         // img_hex4[i][j] = newPD.getHexa4(); 
        } 

        img_hex2[i][j] = newPD.getHexa2(); // the code runs and 
                 // stops here 
        img_hex4[i][j] = newPD.getHexa4(); 
        System.out.println("Output: " + img_hex2[i][j]); 
        System.out.println("Output: " + img_hex4[i][j]); 
        count++; 
        System.out.println("\nRGB Counts: " + count); 
       } 
      } 

      int width2 = img2.getWidth(); 
      int height2 = img2.getHeight(); 
      System.out.println("Image's Width: " + width2); 
      System.out.println("Image's Height: " + height2); 

      int[][] pixelData2 = new int[width2 * height2][3]; 

      System.out.println("Pixel Data: " + pixelData2); 

      int[] rgb2; 
      int counter = 0; 

      img2_hex2 = new String[width2][height2]; 
      img2_hex4 = new String[width2][height2]; 

      for (int i = 0; i < width2; i++) { 
       for (int j = 0; j < height2; j++) { 
        rgb2 = newPD.getPixelData(img2, i, j); 

        for (int k = 0; k < rgb2.length; k++) { 
         pixelData2[counter][k] = rgb2[k]; 

        } 
        img2_hex2[i][j] = newPD.getHexa2(); 
        img2_hex4[i][j] = newPD.getHexa4(); 
        counter++; 
        System.out.println("\nRGB2 Counts: " + counter); 
       } 
      } 

     } catch (FileNotFoundException ex) { 
      Logger.getLogger(getPixelRGB1.class.getName()).log(Level.SEVERE, 
        null, ex); 
     } finally { 
      try { 
       image.close(); 
      } catch (IOException ex) { 
       Logger.getLogger(getPixelRGB1.class.getName()).log(
         Level.SEVERE, null, ex); 
      } 
     } 
     hexRGB.check(); 
    } 

    public String[][] display_imgHex2() { 
     return img_hex2; 
    } 

    public String[][] display_imgHex4() { 
     return img_hex4; 
    } 

    public String[][] display_img2Hex2() { 
     return img2_hex2; 
    } 

    public String[][] display_img2Hex4() { 
     return img2_hex4; 
    } 
} 
+0

現在它運行正常循環。但是輸出對於所有像素值顯示相同的輸出,表示位置2處的像素值不相同。 fyi,只有一些像素在兩個圖像上具有不同的值。我猜我的代碼可能在compareHexRGB方法的某處出錯。你能幫忙嗎? – user2890264

+0

在check()方法中,比較不正確。 (newPD.display_imgHex2()。equals(newPD.display_img2Hex2())&&(newPD.display_imgHex4()。equals(newPD.display_img2Hex4()))) 比較兩個對象display_imgHex2()和display_img2Hex2()。相反,他們的個人元素應該如下進行比較。 (newPD.display_imgHex2()[i] [j] .equals(newPD.display_img2Hex2()[i] [j])&&(newPD.display_imgHex4()[i] [j] .equals(newPD。 ifdisplay_img2Hex4()[i] [j]))) 對於elseif語句也應該進行相同的修改。 –

+0

謝謝!現在輸出是正確的。我可以繼續我的下一個過程,即提取過程。我仍然在開發代碼。希望事情變好。 – user2890264

0

一個可能的原因是頂部有四個未初始化的變量。

private static String[][] img_hex2; 
private static String[][] img_hex4; 
private static String[][] img2_hex2; 
private static String[][] img2_hex4; 

以上四個變量都是在不分配任何內存的情況下訪問的。

一個快速解決方法是在兩個使用它們的循環之前對它們進行初始化。

img_hex2 = new String[width][height]; 
    img_hex4 = new String[width][height]; 
    for(int i=0; i<width; i++) 
    { 
     for(int j=0; j<height; j++) 
     { 
     ..... 

    img2_hex2 = new String[width2][height2]; 
    img2_hex4 = new String[width2][height2];; 


    for(int i=0; i<width2; i++) 
    { 
     for(int j=0; j<height2; j++ 
+0

我改變了你的建議。該錯誤出現在線程「主」java.lang.ArrayIndexOutOfBoundsException中的異常:3 \t at getPixelRGB1.main(getPixelRGB1.java:79)。我現在應該怎麼做? – user2890264

+0

你介意分享更具體的信息嗎?例如,代碼行@ getPixelRGB1.java:79。 –

+0

我這樣做了: img_hex2 = newPD.getHexa2(); img_hex4 = newPD.getHexa4(); //錯誤聲明字符串不能轉換爲字符串[] [] 但如果我這樣做: img_hex2 [width] [height] = newPD.getHexa2(); img_hex4 [width] [height] = newPD.getHexa4(); //錯誤arrayindexoutofbounds出現 – user2890264