我正在尝试为 jpeg 图像设置颜色阈值,以便我可以保持车道线并希望摆脱世界其他地方。
我正在使用 cv2
读取图像如下:
test_image = cv2.imread(myimage)
#convert to RGB space from BGR
test_image = cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB)
然后,我将 test_image 转换为 HLS 颜色空间,以保留 l channel ,如下所示:
def get_l_channel_hls(img):
# Convert to HLS color space and separate the l channel
hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS).astype(np.float)
l_channel = hls[:,:,1]
return l_channel
然后我将一些阈值应用到这个 l channel 并将像素值替换为 0 或 1,这样我就可以只保留我想要的像素。 (稍后我会将这些值乘以 255,使保留的像素显示为白色)
def get_color_channel_thresholded_binary(channel, thresh=(0,255):
# Threshold color channel
print("min color threshold: {}".format(thresh[0]))
print("max color threshold: {}".format(thresh[1]))
binary = np.zeros_like(channel)
binary[(channel > thresh[0]) | (channel <= thresh[1])] = 1
return binary
然后,我拿这个二值图并将保留的像素替换为 255
def get_overall_combined_binary(binary1):
grayscaled_binary = np.zeros_like(binary1)
grayscaled_binary [(binary1 == 1)] = 255
return grayscaled_binary
然后我使用 matplotlib pyplot 显示这个二进制文件,如下所示:
import matplotlib.pyplot as plt
#grayscaled_binary = # get the grayscaled binary using above APIs
imshow(grayscaled_binary, cmap='gray')
我在这里观察到的东西很奇怪。对于 thresh[1] < thresh[0]
的任何值,图像显示为全黑.即最大阈值小于最小阈值。我不知道为什么会这样。
我已经检查了几次代码,没有发现任何错误。我在此处粘贴的代码与我正在使用的代码之间的唯一区别是,我在 Jupyter notebook 中使用 IPython 小部件进行交互。
最佳答案
线
binary[(channel > thresh[0]) | (channel <= thresh[1])] = 1
如果 thresh[0] < thresh[1]
则将所有像素设置为 1 .我想这不是您想要的。
您并不清楚您真正想要的是什么。假设两个阈值内的那些像素应该是白色的,您将使用逻辑 and
代替。
binary[(channel > thresh[0]) & (channel <= thresh[1])] = 1
关于python - 对 HLS jpeg 图像进行颜色阈值处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42907203/