我想提高图像质量,所以我要提高分辨率,使用skimage和scipy插值和锐化边缘。然后,我想使用改进图像的阈值进行进一步分析。问题是,当我尝试将锐化后的图像数组传递到cv2.threshold
函数中时,出现错误。
代码:
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)
_, thresh = cv2.threshold(sharp_img,
0,
255,
cv2.THRESH_BINARY+cv2.THRESH_OTSU)
plt.imsave('sharp_img.png', sharp_img, cmap = 'gray')
输出:
Traceback (most recent call last):
File "/home/artur/Desktop/test.py", line 32, in <module>
cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/thresh.cpp:1406: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'threshold'
我尝试通过以下方法将图像转换成cv.threshold
函数之前>
sharp_img = skimage.img_as_ubyte(sharp_img)
输出:
Traceback (most recent call last):
File "/home/artur/Desktop/test.py", line 24, 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.
我如何进行这项工作?
答案 0 :(得分:1)
您需要确保锐化后的图像的像素值介于-1和1之间。为此,您可以将sharp_img
规范化为[0,1]范围,然后再转换为uint8
或只需像这样使用NumPy的clip:
sharp_img = skimage.img_as_ubyte(np.clip(sharp_img, 0, 1))