如何在Python openCV中显示16位4096强度图像? [英] How to display 16-bit 4096 intensity image in Python openCV?

查看:693
本文介绍了如何在Python openCV中显示16位4096强度图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以灰度16位tiff格式编码的图像.他们使用16位色深的变体,最大强度为4,096.

I have images encoded in grayscale 16-bit tiff format. They use a variant of 16-bit color depth where the max intensity is 4,096.

我相信openCV的默认最大强度为65,536,因此我的图像使用以下代码显示为黑色.

I believe the default max intensity in openCV is 65,536, so my image shows up as black using the following code.

import cv2

image = cv2.imread("test.tif", -1)

cv2.imshow('tiff', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(image)

我可以使用matplotlib中的vminvmax来配置颜色映射:

I can use vmin and vmax in matplotlib to configure the color mapping:

import cv2
import matplotlib.pyplot as plt 

image = cv2.imread("test.tif", -1)
plt.imshow(image, cmap="gray", vmin=0, vmax=4096)
plt.show()

它显示图像的内容:

我要坚持使用openCV的原因是matplotlib,不支持显示16位RGB图像.

The reason why I want to stick with openCV is matplotlib doesn't support displaying 16-bit RGB images.

cv2.imshow文档是并没有真正的帮助.有没有办法在Python openCV中显示16位4096强度图像?

The documentation of cv2.imshow is not really helpful. Are there ways to display 16-bit 4096 intensity images in Python openCV?

可以在此处找到测试图像test.tif .

The testing image test.tif can be found here.

推荐答案

您将要使用

You'll want to use cv2.normalize() to scale the image before displaying.

您可以设置图像的最小/最大,它将适当缩放图像(通过将图像的最小移动到alpha,将图像的最大移动到beta).假设您的img已经是uint16:

You can set the min/max of the image and it will scale the image appropriately (by moving the min of the image to alpha and max of the image to beta). Supposing your img is already a uint16:

img_scaled = cv2.normalize(img, dst=None, alpha=0, beta=65535, norm_type=cv2.NORM_MINMAX)

然后您就可以正常查看了.

And then you can view as normal.

默认情况下,cv2.normalize()将产生与输入图像相同类型的图像,因此,如果要使用无符号16位结果,则输入应为uint16.

By default, cv2.normalize() will result in an image the same type as your input image, so if you want an unsigned 16-bit result, your input should be uint16.

同样,请注意,这会线性扩展图像范围---如果图像从未真正达到0并说最小值为100,则在进行归一化后,该最小值将是您将alpha设置为的最小值.如果您不希望这样做,就像其中一条评论所建议的那样,您可以将图像简单地乘以16,因为它目前最多只能增加4095.使用* 16,则可以最多增加65535.

Again, note that this linearly stretches your image range---if your image never actually hit 0 and say the lowest value was 100, after you normalize, that lowest value will be whatever you set alpha to. If you don't want that, as one of the comments suggests, you can simply multiply your image by 16, since it's currently only going up to 4095. With * 16, it will go up to 65535.

这篇关于如何在Python openCV中显示16位4096强度图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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