opencv-python给图片添加半透明mask
示例中,mask为矩形框,如果是不规则点的分割mask,可以使用cv2.fillPoly()函数代替cv2.rectangle(). 原始图片: import numpy as np import os import cv2 def put_mask ( img_path , output_fold ) : # 1.读取图片 image = cv2 . imread ( img_path ) # 2.获取标签 # 一般的标签格式 bbox = [x, y, w, h] bbox = [ 192 , 364 , 267 , 37 ] # 3.画出mask zeros = np . zeros ( ( image . shape ) ) bbox = [ int ( b ) for b in bbox ] bbox [ 2 ] = bbox [ 2 ] + bbox [ 0 ] bbox [ 3 ] = bbox [ 3 ] + bbox [ 1 ] zeros_mask = cv2 . rectangle ( zeros , ( bbox [ 0 ] , bbox [ 1 ] ) , ( bbox [ 2 ] , bbox [ 3 ] ) , color = ( 0 , 0 , 255 ) , thickness = - 1 ) #thickness=-1 表示矩形框内颜色填充 #