[英]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
.根据此答案可接受的类型是
uint8
、 uint16
、 int
、 float
和double
。
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.