简体   繁体   English

如何将numpy数组更改为灰度opencv图像

[英]How to change numpy array into grayscale opencv image

How can I change numpy array into grayscale opencv image in python?如何在python中将numpy数组更改为灰度opencv图像? After some processing I got an array with following atributes: max value is: 0.99999999988, min value is 8.269656407e-08 and type is: <type 'numpy.ndarray'> .经过一些处理,我得到了一个具有以下属性的数组:最大值为:0.99999999988,最小值为 8.269656407e-08,类型为: <type 'numpy.ndarray'> I can show it as an image using cv2.imshow() function, but I can't pass it into cv2.AdaptiveTreshold() function because it has wrong type:我可以使用cv2.imshow()函数将其显示为图像,但我无法将其传递给cv2.AdaptiveTreshold()函数,因为它的类型错误:

error: (-215) src.type() == CV_8UC1 in function cv::adaptiveThreshold

How can I convert this np.array to correct format?如何将此 np.array 转换为正确的格式?

As the assertion states, adaptiveThreshold() requires a single-channeled 8-bit image.正如断言所述, adaptiveThreshold()需要单通道 8 位图像。

Assuming your floating-point image ranges from 0 to 1, which appears to be the case, you can convert the image by multiplying by 255 and casting to np.uint8 :假设您的浮点图像范围从 0 到 1,这似乎是这种情况,您可以通过乘以 255 并转换为np.uint8来转换图像:

float_img = np.random.random((4,4))
im = np.array(float_img * 255, dtype = np.uint8)
threshed = cv2.adaptiveThreshold(im, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 0)

This one worked for me:这个对我有用:

uint_img = np.array(float_arr*255).astype('uint8')

grayImage = cv2.cvtColor(uint_img, cv2.COLOR_GRAY2BGR)

I need to convert closed image(morphological closing) to binary, and after checking @Aurelius solution, This one work for me better than mentioned solution.我需要将闭合图像(形态闭合)转换为二进制,并在检查@Aurelius 解决方案后,这比提到的解决方案更适合我。

Python cv2.CV_8UC1() Examples Python cv2.CV_8UC1() 示例

mask_gray = cv2.normalize(src=mask_gray, dst=None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)

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

相关问题 如何在openCV中将灰度图像加载为numpy数组 - How to load grayscale image as numpy array in openCV 如何将图像更改为表示为NumPy数组的灰度 - How to change an image to grayscale represented as a NumPy array 如何使用 matplotlib/numpy 将数组保存为灰度图像? - how to save an array as a grayscale image with matplotlib/numpy? 如何使用 Numpy 和 Matplotlib 在 RGB 图像之上叠加灰度蒙版(opencv 或 scikit 图像以防万一) - How to overlay Grayscale Mask on top of RGB image using Numpy and Matplotlib ( opencv or scikit image in case not possible) 用于傅里叶变换的灰度图像到NumPy阵列 - Grayscale image to NumPy array for Fourier transform 如何将图像的 class 从 NoneType 更改为 OpenCv 中的 NumPy 数组? - How can I change class of an image from NoneType to NumPy Array in OpenCv? 将图像numpy数组直接转换为灰度数组而不保存图像 - Convert image numpy array into grayscale array directly without saving image Python - 将颜色贴图应用于灰度numpy数组并将其转换为图像 - Python - Applying a color map to a grayscale numpy array and converting it to an image 将numpy数组作为灰度图像上传到S3存储桶 - Upload numpy array as grayscale image to S3 bucket 从多维numpy数组创建高质量的灰度图像 - Creating a good quality grayscale image from an multidimensional numpy array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM