颜色阈值处理HLS jpeg图像

时间:2017-03-20 15:10:00

标签: python opencv image-processing matplotlib cv2

我正在尝试为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通道,如下所示:

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通道应用一些阈值,并将像素值替换为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笔记本中使用IPython小部件进行交互。

我非常感谢您对此主题的任何帮助或见解。 我还附上两个我正在谈论的例子。 提前致谢。Failure Scenario enter image description here

1 个答案:

答案 0 :(得分:1)

该行

binary[(channel > thresh[0]) | (channel <= thresh[1])] = 1

如果thresh[0] < thresh[1]

将所有像素设置为1。我想这不是你想要的。

它并不是很清楚你真正想要的是什么。假设那些在两个阈值范围内的像素应为白色,那么您可以使用逻辑 and

binary[(channel > thresh[0]) & (channel <= thresh[1])] = 1

相关问题