我试图在python中使用skimage来确定彩色图像的渐变图像。
我遵循的方法是:
1)对于每个RGB波段,计算每个波段的梯度。这导致6个阵列,每个色带2个。每个色带在x和y方向上都具有梯度。(2个方向x 3个颜色= 6个阵列)。
2)为了确定图像的梯度,计算每个色带的大小为:
梯度=((Rx ^ 2 + Ry ^ 2)+(Gx ^ 2 + Gy ^ 2)+(Bx ^ 2 + By ^ 2))^ 0.5
但结果很嘈杂,渐变不清楚。import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage as nd
import skimage.data as dt
img = dt.astronaut()
def gradient(img, x = True, y = True):
f1 = np.array([[-1,-2,-1], [0,0,0], [1,2,1]])
f2 = np.array([[-1,-2,-1], [0,0,0], [1,2,1]]).T
vert_gradient =nd.correlate(img, f1)
horz_gradient =nd.correlate(img, f2)
if x:
return(horz_gradient)
else:
return (vert_gradient)
Rx = gradient(img[:,:,0], y = False)
Ry = gradient(img[:,:,0], x = False)
Gx = gradient(img[:,:,1], y = False)
Gy = gradient(img[:,:,1], x = False)
Bx = gradient(img[:,:,2], y = False)
By = gradient(img[:,:,2], x = False)
grad = np.sqrt(np.square(Rx) + np.square(Ry)) + np.sqrt(np.square(Gx) + np.square(Gy)) + np.sqrt(np.square(Bx) + np.square(By))
grad = ((grad - grad.min()) / (grad.max() - grad.min())) * 255 # rescale for full dynamic range for 8 bit image
grad = grad.astype(np.uint8)
plt.subplot(1,2,1)
plt.imshow(img)
plt.subplot(1,2,2)
plt.imshow(grad)
plt.show()
在渐变图像中,我们可以看到颜色渐变,但它们不是那么清晰,并且有很多噪音。
在计算渐变之前,我还尝试平滑每个色带上的噪点。
如何在不使用OpenCv的情况下改善此结果?