获取浮点型像素颜色值(HDR)

发布于 2025-01-10 16:53:24 字数 389 浏览 5 评论 0原文

在Python中是否可以读取图像并获取0-1(或浮点数中的0-255)之间的每个像素颜色(12位深度)值,而不需要转换为0-255 - > 0-1 范围。 这样我就不会损失精度并获得 HDR 格式的准确颜色。

看起来 cv2 和 PIL 仅读取整数值的像素。

这是 Paul Rohde 的一个很好的仓库,我想在图像示例中使用:https://github.com/codelogic/wide-gamut-tests

Is it possible in Python to read an image and get each pixels color (12 bits depth) value between 0-1 (or 0-255 in float) without doing the conversion to 0-255 -> 0-1 range.
This way I won't get precision loss and get the exact color in HDR format.

It look like cv2 and PIL read pixels only in integer value.

Here is a good repo of Paul Rohde that I would like to use in example for image : https://github.com/codelogic/wide-gamut-tests

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

≈。彩虹 2025-01-17 16:53:24

更新的答案

您可以打开一个正确的 HDR 文件,如下所示,以获得完整的 float32 缩放:

import cv2
import numpy as np

im = cv2.imread('HDR_029_Sky_Cloudy_Ref.hdr',cv2.IMREAD_UNCHANGED)

In [24]: im.dtype
Out[24]: dtype('float32')

In [25]: im.shape
Out[25]: (1536, 3072, 3)

原始答案

我仍然不明白你的问题。您共享的图像是每个样本 8 位,或 RGB888。它既不是 12 位也不是 HDR。

你可以用exiftool看到,从底部开始6行:

ExifTool Version Number         : 12.30
File Name                       : R2020-sRGB-red.jpg
Directory                       : .
File Size                       : 270 KiB
File Modification Date/Time     : 2022:03:01 14:07:19+00:00
File Access Date/Time           : 2022:03:01 14:21:38+00:00
File Inode Change Date/Time     : 2022:03:01 14:21:36+00:00
File Permissions                : -rw-r--r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
Exif Byte Order                 : Big-endian (Motorola, MM)
Orientation                     : Horizontal (normal)
X Resolution                    : 300
Y Resolution                    : 300
Resolution Unit                 : inches
Software                        : Adobe Photoshop CC 2017 (Windows)
Modify Date                     : 2018:05:16 00:08:12
Color Space                     : Uncalibrated
Exif Image Width                : 1000
Exif Image Height               : 1000
Compression                     : JPEG (old-style)
Thumbnail Offset                : 318
Thumbnail Length                : 2619
Current IPTC Digest             : d3c99f4b3d8dea288548356a84159c2b
Coded Character Set             : UTF8
Application Record Version      : 0
IPTC Digest                     : d3c99f4b3d8dea288548356a84159c2b
Displayed Units X               : inches
Displayed Units Y               : inches
Print Style                     : Centered
Print Position                  : 0 0
Print Scale                     : 1
Global Angle                    : 90
Global Altitude                 : 30
URL List                        : 
Slices Group Name               : R2020-sRGB-colors
Num Slices                      : 1
Pixel Aspect Ratio              : 1
Photoshop Thumbnail             : (Binary data 2619 bytes, use -b option to extract)
Has Real Merged Data            : Yes
Writer Name                     : Adobe Photoshop
Reader Name                     : Adobe Photoshop CC 2017
Photoshop Quality               : 12
Photoshop Format                : Standard
Progressive Scans               : 3 Scans
XMP Toolkit                     : Adobe XMP Core 5.6-c138 79.159824, 2016/09/14-01:09:01
Creator Tool                    : Adobe Photoshop CC 2017 (Windows)
Create Date                     : 2018:04:14 13:42:30-07:00
Metadata Date                   : 2018:05:16 00:08:12-07:00
Format                          : image/jpeg
Instance ID                     : xmp.iid:c33f28fc-ee14-6f4c-a98f-03836b827b8d
Document ID                     : adobe:docid:photoshop:df3a2df2-58d7-11e8-83e7-fac92a657c91
Original Document ID            : xmp.did:89fc91da-7e3b-5e42-a65d-2086afe61c0c
Color Mode                      : RGB
ICC Profile Name                : R2020-D65-2_2-Gamma
History Action                  : created, saved, saved, converted, derived, saved
History Instance ID             : xmp.iid:89fc91da-7e3b-5e42-a65d-2086afe61c0c, xmp.iid:bf59df79-a0d5-844d-9a1a-bfe11a6f5dea, xmp.iid:8b239d1a-036e-d746-a911-301848e62193, xmp.iid:c33f28fc-ee14-6f4c-a98f-03836b827b8d
History When                    : 2018:04:14 13:42:30-07:00, 2018:04:14 15:35:31-07:00, 2018:05:16 00:08:12-07:00, 2018:05:16 00:08:12-07:00
History Software Agent          : Adobe Photoshop CC 2017 (Windows), Adobe Photoshop CC 2017 (Windows), Adobe Photoshop CC 2017 (Windows), Adobe Photoshop CC 2017 (Windows)
History Changed                 : /, /, /
History Parameters              : from application/vnd.adobe.photoshop to image/jpeg, converted from application/vnd.adobe.photoshop to image/jpeg
Derived From Instance ID        : xmp.iid:8b239d1a-036e-d746-a911-301848e62193
Derived From Document ID        : adobe:docid:photoshop:b57016aa-40f6-11e8-b223-d728bea1d13d
Derived From Original Document ID: xmp.did:89fc91da-7e3b-5e42-a65d-2086afe61c0c
Text Layer Name                 : Hello R2020., Description
Text Layer Text                 : Hello R2020., .jpeg
Document Ancestors              : adobe:docid:photoshop:142342da-4034-11e8-b223-d728bea1d13d, adobe:docid:photoshop:27a3c052-40f2-11e8-b223-d728bea1d13d, adobe:docid:photoshop:37aa27c0-4028-11e8-b223-d728bea1d13d, adobe:docid:photoshop:3ff7034b-40f2-11e8-b223-d728bea1d13d, adobe:docid:photoshop:426a1762-40f3-11e8-b223-d728bea1d13d, adobe:docid:photoshop:87dac81c-4037-11e8-b223-d728bea1d13d, adobe:docid:photoshop:a9fcfe50-40f3-11e8-b223-d728bea1d13d, adobe:docid:photoshop:adb60761-40f6-11e8-b223-d728bea1d13d, xmp.did:071fe721-2b13-634f-970a-fcb42809f1b4, xmp.did:3ae63550-a464-dd44-bb96-bf97bb0a97e1
Profile CMM Type                : Adobe Systems Inc.
Profile Version                 : 2.1.0
Profile Class                   : Display Device Profile
Color Space Data                : RGB
Profile Connection Space        : XYZ
Profile Date Time               : 2018:04:14 23:02:20
Profile File Signature          : acsp
Primary Platform                : Apple Computer Inc.
CMM Flags                       : Not Embedded, Independent
Device Manufacturer             : none
Device Model                    : 
Device Attributes               : Reflective, Glossy, Positive, Color
Rendering Intent                : ICC-Absolute Colorimetric
Connection Space Illuminant     : 0.9642 1 0.82489
Profile Creator                 : Adobe Systems Inc.
Profile ID                      : 0
Profile Copyright               : Copyright 2018 Adobe Systems Incorporated
Profile Description             : R2020-D65-2_2-Gamma
Media White Point               : 0.95047 1 1.08905
Red Matrix Column               : 0.67348 0.27904 -0.00194
Green Matrix Column             : 0.16568 0.67535 0.02998
Blue Matrix Column              : 0.12505 0.04561 0.79684
Red Tone Reproduction Curve     : (Binary data 14 bytes, use -b option to extract)
Green Tone Reproduction Curve   : (Binary data 14 bytes, use -b option to extract)
Blue Tone Reproduction Curve    : (Binary data 14 bytes, use -b option to extract)
DCT Encode Version              : 100
APP14 Flags 0                   : [14]
APP14 Flags 1                   : (none)
Color Transform                 : YCbCr
Image Width                     : 1000
Image Height                    : 1000
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8                             <<< HERE >>>
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:4:4 (1 1)
Image Size                      : 1000x1000
Megapixels                      : 1.0
Thumbnail Image                 : (Binary data 2619 bytes, use -b option to extract)

如果你用OpenCV加载图像,你可以看到你说看不到的文本:

import cv2
import numpy as np

# Load image as BGR888
im = cv2.imread('R2020-sRGB-red.jpg')

# Make the pixels you want to see white, all else black
hello = np.all(im==[0,0,254], axis=2) * 255

在此处输入图像描述

Updated Answer

You can open a proper HDR file like this to get it in full float32 scaling:

import cv2
import numpy as np

im = cv2.imread('HDR_029_Sky_Cloudy_Ref.hdr',cv2.IMREAD_UNCHANGED)

In [24]: im.dtype
Out[24]: dtype('float32')

In [25]: im.shape
Out[25]: (1536, 3072, 3)

Original Answer

I still don't understand your question. The image you have shared is 8-bits per sample, or RGB888. It is neither 12-bit nor HDR.

You can see that with exiftool, 6 lines from the bottom:

ExifTool Version Number         : 12.30
File Name                       : R2020-sRGB-red.jpg
Directory                       : .
File Size                       : 270 KiB
File Modification Date/Time     : 2022:03:01 14:07:19+00:00
File Access Date/Time           : 2022:03:01 14:21:38+00:00
File Inode Change Date/Time     : 2022:03:01 14:21:36+00:00
File Permissions                : -rw-r--r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
Exif Byte Order                 : Big-endian (Motorola, MM)
Orientation                     : Horizontal (normal)
X Resolution                    : 300
Y Resolution                    : 300
Resolution Unit                 : inches
Software                        : Adobe Photoshop CC 2017 (Windows)
Modify Date                     : 2018:05:16 00:08:12
Color Space                     : Uncalibrated
Exif Image Width                : 1000
Exif Image Height               : 1000
Compression                     : JPEG (old-style)
Thumbnail Offset                : 318
Thumbnail Length                : 2619
Current IPTC Digest             : d3c99f4b3d8dea288548356a84159c2b
Coded Character Set             : UTF8
Application Record Version      : 0
IPTC Digest                     : d3c99f4b3d8dea288548356a84159c2b
Displayed Units X               : inches
Displayed Units Y               : inches
Print Style                     : Centered
Print Position                  : 0 0
Print Scale                     : 1
Global Angle                    : 90
Global Altitude                 : 30
URL List                        : 
Slices Group Name               : R2020-sRGB-colors
Num Slices                      : 1
Pixel Aspect Ratio              : 1
Photoshop Thumbnail             : (Binary data 2619 bytes, use -b option to extract)
Has Real Merged Data            : Yes
Writer Name                     : Adobe Photoshop
Reader Name                     : Adobe Photoshop CC 2017
Photoshop Quality               : 12
Photoshop Format                : Standard
Progressive Scans               : 3 Scans
XMP Toolkit                     : Adobe XMP Core 5.6-c138 79.159824, 2016/09/14-01:09:01
Creator Tool                    : Adobe Photoshop CC 2017 (Windows)
Create Date                     : 2018:04:14 13:42:30-07:00
Metadata Date                   : 2018:05:16 00:08:12-07:00
Format                          : image/jpeg
Instance ID                     : xmp.iid:c33f28fc-ee14-6f4c-a98f-03836b827b8d
Document ID                     : adobe:docid:photoshop:df3a2df2-58d7-11e8-83e7-fac92a657c91
Original Document ID            : xmp.did:89fc91da-7e3b-5e42-a65d-2086afe61c0c
Color Mode                      : RGB
ICC Profile Name                : R2020-D65-2_2-Gamma
History Action                  : created, saved, saved, converted, derived, saved
History Instance ID             : xmp.iid:89fc91da-7e3b-5e42-a65d-2086afe61c0c, xmp.iid:bf59df79-a0d5-844d-9a1a-bfe11a6f5dea, xmp.iid:8b239d1a-036e-d746-a911-301848e62193, xmp.iid:c33f28fc-ee14-6f4c-a98f-03836b827b8d
History When                    : 2018:04:14 13:42:30-07:00, 2018:04:14 15:35:31-07:00, 2018:05:16 00:08:12-07:00, 2018:05:16 00:08:12-07:00
History Software Agent          : Adobe Photoshop CC 2017 (Windows), Adobe Photoshop CC 2017 (Windows), Adobe Photoshop CC 2017 (Windows), Adobe Photoshop CC 2017 (Windows)
History Changed                 : /, /, /
History Parameters              : from application/vnd.adobe.photoshop to image/jpeg, converted from application/vnd.adobe.photoshop to image/jpeg
Derived From Instance ID        : xmp.iid:8b239d1a-036e-d746-a911-301848e62193
Derived From Document ID        : adobe:docid:photoshop:b57016aa-40f6-11e8-b223-d728bea1d13d
Derived From Original Document ID: xmp.did:89fc91da-7e3b-5e42-a65d-2086afe61c0c
Text Layer Name                 : Hello R2020., Description
Text Layer Text                 : Hello R2020., .jpeg
Document Ancestors              : adobe:docid:photoshop:142342da-4034-11e8-b223-d728bea1d13d, adobe:docid:photoshop:27a3c052-40f2-11e8-b223-d728bea1d13d, adobe:docid:photoshop:37aa27c0-4028-11e8-b223-d728bea1d13d, adobe:docid:photoshop:3ff7034b-40f2-11e8-b223-d728bea1d13d, adobe:docid:photoshop:426a1762-40f3-11e8-b223-d728bea1d13d, adobe:docid:photoshop:87dac81c-4037-11e8-b223-d728bea1d13d, adobe:docid:photoshop:a9fcfe50-40f3-11e8-b223-d728bea1d13d, adobe:docid:photoshop:adb60761-40f6-11e8-b223-d728bea1d13d, xmp.did:071fe721-2b13-634f-970a-fcb42809f1b4, xmp.did:3ae63550-a464-dd44-bb96-bf97bb0a97e1
Profile CMM Type                : Adobe Systems Inc.
Profile Version                 : 2.1.0
Profile Class                   : Display Device Profile
Color Space Data                : RGB
Profile Connection Space        : XYZ
Profile Date Time               : 2018:04:14 23:02:20
Profile File Signature          : acsp
Primary Platform                : Apple Computer Inc.
CMM Flags                       : Not Embedded, Independent
Device Manufacturer             : none
Device Model                    : 
Device Attributes               : Reflective, Glossy, Positive, Color
Rendering Intent                : ICC-Absolute Colorimetric
Connection Space Illuminant     : 0.9642 1 0.82489
Profile Creator                 : Adobe Systems Inc.
Profile ID                      : 0
Profile Copyright               : Copyright 2018 Adobe Systems Incorporated
Profile Description             : R2020-D65-2_2-Gamma
Media White Point               : 0.95047 1 1.08905
Red Matrix Column               : 0.67348 0.27904 -0.00194
Green Matrix Column             : 0.16568 0.67535 0.02998
Blue Matrix Column              : 0.12505 0.04561 0.79684
Red Tone Reproduction Curve     : (Binary data 14 bytes, use -b option to extract)
Green Tone Reproduction Curve   : (Binary data 14 bytes, use -b option to extract)
Blue Tone Reproduction Curve    : (Binary data 14 bytes, use -b option to extract)
DCT Encode Version              : 100
APP14 Flags 0                   : [14]
APP14 Flags 1                   : (none)
Color Transform                 : YCbCr
Image Width                     : 1000
Image Height                    : 1000
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8                             <<< HERE >>>
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:4:4 (1 1)
Image Size                      : 1000x1000
Megapixels                      : 1.0
Thumbnail Image                 : (Binary data 2619 bytes, use -b option to extract)

If you load the image with OpenCV, you can see the text that you say you cannot see:

import cv2
import numpy as np

# Load image as BGR888
im = cv2.imread('R2020-sRGB-red.jpg')

# Make the pixels you want to see white, all else black
hello = np.all(im==[0,0,254], axis=2) * 255

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文