HLS-jpeg图像的颜色阈值化

2024-03-29 10:37:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试彩色阈值的jpeg图像,这样我可以保持车道线,并希望摆脱世界其他地方。 我使用cv2读取图像,如下所示:

test_image = cv2.imread(myimage)
#convert to RGB space from BGR
test_image = cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB)

然后,我将测试图像转换为HLS颜色空间,以保留l通道,如下所示:

^{pr2}$

然后我对这个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]的任何值,图像显示为全黑。i、 e.最大阈值小于最小阈值。我不知道为什么会这样。在

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

我非常感谢你对这个话题的任何帮助或见解。 我还附上了我所说的两个例子。 提前谢谢。Failure Scenarioenter image description here


Tags: 代码test图像imagegetdefchannel阈值
1条回答
网友
1楼 · 发布于 2024-03-29 10:37:27

线

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

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

现在还不清楚你到底想要什么。假设那些在两个阈值内的像素应该是白色的,那么应该使用逻辑的and。在

^{pr2}$

相关问题 更多 >