【问题标题】:Gradient for Color Image in PythonPython中彩色图像的渐变
【发布时间】:2019-06-05 03:55:11
【问题描述】:

我正在尝试在 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 的情况下改进这个结果?

【问题讨论】:

  • 我们需要查看您的结果图片。
  • 我建议尝试使用更简单的图像来测试这种方法是否可行。例如,测试图像的红色可能从左到右从 255 减少到 0。如果你得到一个恒定的梯度向量幅度,你就知道你在正确的轨道上。
  • 使用您指定的公式是否有特定原因?最后,您会以更复杂的方式得到类似于拉普拉斯滤波器的东西。也许高斯差可以给你更好的结果。
  • 先尝试将输入图像转换为浮点数。

标签: python image-processing scikit-image


【解决方案1】:

像这样分别找到每个通道的梯度

gradR = np.sqrt(np.square(Rx) + np.square(Ry))
gradG = np.sqrt(np.square(Gx) + np.square(Gy))
gradB = np.sqrt(np.square(Bx) + np.square(By))

制作新图片

grad = np.dstack((gradR,gradG,gradB))

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2021-06-12
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    相关资源
    最近更新 更多