python画图渐变颜色的代号_Python中彩色图像的渐变

我试图在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的情况下改善此结果?

cHGLW.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值