【发布时间】:2018-10-20 01:29:45
【问题描述】:
我正在尝试将图像分离为其 rgb 颜色分量,然后尝试对每个颜色分量图像执行 sobel h 和 v 过滤器。我不太确定问题是什么,但我得到了以下除以 0 的错误。我认为这可能是因为我的 u 和 v 原来是完全相同的数组。
错误
/Users/Sam/PycharmProjects/temp/A2.py:51: RuntimeWarning: divide by zero encountered in true_divide
theta = 0.5 * np.arctan(2 * gxy / (gxx - gyy))
/Users/Sam/PycharmProjects/temp/A2.py:51: RuntimeWarning: invalid value encountered in true_divide
theta = 0.5 * np.arctan(2 * gxy / (gxx - gyy))
/Users/Sam/PycharmProjects/temp/A2.py:54: RuntimeWarning: invalid value encountered in sqrt
fTheta = np.sqrt(0.5 * ((gxx + gyy) + (gxx - gyy) * np.cos(2 * theta) + (2 * gxy * np.sin(2 * theta))))
Traceback (most recent call last):
File "/Users/Sam/PycharmProjects/A1/venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 51, in _wrapfunc
return getattr(obj, method)(*args, **kwds)
代码
import skimage.filters as filt
import numpy as np
def color_dot_product(A, B):
return np.sum(A.conj() * B, axis=2)
mushroom = io.imread('mushroom.jpg')
I = util.img_as_float(mushroom)
red = I[:, :, 0] # Zero out contribution from green
blue = I[:,:,1]
green = I[:,:,2]
# Compute horizontal and vertical derivatives of red, blue and green channels through applying sobel filters
u = I.copy()
u[:, :, 0] = filt.sobel_h(red)
u[:, :, 1] = filt.sobel_h(green)
u[:, :, 2] = filt.sobel_h(blue)
v = I.copy()
v[:, :, 0] = filt.sobel_v(red)
v[:, :, 1] = filt.sobel_v(green)
v[:, :, 2] = filt.sobel_v(blue)
gxx = color_dot_product(u, u)
gyy = color_dot_product(v, v)
gxy = color_dot_product(u, v)
# Calculate gradient direction (direction of the largest colour change)
theta = 0.5 * np.arctan(2 * gxy / (gxx - gyy))
# Calculate the magnitude of the rate of change
fTheta = np.sqrt(0.5 * ((gxx + gyy) + (gxx - gyy) * np.cos(2 * theta) + (2 * gxy * np.sin(2 * theta))))
【问题讨论】:
-
如果 gxx == gyy 你可以除以零
-
是的,我已经明白了,但我认为 gxx 和 gyy 可能相等,因为 u 和 v 相等
-
任何单个索引都是相等的...您始终可以打印内容以查看它们的外观...或使用断点检查它们
numpy([1,2,3])/numpy([1,2,0])
标签: python image-processing colors scikit-image