简体   繁体   English

将图像从 CV_64F 转换为 CV_8U

[英]convert image from CV_64F to CV_8U

I want to convert an image of type CV_64FC1 to CV_8UC1 in Python using OpenCV.我想使用 OpenCV 在 Python CV_64FC1 CV_8UC1类型的图像转换为CV_8UC1

In C++, using convertTo function, we can easily convert image type using following code snippet:在 C++ 中,使用convertTo函数,我们可以使用以下代码片段轻松转换图像类型:

image.convertTo(image, CV_8UC1);

I have searched on Internet but unable to find any solution without errors.我在互联网上搜索过,但找不到任何没有错误的解决方案。 Any function in Python OpenCV to convert this? Python OpenCV 中的任何函数来转换它?

You can convert it to a Numpy array.您可以将其转换为 Numpy 数组。

import numpy as np

# Convert source image to unsigned 8 bit integer Numpy array
arr = np.uint8(image)

# Width and height
h, w = arr.shape

It seems OpenCV Python APIs accept Numpy arrays as well.似乎 OpenCV Python API 也接受 Numpy 数组。 I've not tested it though.不过我还没有测试过。 Please test it and let me know the result.请测试一下,让我知道结果。

I faced similar issue and when I trying to convert the image 64F to CV_U8 I would end up with a black screen.我遇到了类似的问题,当我尝试将图像 64F 转换为 CV_U8 时,最终会出现黑屏。

This link will help you understand the datatypes and conversion.链接将帮助您了解数据类型和转换。 Below is the code that worked for me.下面是对我有用的代码。

from skimage import img_as_ubyte
cv_image = img_as_ubyte(any_skimage_image)

For those getting a black screen or lots of noise, you'll want to normalize your image first before converting to 8-bit.对于那些出现黑屏或大量噪点的人,您需要先对图像进行标准化,然后再转换为 8 位。 This is done with numpy directly as OpenCV uses numpy arrays for its images.这是直接使用 numpy 完成的,因为 OpenCV 对其图像使用 numpy 数组。

Before normalization, the image's range is from 4267.0 to -4407.0 in my case.在标准化之前,图像的范围在我的情况下是从4267.0-4407.0 Now to normalize:现在规范化:

# img is a numpy array/cv2 image
img = img - img.min() # Now between 0 and 8674
img = img / img.max() * 255

Now that the image is between 0 and 255, we can convert to a 8-bit integer.现在图像在 0 到 255 之间,我们可以转换为 8 位整数。

new_img = np.uint8(img)

This can also be done by img.astype(np.uint8) .这也可以通过img.astype(np.uint8)来完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python:不支持图像的 OpenCV 深度(CV_64F) - python: OpenCV depth of image unsupported (CV_64F) 不支持的输入图像深度:> 'VDepth::contains(depth)' > 其中 > 'depth' 为 6 (CV_64F) - Unsupported depth of input image: > 'VDepth::contains(depth)' > where > 'depth' is 6 (CV_64F) depth' 是 6 (cv_64f) cvtcolor 不起作用 - depth' is 6 (cv_64f) cvtcolor does not work OpenCV:错误(-215)深度== CV_8U || 深度== CV_16U || 深度== CV ::: cvtColor函数中的CV_32F - OpenCV: Error (-215) depth == CV_8U || depth == CV_16U|| depth == CV_32F in function cv:::cvtColor OpenCV 错误:断言失败 ((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F)) 在 cvtColor 中 - OpenCV Error: Assertion failed ((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F)) in cvtColor OpenCV 错误:(-215:断言失败) (M0.type() == CV_32F || M0.type() == CV_64F) && M0.rows == 3 && M0.cols == 3 in function 'warpPerspective' - OpenCV error: (-215:Assertion failed) (M0.type() == CV_32F || M0.type() == CV_64F) && M0.rows == 3 && M0.cols == 3 in function 'warpPerspective' OpenCV(4.0.0)Python错误:( - 15:断言失败)(mtype == CV_8U || mtype == CV_8S)&& _mask.sameSize(* psrc1)函数'cv :: binary_op' - OpenCV(4.0.0) Python Error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op' OpenCV Python 错误:错误:(-215) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function cv::binary_op - OpenCV Python Error: error: (-215) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function cv::binary_op OpenCV 掩码错误错误:(-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op - OpenCV Mask Error error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op Bitwise_and 掩码错误:(-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op' - Bitwise_and mask error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM