简体   繁体   English

无法使用 scikit 图像将 RGB 图像转换为灰度

[英]Not able to convert RGB image to gray scale using scikit image

I have tried to convert an RGB image to Otsu binary image (gray scale) but that doesn't seem to work as I get the error as mentioned below.我曾尝试将 RGB 图像转换为 Otsu 二进制图像(灰度),但这似乎不起作用,因为我收到了如下所述的错误。

from cv2 import cv2
import numpy as np
from skimage.color import rgb2gray
from skimage.filters import threshold_otsu
from skimage.io import imread
from skimage.morphology import skeletonize
from skimage.util import invert
import matplotlib.pyplot as plt

img = rgb2gray(imread('Ared.png'))
binary = img > threshold_otsu(img)
np.unique(binary)
skeleton = skeletonize(invert(binary))
cv2.imshow('original', img)
cv2.imshow('skeleton', skeleton)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result in terminal结果在终端

  img = rgb2gray(imread('Ared.png'))
Traceback (most recent call last):
  File "preprocessing.py", line 16, in <module>
    cv2.imshow('skeleton', skeleton)
TypeError: Expected Ptr<cv::UMat> for argument '%s'

Bad exception messages are bad...错误的异常消息很糟糕...

cv2.imshow does not handle binary arrays. cv2.imshow不处理二进制数组。 The acceptable types according to this answer are uint8 , uint16 , int , float , and double .根据答案可接受的类型是uint8uint16intfloatdouble

you should be able to convert the array to uint8 with:您应该能够使用以下命令将数组转换为uint8

skeleton.astype('u1')

This will leave you with values between 0 and 1 though, which is all very dark.不过,这会给你留下 0 到 1 之间的值,这很暗。 If you the multiply the array by 255, the colors should be black and white as expected:如果将数组乘以 255,则颜色应为预期的黑色和白色:

skeleton.astype('u1') * 255

full example with data image from skimage:来自 skimage 的数据图像的完整示例:

from cv2 import cv2
from skimage.filters import threshold_otsu
from skimage.morphology import skeletonize
from skimage.util import invert
from skimage.data import camera

img = camera()
binary = img > threshold_otsu(img)
skeleton = skeletonize(invert(binary))
cv2.imshow('original', img)
cv2.imshow('skeleton', skeleton.astype('u1')*255)
cv2.waitKey(0)
cv2.destroyAllWindows()

Your format is wrong.你的格式不对。 You need to change it to float32.您需要将其更改为 float32。 That's a common error with opencv.这是 opencv 的一个常见错误。 You can change this line to convert it to float32 and it should work fine.您可以更改此行以将其转换为 float32,它应该可以正常工作。

cv2.imshow('skeleton', np.float32(skeleton))

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

相关问题 我想使用Python将32 * 32 * 3 rgb图像转换为32 * 32 * 1维度的灰度 - I want to convert 32*32*3 rgb image to gray scale with 32*32*1 dimension using Python scikit-image color.rgb2gray 警告 - scikit-image color.rgb2gray warning 我如何使用python将图像转换为灰度? - how do i convert image into gray scale using python? 如何将图像从灰度格式更改为 RGB 格式 - How to change image from gray scale format into RGB format 转换图像灰度python错误 - convert image gray scale python error 将灰色 pixel_array 从 DICOM 转换为 RGB 图像 - Convert gray pixel_array from DICOM to RGB image “FutureWarning:rgb2gray 的行为将在 scikit-image 0.19 中发生变化”实际上是什么意思? - What does "FutureWarning: The behavior of rgb2gray will change in scikit-image 0.19" actually mean? 在python中使用用户定义的转换功能将灰度图像转换回rgb图像时出现问题 - Problem in converting a gray scale image back to an rgb image with user defined transformation function in python 截屏并使用枕头将其转换为灰度图像的最佳方法是什么? - What is is best way to take screenshot and convert it into gray scale image using pillow? 如何使用python连接图像的灰度和RGB通道 - How concatenate gray and rgb channel of image using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM