应用阈值后无法显示黑白图像 求HSL车道检测代码Bug修复
问题描述
我正在开展基于HSL图像格式阈值的车道线检测项目,编写了如下代码。代码无报错,但输出仅为全黑图像。我已尝试调整阈值参数、更换多张图像,问题仍未解决,恳请帮忙修复Bug。
原始图像:包含道路、车道线及桥洞阴影的道路场景图
当前输出:全黑图像
代码
import cv2 import numpy as np import logging import matplotlib.pyplot as plt logging.basicConfig(level=logging.INFO, format='%(funcName)s:%(message)s') class LaneFinding: def __init__(self, img): self.img = img def get_hsl_transform(self, thresh: tuple): """ This method will return a HSL image for the given image for which the pixel values are adjusted between the lower and higher threshold. The values between lower and upper threshold will be 1 and all other values will be zero. :param tuple thresh: Tuple of two values, the lower value threshold and upper threshold for the image. """ # Convert the given RGB image to HSL image img_hsl = cv2.cvtColor(self.img, cv2.COLOR_RGB2HLS) # Separate individula channels in the imge and create zero masks for each channel. h = img_hsl[:,:,0] l = img_hsl[:,:,1] s = img_hsl[:,:,2] binary_h = np.zeros_like(img_hsl) binary_s = np.zeros_like(img_hsl) binary_l = np.zeros_like(img_hsl) # Apply threshold to individual channels and make a binary image. binary_h[(h>thresh[0]) & (h<=thresh[1])] = 1 binary_s[(s>thresh[0]) & (s<=thresh[1])] = 1 binary_l[(l>thresh[0]) & (l<=thresh[1])] = 1 return binary_h, binary_s, binary_l if __name__ == '__main__': # This will only check if the new methods added in the class above work or not. img = cv2.imread(r"Gradient_and_color_space\\bridge_shadow.jpg") thresh = (10, 100) lane_finding = LaneFinding(img) _, binary_s, _ = lane_finding.get_hsl_transform(thresh=thresh) plt.imshow(binary_s, cmap='gray') plt.show()
问题分析与修复
代码存在三个核心问题:
1. 颜色空间转换错误
cv2.imread读取图像默认返回BGR格式,但代码中使用cv2.COLOR_RGB2HLS进行转换,这会导致颜色通道映射错误,HSL转换结果完全偏离预期。
2. 二进制掩码维度不匹配
分离出的h/s/l是单通道(2D数组),但创建的binary_h/binary_s/binary_l用np.zeros_like(img_hsl)生成了三通道(3D数组)。赋值1时仅会给单通道赋值,而plt.imshow显示三通道数组时,单通道的1会被视为极低亮度,最终呈现全黑。
3. 阈值参数不符合S通道特征
原始图像中车道线的饱和度(S通道)值较高,原阈值(10,100)过低,无法筛选出车道线像素。
修复后的代码
import cv2 import numpy as np import logging import matplotlib.pyplot as plt logging.basicConfig(level=logging.INFO, format='%(funcName)s:%(message)s') class LaneFinding: def __init__(self, img): self.img = img def get_hsl_transform(self, thresh: tuple): # 将BGR图像转换为HSL格式 img_hsl = cv2.cvtColor(self.img, cv2.COLOR_BGR2HLS) h = img_hsl[:,:,0] l = img_hsl[:,:,1] s = img_hsl[:,:,2] # 创建单通道的二进制掩码,与分离出的通道维度一致 binary_h = np.zeros_like(h) binary_s = np.zeros_like(s) binary_l = np.zeros_like(l) # 对各通道应用阈值,用255作为高亮值更符合图像显示习惯 binary_h[(h > thresh[0]) & (h <= thresh[1])] = 255 binary_s[(s > thresh[0]) & (s <= thresh[1])] = 255 binary_l[(l > thresh[0]) & (l <= thresh[1])] = 255 return binary_h, binary_s, binary_l if __name__ == '__main__': img = cv2.imread(r"Gradient_and_color_space\\bridge_shadow.jpg") # 针对S通道调整阈值,适配车道线的饱和度特征 thresh = (90, 255) lane_finding = LaneFinding(img) _, binary_s, _ = lane_finding.get_hsl_transform(thresh=thresh) plt.imshow(binary_s, cmap='gray') plt.show()
修复说明
- 把
cv2.COLOR_RGB2HLS改为cv2.COLOR_BGR2HLS,匹配cv2.imread的BGR输入格式; - 用
np.zeros_like(h)创建单通道掩码,确保维度匹配; - 将赋值从
1改为255,因为plt.imshow的灰度图中255对应白色,更直观; - 调整S通道阈值为
(90,255),可以有效筛选出高饱和度的车道线像素。
内容的提问来源于stack exchange,提问作者programmer_04_03
相关产品推荐
相关产品推荐

