将图像上的白色变为红色

问题描述 投票:0回答:0

我认为我的代码运行良好。 首先我想搜索white lane 第二个将白色车道更改为任何颜色 最后添加原始图像和遮罩 我的代码有什么问题?

将 cv2 导入为 cv

将 numpy 导入为 np

进口操作系统

folder_path='test_run/' new_folder_path='after_delete_lane_disturbance/'

image_paths = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith('.png')]

def regoin_of_interest(img、顶点、color3=(255,255,255)、color1=255):

掩码=np.zeros_like(img)

if len(img.shape)>2:
    color=color3
else:
    color=color1
cv.fillPoly(mask,vertices,color)
ROI_image=cv.bitwise_and(img,mask)
return ROI_image

def mark_img(图像,blue_threshold=200,green_threshold=200,red_threshold=200):

bgr_threshold = [blue_threshold, green_threshold, red_threshold]


thresholds = (image[:,:,0] < bgr_threshold[0]) \
            | (image[:,:,1] < bgr_threshold[1]) \
            | (image[:,:,2] < bgr_threshold[2])
mark[thresholds] = [0,0,0]
return mark

对于 image_paths 中的 image_path: image=cv.imread(图像路径) 高度,宽度,_=image.shape

vertices = np.array([[(50,height),(width/2-45, height/2+60), (width/2+45, height/2+60), (width-50,height)]], dtype=np.int32)
roi_img=regoin_of_interest(image,vertices,(0,0,255))

mark=np.copy(roi_img)
mark=mark_img(roi_img)
color_thresholds = (mark[:,:,0] == 0) & (mark[:,:,1] == 0) & (mark[:,:,2] > 200)
image[color_thresholds] = [0,0,255]

file_name=os.path.basename(image_path)
new_file_name=os.path.splitext(file_name)[0]+' after.png'
save_path=os.path.join(new_folder_path,new_file_name)
cv.imwrite(save_path,image)

请告诉我 我不知道

python opencv
© www.soinside.com 2019 - 2024. All rights reserved.