첫째, 나에게 약간 질문을 바꿔 보자
왜 소벨 연산자의 scipy.ndimage
의 버전은 위키 백과에 주어진 정의에서 반전이 나타 납니까?
차이점을 보여주는 비교 결과는 다음과 같습니다. 입력을 위해, 우리는 위키 피 디아 문서에서 자전거 이미지를 사용 : 값이 효율적으로 뒤집 http://en.wikipedia.org/wiki/File:Bikesgray.jpg
import matplotlib.pyplot as plt
import numpy as np
import scipy.ndimage as ndimage
# Note that if we leave this as unsigned integers we'll have problems with
# underflow anywhere the gradient is negative. Therefore, we'll cast as floats.
z = ndimage.imread('Bikesgray.jpg').astype(float)
# Wikipedia Definition of the x-direction Sobel operator...
kernel = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]], dtype=float)
sobel_wiki = ndimage.convolve(z, kernel)
# Scipy.ndimage version of the x-direction Sobel operator...
sobel_scipy = ndimage.sobel(z, axis=1)
fig, axes = plt.subplots(figsize=(6, 15), nrows=3)
axes[0].set(title='Original')
axes[0].imshow(z, interpolation='none', cmap='gray')
axes[1].set(title='Wikipedia Definition')
axes[1].imshow(sobel_wiki, interpolation='none', cmap='gray')
axes[2].set(title='Scipy.ndimage Definition')
axes[2].imshow(sobel_scipy, interpolation='none', cmap='gray')
plt.show()
공지있다.
배후 논리
은 소벨 필터 구배 연산자 (
numpy.gradient
의 가장자리 제외
[1, 0, -1]
와 회선에 상당) 기본적이다. Wikipedia에 주어진 정의는 그라디언트의 수학적 정의가 부정적임을 나타냅니다.
import matplotlib.pyplot as plt
import numpy as np
import scipy.ndimage as ndimage
z = ndimage.imread('/home/jofer/Bikesgray.jpg').astype(float)
sobel = ndimage.sobel(z, 1)
gradient_y, gradient_x = np.gradient(z)
fig, axes = plt.subplots(ncols=2, figsize=(12, 5))
axes[0].imshow(sobel, interpolation='none', cmap='gray')
axes[0].set(title="Scipy's Sobel")
axes[1].imshow(gradient_x, interpolation='none', cmap='gray')
axes[1].set(title="Numpy's Gradient")
plt.show()
따라서, scipy.ndimage
우리가 수학 그라데이션에서 기대할 수있는 것과 일치의 컨벤션을 사용하고 있습니다 : 예를 들어
는 numpy.gradient
는 scipy.ndimage
의 소벨 필터와 비슷한 결과를 제공 .
빠른 사이드 노트 : 실제로 "sobel 필터"라고하는 것은 실제로 다른 축을 따라 두 개의 소벨 필터에서 계산 된 그래디언트 크기입니다. 또한 모든 문제는 입력 기울기의 절대 값으로 당신이이 경우에 사용하는 규칙 중요하지 않습니다
import matplotlib.pyplot as plt
import scipy.ndimage as ndimage
z = ndimage.imread('/home/jofer/Bikesgray.jpg').astype(float)
sobel = ndimage.generic_gradient_magnitude(z, ndimage.sobel)
fig, ax = plt.subplots()
ax.imshow(sobel, interpolation='none', cmap='gray')
plt.show()
: scipy.ndimage
, 당신은으로 계산 것입니다.
대부분의 경우에 조정 가능한 창 (예 : ndimage.gaussian_gradient_magnitude
)이있는 부드러운 그라디언트 필터는 가장자리 감지에 더 적합합니다.
** 프로그래밍 ** 질문은 무엇입니까? – Taosique
링크에는 그들이 사용하는 Y 규칙이 나와 있지 않습니다. 오리엔테이션 규칙에 관계없이 F (X + 1, Y) -F (X-1, Y)와 F (X, Y + 1) -F (X, Y-1)를 사용하십시오. –
@Taosique 혼란스럽게 생각합니다. SciPy에 예제를 추가하여 제 질문을보다 명확하게 설명했습니다. – Ukyoi