2010-09-03 96 views
2

我覺得我在這裏錯過了一些簡單的東西(像往常一樣)。如何讀取Java中的PGM圖像?

我正在嘗試使用Java讀取PGM圖像。 Matlab的不只是罰款 - 輸出圖像的像素(例如,一個小的32×32圖像)在Matlab中給了我這樣的事情:

1 0 11 49 94 118 118 106 95 88 85 96 124 143 142 133

我的Java的讀者,但是,輸出這樣的:

1 0 11 49 94 118 118 106 95 88 85 96 124 65533 65533 65533

似乎像127以上的像素值用65533填充,儘管它確實得到了一些不正確的隨機值,甚至幾乎將整個底行賦值爲-1。

下面是我使用的代碼:

filePath = 'imagepath.pgm'; 
FileInputStream fileInputStream = new FileInputStream(filePath); 
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); 

// read the header information ... 

int [][] data2D = new int [picWidth] [picHeight]; 

for (int row = 0; row < picHeight; row++) { 
    for (int col = 0; col < picWidth; col++) { 
    data2D[row][col] = bufferedReader.read(); 
    System.out.print(data2D[row][col] + " "); 
    } 
    System.out.println(); 
} 

fileInputStream.close();

任何想法,將不勝感激。

編輯這裏是無符號的PGM值:

  1  0 11 49 94 118 118 106 95 88 85 96 124 143 142 133 
    30 26 29 57 96 122 125 114 102 94 91 101 127 146 145 136 
    96 85 70 75 101 128 136 126 111 106 106 112 131 149 153 147 
    163 147 114 93 99 120 132 123 110 113 124 129 137 154 166 168 
    215 195 149 105 88 99 114 111 106 123 148 158 160 174 191 197 
    245 224 173 115 81 82 100 109 117 144 179 194 194 205 222 230 
    235 217 170 115 78 78 113 117 100 83 80 212 214 226 244 253 
    178 167 135 93 68 78 123 129 106 77 69 202 204 222 244 255 
    114 110 92 64 54 81 107 105 83 59 56 182 184 201 222 231 
    79 80 71 52 55 97 67 55 41 33 42 184 179 181 185 183 
    62 66 65 52 63 115 29 16 12 17 30 209 197 174 150 132 
    40 47 52 44 55 109 171 196 188 186 208 229 218 179 136 107 
    31 38 44 37 43 89 145 167 158 159 191 223 219 179 133 105 
    48 52 56 51 57 91 128 133 117 120 157 196 200 168 128 105 
    64 67 70 73 87 114 127 107 79 81 118 159 173 154 123 104 
    63 67 73 83 107 132 129 91 54 54 88 130 153 146 123 106

標題是這樣的:

P5 
# MatLab PGMWRITE file, saved 27-Jun-2002 
16 16 
255

編輯#2

下面是完整的輸出,以證明下面的概念代碼:

 
Skipping unknow token: "" 
Skipping unknow token: "1^vvj_XU`|���" 
Skipping unknow token: "" 
Skipping unknow token: "9`z}rf^[e���`UFKe��~ojjp������r]cx�{nq|������ÕiXcroj{��������sQRdmu��������٪sNNqudSP�����]DN{�jME�����rn\@6QkiS;8�����OPG47aC7)!*�����>BA4?s" 
Skipping unknow token: "" 
Skipping unknow token: "" 
Skipping unknow token: "�Ů��(/4,7m�ļ���ڳ�k" 
Skipping unknow token: "&,%+Y������۳�i04839[��ux��Ȩ�[email protected]���{h?CISk��[66X���{j" 
Exception in thread "main" java.util.NoSuchElementException 
    at java.util.Scanner.throwFor(Scanner.java:838) 
    at java.util.Scanner.next(Scanner.java:1347) 
    at Test.main(Test.java:49) 

行中拋出的異常簡稱爲:

System.out.println(String.format("Skipping unknow token: \"%s\"", scan.next())); 

的問題,我敢肯定,有事情做的事實,這些圖像文件包括兩個ASCII文本/數字,以及作爲二進制圖像數據。但是,如果Java閱讀PNG沒有問題,爲什麼缺乏對PGM的支持?

編輯3

好吧,我找到問題的實施......不幸的是,它棄用:

filePath = "imagepath.pgm" 
    FileInputStream fileInputStream = new FileInputStream(filePath); 
    DataInputStream dis = new DataInputStream(fileInputStream); 
    StreamTokenizer streamTokenizer = new StreamTokenizer(dis); 

    // read header text using StreamTokenizer.nextToken() 

    data2D = new int [picWidth] [picHeight]; 
    for (int row = 0; row < picHeight; row++) { 
    for (int col = 0; col < picWidth; col++) { 
     data2D[row][col] = dis.readUnsignedByte(); 
     System.out.print(data2D[row][col] + " "); 
    } 
    System.out.println(); 
    } 

根據Java文檔中,StreamTokenizer(InputStream)構造已過時,因爲DataInputStream.readLine()方法不能正確地將原始字節轉換爲字符。但是,它似乎在標題上的這個特定情況下起作用,並且顯然適用於隨後的二進制圖像數據。

不幸的是,它仍然是過時了,它似乎是通過混合一BufferedReader作爲文檔讀取頭和嘗試使用DataInputStream讀取原始字節後,表明在EOFException唯一的結果。仍然在尋找解決方案...

+0

完成並完成,讓我知道你可能還需要什麼。 – Magsol 2010-09-06 21:53:58

回答

6

您的代碼存在的問題是您使用錯誤的類從文件中讀取原始數據。由於BufferedReader文件說:

public int read() throws IOException

讀取單個字符。

返回:字符範圍讀,爲整數0到65535(0x00-0xffff),或-1,如果流的末尾,已達到

所以每次調用read()方法的BufferedReader實際上會從輸入流中消耗一個或兩個字節(基於字符編碼),這不是您想要的。這也解釋了爲什麼你得到了很多-1:流比你想象的要早得多。

由於PGM包含ASCII碼十進制值,因此使用Scanner類很容易解析。

下面是一個幾乎未經測試的代碼演示瞭如何讀取PGM圖像假設:

  • 它包含一個神奇的數字後一個註釋(即它沒有以#除了開頭的行第二個)
  • PGM文件正好是4行。

下面的代碼:

String filePath = "image.pgm"; 
fileInputStream = new FileInputStream(filePath); 
Scanner scan = new Scanner(fileInputStream); 
// Discard the magic number 
scan.nextLine(); 
// Discard the comment line 
scan.nextLine(); 
// Read pic width, height and max value 
int picWidth = scan.nextInt(); 
int picHeight = scan.nextInt(); 
int maxvalue = scan.nextInt(); 

fileInputStream.close(); 

// Now parse the file as binary data 
fileInputStream = new FileInputStream(filePath); 
DataInputStream dis = new DataInputStream(fileInputStream); 

// look for 4 lines (i.e.: the header) and discard them 
int numnewlines = 4; 
while (numnewlines > 0) { 
    char c; 
    do { 
     c = (char)(dis.readUnsignedByte()); 
    } while (c != '\n'); 
    numnewlines--; 
} 

// read the image data 
int[][] data2D = new int[picHeight][picWidth]; 
for (int row = 0; row < picHeight; row++) { 
    for (int col = 0; col < picWidth; col++) { 
     data2D[row][col] = dis.readUnsignedByte(); 
     System.out.print(data2D[row][col] + " "); 
    } 
    System.out.println(); 
} 

需要實現:註釋行的支持,對於每一個元素的值應該由maxvalue進行劃分,對錯誤的文件,異常處理的錯誤檢查。我使用UNIX行尾測試了PGM文件,但它也應該在Windows上工作。

請讓我強調一下,這不是PGM解析器的強大而不完整的實現。此代碼僅用於概念驗證,可能會滿足您的需求。

如果你確實需要一個健壯的PGM解析器,你可以使用Netpbm提供的工具。

+0

這很好,但它會產生一個新問題:解析出頭文件。我使用BufferedReader/StreamTokenizer來讀取標題字符,出於某種原因,一旦完成,第一次調用dis.readByte()就會拋出EOFException異常。如果我從文件中刪除標題並直接從二進制文件中讀取,我遇到了一個不同的問題:它讀取的前55個字節是垃圾數字;第56個字節是在我的原始文章中首先顯示的「1」,後面是所有相應的數字(由於垃圾導入,最多55個字節)。有什麼想法嗎? – Magsol 2010-09-06 21:56:33

+0

呃抱歉,無視55字節的位;如果我消除標題(因此,BufferedReader/StreamTokenizer並具有單個文件句柄 - DataInputStream - 從文件讀取),它工作得很好。 – Magsol 2010-09-06 22:05:54

+0

我的不好,我沒有閱讀PGM文件格式規範。我會盡量在幾分鐘內給它一個鏡頭。 – 2010-09-06 22:33:17