python - 在 cv2 中使用 skimage 图像

标签 python opencv matplotlib scipy scikit-image

我正在尝试使用 skimage 和 scipy 提高灰度图像的图像质量,然后在 cv2 中使用它们。不幸的是,当我尝试使用 cv2 转换 skimage 图像时出现错误。

图片: enter image description here

import skimage
import scipy
import cv2
import matplotlib.pyplot as plt


img = cv2.imread('Image.png')

scale = 5
img_rs = skimage.transform.rescale(image = img,
                                   scale = scale,
                                   order = 3,
                                   mode = 'wrap',
                                   cval = 0,
                                   multichannel = True,
                                   anti_aliasing = 'none')
img_int = scipy.misc.imresize(img_rs, 0.99999, interp = 'cubic')
img_int_gray = skimage.color.rgb2gray(img_int)
blurred_img = scipy.ndimage.gaussian_filter(img_int_gray, 3)
filter_blurred_img = scipy.ndimage.gaussian_filter(blurred_img, 1)
alpha = 30
sharp_img = blurred_img + alpha * (blurred_img - filter_blurred_img)

#error
img_gs = cv2.cvtColor(sharp_img, cv2.COLOR_BGR2GRAY)

plt.imsave('sharp_img.png', img_gs, cmap = 'gray')

输出:

Traceback (most recent call last):
  File "/home/artur/Desktop/test.py", line 25, in <module>
    img_gs = cv2.cvtColor(sharp_img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/color.hpp:255: error: (-2:Unspecified error) in function 'cv::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::Set<3, 4>; VDcn = cv::Set<1>; VDepth = cv::Set<0, 2, 5>; cv::SizePolicy sizePolicy = (cv::SizePolicy)2u; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
> Invalid number of channels in input image:
>     'VScn::contains(scn)'
> where
>     'scn' is 1

现在我读到 skimage 和 cv2 使用不同的格式,所以我尝试在转换为 cv2 之前添加这一行,这应该使它与 cv2 兼容:

sharp_img = skimage.img_as_ubyte(sharp_img)

输出:

Traceback (most recent call last):
  File "/home/artur/Desktop/test.py", line 23, in <module>
    sharp_img = skimage.img_as_ubyte(sharp_img)
  File "/home/artur/.local/lib/python3.6/site-packages/skimage/util/dtype.py", line 492, in img_as_ubyte
    return convert(image, np.uint8, force_copy)
  File "/home/artur/.local/lib/python3.6/site-packages/skimage/util/dtype.py", line 261, in convert
    raise ValueError("Images of type float must be between -1 and 1.")
ValueError: Images of type float must be between -1 and 1.

我做错了什么?

最佳答案

你已经在行中将图片转为灰度

img_int_gray = skimage.color.rgb2gray(img_int)

它失败了

img_gs = cv2.cvtColor(sharp_img, cv2.COLOR_BGR2GRAY)

因为它需要一个 3 channel BGR 图像。删除该行,您只需使用 plt.imsave('sharp_img.png', sharp_img, cmap = 'gray')

保存 sharp_img

关于python - 在 cv2 中使用 skimage 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53105780/

相关文章:

python - opencv cv2.imshow自动向小图像添加填充

python - 有没有一种方法可以将多个图像保存到python中的数组中供以后使用?

c# - 如何获取 EmguCV ImageBox 图像?

python - 设置 yticks 颜色以匹配条形图颜色 python

python - 在 python matplotlib 上使用主要 xticks 的问题

python - 结合两个 matplotlib 颜色图

python - 在Python中永久设置种子

python - 根据Python中的另一个字典列表对字典列表进行排序

python - 如何查找所有数据框的最大值,最小值[不是按列的值,也不是按行的值]

python - Python 中的交互式绘图?