简体   繁体   中英

Using skimage images in cv2

I am trying to improve image quality of grayscale images with skimage and scipy and then use them in cv2. Unfortunately I get an error when I try to convert the skimage image with cv2.

Image: 在此处输入图片说明

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')

Output:

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

Now I read that skimage and cv2 use different format so I tried adding this line before converting to cv2, which should make it compatible with cv2:

sharp_img = skimage.img_as_ubyte(sharp_img)

Output:

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.

What am I doing wrong?

You have already converted the image to grayscale in the line

img_int_gray = skimage.color.rgb2gray(img_int)

It fails at the line

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

because it is expecting a 3-channel BGR image. Remove that line and you can just save sharp_img with plt.imsave('sharp_img.png', sharp_img, cmap = 'gray')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM