Opencv 使白色像素比图像中的其他像素更亮,以便在阈值处理后显示
Opencv Make White Pixels brighter than other pixels in the image so that to be displayed after thresholding
我有一张带有白线的图像,我正在尝试使这条线更亮,以便它在阈值处理后能够清晰显示,如预期输出所示。但是,使用我的代码无法检测到该行。
我的代码:
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, 255))
color_mask = cv2.bitwise_or(mask_white, mask_yellow)
mask_img[color_mask == 0] = [0,0,0]
# apply image thresholding
img = cv2.bitwise_and(mask_img[:,:,0], masked_img[:,:,0], mask=stencil)
ret, thresh = cv2.threshold(img, 130, 145, cv2.THRESH_BINARY)
# plot image
plt.figure(figsize=(10,10))
plt.imshow(thresh, cmap= "gray")
预期输出:
您可以使用 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)
结果:
然后你可以应用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)
结果:
我有一张带有白线的图像,我正在尝试使这条线更亮,以便它在阈值处理后能够清晰显示,如预期输出所示。但是,使用我的代码无法检测到该行。
我的代码:
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, 255))
color_mask = cv2.bitwise_or(mask_white, mask_yellow)
mask_img[color_mask == 0] = [0,0,0]
# apply image thresholding
img = cv2.bitwise_and(mask_img[:,:,0], masked_img[:,:,0], mask=stencil)
ret, thresh = cv2.threshold(img, 130, 145, cv2.THRESH_BINARY)
# plot image
plt.figure(figsize=(10,10))
plt.imshow(thresh, cmap= "gray")
预期输出:
您可以使用 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)
结果:
然后你可以应用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)
结果: