Opencv使白色像素比图像中的其他像素更亮,以便在阈值设置后显示

问题描述

我有一个带有白线的图像,我正在尝试使该线更亮,以便在阈值化之后可以清楚地显示它,如预期输出所示。但是,使用我的代码,它无法检测到该行。

enter image description here

我的代码

img = mpimg.imread(filenm)

hsv_img = cv2.cvtColor(image,cv2.COLOR_RGB2HSV)

mask_white = cv2.inRange(img,(200,200,200),(255,255,255))
mask_yellow = cv2.inRange(hsv_img,(15,60,20),(25,255))
color_mask = cv2.bitwise_or(mask_white,mask_yellow)
mask_img[color_mask == 0] = [0,0]

# apply image thresholding
img = cv2.bitwise_and(mask_img[:,:,0],masked_img[:,mask=stencil)
ret,thresh = cv2.threshold(img,130,145,cv2.THRESH_BINARY)

# plot image
plt.figure(figsize=(10,10))
plt.imshow(thresh,cmap= "gray")

预期输出

enter image description here

解决方法

您可以使用HSV-Color-Picker查找inRange方法的上下颜色输入。

import cv2
import numpy as np

img = cv2.imread("NPmF8.jpg")
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lwr = np.array([16,23,129])
upp = np.array([36,43,209])
img_mask = cv2.inRange(hsv,lwr,upp)

结果:

enter image description here

然后您可以应用line-detector来检测行:

lines = cv2.ximgproc.createFastLineDetector().detect(img_mask)

for ln in lines:
    x1 = int(ln[0][0])
    y1 = int(ln[0][1])
    x2 = int(ln[0][2])
    y2 = int(ln[0][3])
    cv2.line(img,pt1=(x1,y1),pt2=(x2,y2),color=(0,255,0),thickness=3)

结果:

enter image description here