opencv-python图像剪切(剪切不规则区域)

直接看代码吧

import cv2;
import numpy as np


img = cv2.imread('D:\\shi\\a.png');
rows=img.shape[0]
cols=img.shape[1]
channels=img.shape[2]
mask=np.zeros(img.shape,dtype=np.uint8)
#输入点的坐标
roi_corners=np.array([[(10,10),(40,20),(70,80),(5,100)]],dtype=np.int32)
channel_count=channels
ignore_mask_color = (255,)*channel_count
#创建mask层
cv2.fillPoly(mask,roi_corners,ignore_mask_color)
#为每个像素进行与操作,除mask区域外,全为0
masked_image=cv2.bitwise_and(img,mask)
cv2.imshow("src",masked_image)
cv2.waitKey(0)

你可能感兴趣的:(opencv,python)