尝试从给定的PNG图像中提取像素值 [英] Trying to extract pixel values from a given PNG image

查看:605
本文介绍了尝试从给定的PNG图像中提取像素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试了解PNG格式。



考虑此PNG图片:





图像被拍摄来自以及这里,到目前为止,我已经注意到以下关于此图片:



IHDR块必须首先出现。它包含:

 宽度:4个字节
高度:4个字节
位深度:1个字节
颜色类型:1字节
压缩方法:1字节
过滤方法:1字节
隔行扫描方法:1字节

下面我开始按顺序阅读HEX数据:



1 - 前8字节:这是8字节签名

  89 50 4E 47 0D 0A 1A 0A 

相当于:%PNG,可以在HEX编辑器中看到



有效的PNG图像必须包含IHDR块,一个或多个IDAT块以及IEND块。



2-块:长度

  00 00 00 0D 

3-Chunk:Chunk Type

  49 48 44 52 

哪个是IHDR。



http://www.w3.org/TR/PNG-Chunks.html



4-块:图像的宽度(以十进制128为单位)

  00 00 00 80 

5-块:图像的高度(以十进制68为单位)

  00 00 00 44 

6-块:BIT DEPTH(1字节)

  08 

7-块:颜色类型

  02 

8-压缩方法

  00 

9-过滤方式:

  00 

10-隔行扫描方法:

  00 

11-什么是以下数据?



C6 25 AA 3E 00 00 00 C2



12-- IDAT



49 44 41 54



13-这些数据是什么(在IDAT之后):



78 5E ED D4 81 06 C3 30 14 40 D1 B7 34 DD FF FF 6F B3 74 56 EA 89 12 6C 28 73 E2 AA 34 49 03 87 D6 FE D8 7B 89 BB 52 8D 3B 87 FE 01 00 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 04 00 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 00 D4 5E 6A 64 4B 94 F5 98 7C D1 F4 92 5C 5C 3E CF 9C 3F 73 71 58 5F AF 8B 79 5B EE 96 B6 47 EB F1 EA D1 CE B6 E3 75 3B E6 B9 95 8D C7 CE 03 39 C9 AF C6 33 93 7B 66 37 CF AB BF F9 C9 2F 08 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 04 00 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 8C 37 DB 68 03 20 FB ED 96 65 00 00 00 00



14- IEND:



49 45 4E 44



15-最后4个字节

  AE 42 60 82 

这些是什么?



有些人可以帮助我理解,上面第11,13和15点吗? Pixel值在哪里?图像具有(128 x 68像素)



了解这些详细信息的目的:



一旦我知道这些细节,我将生成自己的16位PNG图像。我已经有了像素值,所以我的工作就是引入标题等。

我不知道是否有软件可以执行这项工作。



更新



我现在明白因为压缩,我无法理解找到像素值。



我知道我可以在OpenCV中编写一个文件并将其另存为png。那么现在我的直接问题是:我有一个二进制文件,具有灰度16位像素值。我可以在OpenCV中将其写为16位PNG吗?

解决方案

虽然了解PNG图像实际是什么,以及图像在文件中的实际表现方式可能会很有趣,你不需要知道这个来生成一个PNG文件。



注意PNG使用无损压缩,这意味着你不会得到每个像素两个字节。



您可以在程序中生成图像,并使用许多库中的库以PNG格式输出图像。
例如,您可以在OpenCV中创建图像,然后使用 imWrite 。其中一个参数可以输出到PNG。






如果你有灰度16位像素值,然后你可以将它们放入 Mat



然后将其转换为IplImage:将cv :: Mat转换为IplImage *



然后你可以将它输出到一个文件。


Trying to understand PNG format.

Consider this PNG Image:

The Image is taken from here

In Hex Editor , it looks like this:

89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 00 00 00 80 00 00 00 44 08 02 00 00 00 
C6 25 AA 3E 00 00 00 C2 49 44 41 54 78 5E ED D4 81 06 C3 30 14 40 D1 B7 34 DD FF FF 6F 
B3 74 56 EA 89 12 6C 28 73 E2 AA 34 49 03 87 D6 FE D8 7B 89 BB 52 8D 3B 87 FE 01 00 80 
00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 04 00 80 00 00 10 00 00 02 00 40 
00 00 08 00 00 01 00 20 00 00 00 D4 5E 6A 64 4B 94 F5 98 7C D1 F4 92 5C 5C 3E CF 9C 3F 
73 71 58 5F AF 8B 79 5B EE 96 B6 47 EB F1 EA D1 CE B6 E3 75 3B E6 B9 95 8D C7 CE 03 39 
C9 AF C6 33 93 7B 66 37 CF AB BF F9 C9 2F 08 80 00 00 10 00 00 02 00 40 00 00 08 00 00 
01 00 20 00 00 04 00 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 8C 37 DB 
68 03 20 FB ED 96 65 00 00 00 00 49 45 4E 44 AE 42 60 82

Equivalent characters:

‰PNG........IHDR...€...D.....Æ%ª>...ÂIDATx^íÔ..Ã0.@Ñ·4Ýÿÿo³tVê‰.l(sâª4I.‡ÖþØ{‰
»R.;‡þ..€.......@....... ....€.......@....... ...Ô^jdK"õ˜|Ñô’\\>Ïœ?sqX_¯
‹y[î–¶GëñêÑζãu;湕.ÇÎ.9ɯÆ3"{f7Ï«¿ùÉ/.€.......@....... ....€.......@....... ..Œ7Ûh. 
ûí–e....IEND®B`‚

The same is shown in following Screenshot of the HEX Editor:

I am trying to reverse engineer this image to extract the header part and the RGB pixel values. I read about the PNG and also here , and so far I have noted the following about this Image:

The IHDR chunk must appear FIRST. It contains:

Width:              4 bytes
Height:             4 bytes
Bit depth:          1 byte
Color type:         1 byte
Compression method: 1 byte
Filter method:      1 byte
Interlace method:   1 byte

Below I am starting reading the HEX Data in sequence:

1- First 8-bytes: This is the 8-Byte signature

 89 50 4E 47 0D 0A 1A 0A

Equivalently this is : %PNG as can be seen in HEX Editor

A valid PNG image must contain an IHDR chunk, one or more IDAT chunks, and an IEND chunk.

2- Chunk: Length

 00 00 00 0D

3-Chunk: Chunk Type

 49 48 44 52

Which is IHDR.

http://www.w3.org/TR/PNG-Chunks.html

4- Chunk: Width of the Image (in Decimal 128)

00 00 00 80

5- Chunk: Height of the image (in Decimal 68)

00 00 00 44

6- Chunk: BIT DEPTH (1 byte )

08

7- Chunk: Color Type

02

8- Compression method

00

9- Filter method:

00

10- Interlace method:

00

11- What is the following data?

C6 25 AA 3E 00 00 00 C2

12-- IDAT

49 44 41 54

13- What is this data (after IDAT):

78 5E ED D4 81 06 C3 30 14 40 D1 B7 34 DD FF FF 6F B3 74 56 EA 89 12 6C 28 73 E2 AA 34 49 03 87 D6 FE D8 7B 89 BB 52 8D 3B 87 FE 01 00 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 04 00 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 00 D4 5E 6A 64 4B 94 F5 98 7C D1 F4 92 5C 5C 3E CF 9C 3F 73 71 58 5F AF 8B 79 5B EE 96 B6 47 EB F1 EA D1 CE B6 E3 75 3B E6 B9 95 8D C7 CE 03 39 C9 AF C6 33 93 7B 66 37 CF AB BF F9 C9 2F 08 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 04 00 80 00 00 10 00 00 02 00 40 00 00 08 00 00 01 00 20 00 00 8C 37 DB 68 03 20 FB ED 96 65 00 00 00 00

14- IEND:

49 45 4E 44

15- Last 4 bytes

 AE 42 60 82

What are these ?

Can some help me understand, points 11, 13 and 15 above? And where are the Pixel values? The Image is having (128 x 68 pixels)

Purpose of knowing these details:

Once I know these details, I will generate my own 16 bit PNG image. I already have pixel values, so my job would be to introduce headers etc.
I dont know if there is software that can perform this job.

UPDATE

I understand now because of compression, I would not be able to locate the pixel values.

I got the idea that I can write a file in OpenCV and save it as png. Well now my direct question is: I have a binary file having gray-scale 16 bit-pixel values. Can I write this in OpenCV as 16 bit PNG ?

解决方案

Although it might be interesting to learn about what PNG Images actually are, and how the image is actually represented in the file, you don't need to know this to generate a PNG file.

Note that PNG uses lossless compression, which means you won't get two bytes per pixel.

You can generate your image in a program and output it in PNG format using many of the libraries that there are out there. For example, you can make your image in OpenCV and then output it with imWrite. One of the parameters can make it output to a PNG.


If you have the gray-scale 16-bit pixel values, then you can put them into a Mat.

Then convert that to an IplImage: Converting cv::Mat to IplImage*

Then you can output it to a file.

这篇关于尝试从给定的PNG图像中提取像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆