【发布时间】:2018-08-03 19:53:35
【问题描述】:
参考this帖子,当将颜色渐变方向的程度硬编码到图像上时,强度变化的地方应该着色,而没有变化的地方,图像应该是黑色的。
我不确定该帖子是如何实现的。由于所有度数都被分配了一种颜色,因此对图像中的所有像素进行着色而不留下任何黑色。
我的代码如下:
# where gray_blur is a grayscale image of dimension 512 by 512
# 3x3 sobel filters for edge detection
sobel_x = np.array([[ -1, 0, 1],
[ -2, 0, 2],
[ -1, 0, 1]])
sobel_y = np.array([[ -1, -2, -1],
[ 0, 0, 0],
[ 1, 2, 1]])
# Filter the orginal and blurred grayscale images using filter2D
filtered = cv2.filter2D(gray_noise, cv2.CV_32F, sobel_x)
filtered_blurred_x = cv2.filter2D(gray_blur, cv2.CV_32F, sobel_x)
filtered_blurred_y = cv2.filter2D(gray_blur, cv2.CV_32F, sobel_y)
# Compute the orientation of the image
orien = cv2.phase(filtered_blurred_x, filtered_blurred_y, angleInDegrees=True)
image_map = np.zeros((orien.shape[0], orien.shape[1], 3), dtype=np.int16)
# Define RGB colours
red = np.array([255, 0, 0])
cyan = np.array([0, 255, 255])
green = np.array([0, 255, 0])
yellow = np.array([255, 255, 0])
# Set colours corresponding to angles
for i in range(0, image_map.shape[0]):
for j in range(0, image_map.shape[1]):
if orien[i][j] < 90.0:
image_map[i, j, :] = red
elif orien[i][j] >= 90.0 and orien[i][j] < 180.0:
image_map[i, j, :] = cyan
elif orien[i][j] >= 180.0 and orien[i][j] < 270.0:
image_map[i, j, :] = green
elif orien[i][j] >= 270.0 and orien[i][j] < 360.0:
image_map[i, j, :] = yellow
# Display gradient orientation
f, ax1 = plt.subplots(1, 1, figsize=(20,10))
ax1.set_title('gradient orientation')
ax1.imshow(image_map)
我的代码在左侧生成输出,而我认为正确的表示是右侧的图像:
我想我错过了将每个像素硬编码为这些颜色之一的东西。
【问题讨论】: