关于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)
我的代码在左侧产生输出,而我相信正确的表示形式将是右侧的图像:
我想我缺少将每个像素硬编码为这些颜色之一的方法。
答案 0 :(得分:2)
您所缺少的是幅度阈值。通过幅度,您可以知道它实际上是否具有任何相关的方向...以某种方式,它可以过滤方向以仅给出具有强烈响应的方向,在大多数情况下为边缘。
如果不执行此步骤,则每个像素将至少具有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)
结果是: