我尝试创建一个具有十二个方向特征提取功能的图像处理程序(来自此url源https://www.ijcaonline.org/journal/number3/pxc387173.pdf)
我用这段代码得到了梯度结果
def sobel(citra_thresh):
h, w = citra_thresh.shape
# define filters
horizontal = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
vertical = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
newhorizontalImage = np.zeros((h, w))
newverticalImage = np.zeros((h, w))
newgradientImage = np.zeros((h, w))
newdirectionImage = np.zeros((h,w))
for i in range(1, h - 1):
for j in range(1, w - 1):
horizontalGrad = (horizontal[0, 0] * citra_thresh[i - 1, j - 1]) + \
(horizontal[0, 1] * citra_thresh[i - 1, j]) + \
(horizontal[0, 2] * citra_thresh[i - 1, j + 1]) + \
(horizontal[1, 0] * citra_thresh[i, j - 1]) + \
(horizontal[1, 1] * citra_thresh[i, j]) + \
(horizontal[1, 2] * citra_thresh[i, j + 1]) + \
(horizontal[2, 0] * citra_thresh[i + 1, j - 1]) + \
(horizontal[2, 1] * citra_thresh[i + 1, j]) + \
(horizontal[2, 2] * citra_thresh[i + 1, j + 1])
#if (horizontalGrad == 0):
# horizontalGrad = 0.0001
newhorizontalImage[i - 1, j - 1] = abs(horizontalGrad)
verticalGrad = (vertical[0, 0] * citra_thresh[i - 1, j - 1]) + \
(vertical[0, 1] * citra_thresh[i - 1, j]) + \
(vertical[0, 2] * citra_thresh[i - 1, j + 1]) + \
(vertical[1, 0] * citra_thresh[i, j - 1]) + \
(vertical[1, 1] * citra_thresh[i, j]) + \
(vertical[1, 2] * citra_thresh[i, j + 1]) + \
(vertical[2, 0] * citra_thresh[i + 1, j - 1]) + \
(vertical[2, 1] * citra_thresh[i + 1, j]) + \
(vertical[2, 2] * citra_thresh[i + 1, j + 1])
newverticalImage[i - 1, j - 1] = abs(verticalGrad)
mag = np.sqrt(pow(horizontalGrad, 2.0) + pow(verticalGrad, 2.0))
newgradientImage[i - 1, j - 1] = mag
newgradientImage = newgradientImage.astype(np.int)
direct = np.arctan2((verticalGrad),(horizontalGrad))
#tes=(horizontalGrad/verticalGrad)
#direct = (np.arctan(tes))
newdirectionImage[i - 1, j - 1] = direct
#newdirectionImage = newdirectionImage.astype(np.float)
newdirectionImage = np.around(newdirectionImage,decimals=2)
return newdirectionImage
我试图获取这张图片的渐变
我得到了这个结果
期刊希望渐变值应介于0到6.28之间。
但是我得到了负值梯度。如何将它们转换为0到6.28范围?