你缺少的是数量级的阈值。你可以知道它是否有任何相关的方向。。。在某种程度上,它是一种对方向进行过滤的方法,只给出响应强的方向,在大多数情况下,这些方向是边缘。
如果这一步没有完成,那么每一个像素至少有一个4种颜色,我认为这不是你想要的。
下面是一个示例代码来说明我的观点:
import numpy as np
import cv2
# reads the image
img = cv2.imread("lena.png", 0
# sobel derivatives
derivX = cv2.Sobel(img, cv2.CV_32F, 1, 0)
derivY = cv2.Sobel(img, cv2.CV_32F, 0, 1)
# orientation and magnitude
orien = cv2.phase(derivX, derivY, angleInDegrees=True)
mag = cv2.magnitude(derivX, derivY)
# thresholding of the magnitude values, play with the thresh value adjust it too your liking
thresh = 50
_, mask = cv2.threshold(mag, thresh, 255, cv2.THRESH_BINARY)
# I used OpenCV imshow instead of matplotlib, so the colors are in BGR (use yours)
red = np.array([0, 0, 255])
cyan = np.array([255, 255, 0])
green = np.array([0, 255, 0])
yellow = np.array([0, 255, 255])
# for the same reason I use np.uint8
image_map = np.zeros((orien.shape[0], orien.shape[1], 3), dtype=np.uint8)
# setting the colors, maybe there is a better way, my numpy skills are rusty
# it checks that magnitude is above the threshold and that the orientation is in range
image_map[ (mask == 255) & (orien < 90) ] = red
image_map[(mask == 255) & (orien > 90) & (orien < 180)] = cyan
image_map[(mask == 255) & (orien > 180) & (orien < 270)] = green
image_map[(mask == 255) & (orien > 270)] = yellow
# just for showing it with opencv, replace it with matplotlib if you prefer
cv2.imshow("frame", image_map)
cv2.waitKey(0)
结果是: