【问题标题】:Color thresholding an HLS jpeg image对 HLS jpeg 图像进行颜色阈值处理
【发布时间】:2017-08-11 22:39:03
【问题描述】:

我正在尝试为 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] &lt; thresh[0] 的任何值,图像都显示为全黑。即最大阈值小于最小阈值。而且我不知道为什么会这样。

我已经检查了几次代码,我没有看到任何错误。我在此处粘贴的代码与我正在使用的代码之间的唯一区别是,我在 Jupyter 笔记本中使用 IPython 小部件进行交互。

我非常感谢有关此主题的任何帮助或见解。 我还附上了我所说的两个例子。 提前致谢。

【问题讨论】:

    标签: python opencv image-processing matplotlib cv2


    【解决方案1】:

    线

    二进制[(channel > thresh[0]) | (channel 
    
    

    如果thresh[0] &lt; thresh[1],则将所有像素设置为1。我想这不是你想要的。

    实际上并不清楚您真正想要什么。假设这两个阈值内的像素应该是白色的,您将使用逻辑 and 代替。

    二进制[(channel > thresh[0]) & (channel 
        

    【讨论】:

    • 非常感谢!一个复制粘贴错误花了我半天时间。我想知道为什么它以前有效。
    猜你喜欢
    • 2013-05-19
    • 2021-12-23
    • 2016-03-03
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 2021-02-10
    相关资源
    最近更新 更多