NumPy将8位转换为16/32位图像 [英] NumPy convert 8-bit to 16/32-bit image

查看:2036
本文介绍了NumPy将8位转换为16/32位图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenCV 2在YCbCr色彩空间中进行一些图像处理。目前我可以检测到由于转换RGB - > YCbCr然后YCbCr - > RGB引起的噪声,但如文档

I am using OpenCV 2 to do some images manipulations in YCbCr color space. For the moment I can detect some noise due to the conversion RGB -> YCbCr and then YCbCr -> RGB, but as said in the documentation:


如果你使用带有8位图像的cvtColor,转换将会有一些信息丢失了。对于许多应用程序,这将不会引人注意,但建议在需要全范围颜色的应用程序中使用32位图像,或者在操作之前转换图像然后转换回来。

If you use cvtColor with 8-bit images, the conversion will have some information lost. For many applications, this will not be noticeable but it is recommended to use 32-bit images in applications that need the full range of colors or that convert an image before an operation and then convert back.

所以我想用16或32位转换我的图像,但我没有找到如何用NumPy做到这一点。一些想法?

So I would like to convert my image in 16 or 32 bits, but I didn't found how to do it with NumPy. Some ideas?

img = cv2.imread(imgNameIn)
# Here I want to convert img in 32 bits
cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB, img)
# Some image processing ...
cv2.cvtColor(img, cv2.COLOR_YCR_CB2BGR, img)
cv2.imwrite(imgNameOut, img, [cv2.cv.CV_IMWRITE_PNG_COMPRESSION, 0])


推荐答案

感谢@moarningsun,解决了问题:

Thanks to @moarningsun, problem resolved:

i = cv2.imread(imgNameIn, cv2.CV_LOAD_IMAGE_COLOR) # Need to be sure to have a 8-bit input
img = np.array(i, dtype=np.uint16) # This line only change the type, not values
img *= 256 # Now we get the good values in 16 bit format

这篇关于NumPy将8位转换为16/32位图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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