- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我觉得我在这里遗漏了一些简单的东西(像往常一样)。
我正在尝试使用 Java 读取 PGM 图像。 Matlab 做得很好——在 Matlab 中输出图像像素(例如,一个小的 32x32 图像)给我这样的结果:
1 0 11 49 94 118 118 106 95 88 85 96 124 143 142 133
My Java reader, however, outputs this:
1 0 11 49 94 118 118 106 95 88 85 96 124 65533 65533 65533
It seems like pixel values above 127 are filled in with 65533, though it does get some random values incorrect, and even assigns almost the entire bottom row to the value of -1.
Here's the code I'm using:
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();
Any ideas would be greatly appreciated.
Edit Here are the unsigned PGM values:
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
The header looks like this:
P5# MatLab PGMWRITE file, saved 27-Jun-200216 16255
Edit #2
Here's the full output to the proof of concept code below:
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��Ȩ�i@CFIWrkOQv���{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)
Line 49 referred to in the thrown exception is:
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()
方法无法将原始字节正确转换为字符。但是,它似乎在 header 上的这种特定情况下有效,并且显然适用于随后的二进制图像数据。
不幸的是,它仍然被弃用,并且似乎通过混合 BufferedReader
作为文档建议只会导致 EOFException
在读取 header 并尝试使用 DataInputStream
读取原始字节。仍在寻找解决方案...
最佳答案
您的代码存在的问题是您使用了错误的类从文件中读取原始数据。正如 BufferedReader
文档所说:
public int read() throws IOException
Reads a single character.
Returns: The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached
所以每次调用 BufferedReader
的 read()
方法实际上会从输入流中消耗一两个字节(基于字符编码),这不是你想要的想。这也解释了为什么你会得到很多 -1:流结束的时间比你想象的要早得多。
由于 PGM 包含 ASCII 十进制值,因此很容易使用 Scanner 进行解析类。
这是一段几乎未经测试的代码,它展示了如何读取 PGM 图像并假定:
代码如下:
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 提供的工具。 .
关于java - 如何在 Java 中读取 PGM 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3639198/
#include void lower_string(char*); int main() { char string[100]; printf("Enter
如何使用 Java 将二进制 PGM 文件转换为 ASCII PGM 文件? 当我使用以下代码时,我无法在 B.pgm 中写入 ASCII 值。我也尝试过使用dos.writeInt。 FileInp
我正在尝试对 pgm 执行卷积类型图像 P5 (二进制)使用以下设置: 输入输出数组 vector> image(rows, vector(cols, '\0')); vector> out(rows
我的机器上安装了 zmq 版本 4.1.3 和 pyzmq 版本 15.2.0(我假设是通过 pip 但我现在不记得了)。我需要连接到 UDP epgm 套接字,但收到错误“协议(protocol)不
我目前正在做一个学生项目,它对灰度 pgm 文件进行一些图像处理(膨胀和腐 eclipse )。我试图对图像对象实现扩张,但得到了奇怪的结果。我预计图像对象会比原始图像大。但是根据我使用的内核大小,我
以下代码适用于管理员帐户,但对于非管理员帐户,它会打印两次成功然后抛出 System.Net.Sockets.SocketException (0x80004005):尝试以其禁止的方式访问套接字访问
我需要在库 netpbm/pgm 中创建拉普拉斯运算符。处理图像很简单 - 我计算每个像素。我不使用标准化,因为我知道它是 0 并且我想简化解决方案。 我的代码: #include #include
我需要为类项目创建 PGM 图像的单个灰度直方图。我已经有了读取 PGM 图像的代码。但是我不完全理解如何计算唯一的像素值。我不需要输出图表或类似的东西。任何帮助将不胜感激! 读取PGM图像的代码 h
int i, j; for (i = (*pgm).height - 1; i >= 0; i--) for (j = 0; j < (*pgm).width; j++)
您好,我有一个关于读取 pgm 值的底部值的问题。我制作了一个 2d 动态数组,并为高度和宽度添加了 2 个额外的空格,以便能够创建缓冲区,但是当我尝试读取时将第一个整数设置为 width+1 它不起
我正在尝试自学数字图像处理。我已将我的 pgm 读入结构中。在这个结构中我有像素数组。我可以打印得很好。现在我想根据用户输入裁剪这张照片。我接受这些输入并根据输入的坐标得到一个黑色矩形。我做错了什么?
我正在尝试使用这段代码编写一个 pgm 文件.. myfile (image), (sizeRow*sizeColumn)*sizeof(unsigned char)); 如果我尝试将其写入 .txt
我有一个非常奇怪的错误, 所以我尝试通过将其像素值加载到数组中来读取 pgm 图像,我能够正确读取其版本、高度、宽度和最大可能像素值。然而,当我开始读取像素值时,我总是得到 0。(我知道它不是零,因为
我的目标是读入 PGM 图像并生成颜色值反转的图像。但是当我输入 this image ,我回来了this image .我在 C 中编程,在 Windows 7(64 位)上使用 Eclipse 和
我正在尝试从 .pgm 图像文件 (mars.pgm) 的 header 读取大小值,并使用 sscanf 将结果值分配给整数变量 u 和 v。 程序执行时在第一行打印 P5 832 700 127,
我正在尝试读取和重写 PGM 图像,但它会导致形状困惑。右图为原图,左图为重制图: 这是我正在使用的代码: #include #include #include #include using
我有一百个 128 x 128 的 .pgm 文件,上面有一些形状,我认为它们的色阶是 255(虽然不确定这个,所以如果解决方案也可以接受它会很好考虑),我需要提取这些颜色来处理图像。所以我想最终得到
我有一个包含 0-254 灰度值的 int 数组,我还有图像的 x 和 y 大小。创建 pgm 图像是一件容易的事情,但我想在 jsp 中显示它,所以我需要以某种方式将其转换为 jpeg 或 png
我已经被这个问题困扰了一段时间了。我如何将 pgm 文件中的像素值 0-255 复制到数组中?这是我到目前为止所拥有的。我知道我偏离了应有的样子。 #include #include int ma
我正在尝试将 JavaScript 中的 Canvas 转换为 pgm。在我转换文件并将其下载到我的 table 面后,它具有 pgm 扩展名,但是当我用编辑器打开它时,它被编码为 png。我认为
我是一名优秀的程序员,十分优秀!