如何使用 python 显示 16 位彩色 FITS 图像

发布于 2025-01-12 03:22:41 字数 1067 浏览 3 评论 0原文

我想在 python 中显示 FITS 图像。我使用 astropy(FITS 加载程序)、matplotlib 和灰度图像完成所有这些工作。然而,我有 16 位颜色的图像,带有 RGGB 拜耳矩阵,但我不知道如何将其显示为 RGB 彩色图像。

这适用于灰度图像:

import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
m42 = fits.open('FITS Data/frame-u-006073-4-0063.fits')
imagedata = m42[0].data
plt.imshow(imagedata, cmap='gray')
plt.colorbar()

在此处输入图像描述

但是我有第二个图像,每像素 16 位,我不知道如何将这些位映射到 r、g、b 值并在 matplotlib 中显示它们。

例如(第一个像素是 3148):

pixel = imagedata[0][0]
r = (pixel & 0b1111000000000000) >> 12
g = (pixel & 0b0000111100000000) >> 8
g = int((g + ((pixel & 0b0000000011110000) >> 4)) / 2)
b = pixel & 0b0000000000001111

分别为红色、绿色和蓝色给出 0、8 和 12。如何将整个数组imagedata映射到RGB并让matplotlib显示它?还假设您对两个绿色值进行平均?任何帮助表示赞赏。

更新:我是否误解了带有拜耳矩阵的 16 位图像的格式?每像素完整 16 位是 R、G、G 还是 B?在什么情况下我需要先对图像进行去马赛克/去拜耳处理?

I want to display a FITS image in python. I have all this working using astropy (the FITS loader) and matplotlib and a greyscale image. However I have images that are 16 bit colour with a Bayer matrix of RGGB and I don't know how to display this as an RGB colour image.

This works for a greyscale image:

import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
m42 = fits.open('FITS Data/frame-u-006073-4-0063.fits')
imagedata = m42[0].data
plt.imshow(imagedata, cmap='gray')
plt.colorbar()

enter image description here

However I have a second image that is 16 bits per pixel and I don't know how to map the bits to r, g, b values and display them in matplotlib.

For example (first pixel is 3148):

pixel = imagedata[0][0]
r = (pixel & 0b1111000000000000) >> 12
g = (pixel & 0b0000111100000000) >> 8
g = int((g + ((pixel & 0b0000000011110000) >> 4)) / 2)
b = pixel & 0b0000000000001111

Gives 0, 8, and 12 for red, green, and blue respectively. How do I map the entire array imagedata to RGB and get matplotlib to display it? Also assuming you average the two green values? Any help appreciated.

Update: Have I misunderstood the format of a 16 bit image with a Bayer matrix? Are the full 16 bits per pixel either R, G, G, or B? In which case do I need to look at demosaicing / debayering the image first?

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

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

发布评论

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

评论(1

祁梦 2025-01-19 03:22:41

答案是使用 OpenCV 对图像进行去拜耳处理,然后对数据进行标准化。

import cv2
debayered_image = cv2.cvtColor(imagedata, cv2.COLOR_BayerBG2BGR)
normalised_image = cv2.normalize(debayered_image, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
imgplot = plt.imshow(normalised_image)

The answer was to use OpenCV to debayer the image and then normalise the data.

import cv2
debayered_image = cv2.cvtColor(imagedata, cv2.COLOR_BayerBG2BGR)
normalised_image = cv2.normalize(debayered_image, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
imgplot = plt.imshow(normalised_image)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文