本文共 850 字,大约阅读时间需要 2 分钟。
#!/usr/bin/env python# -*- coding: utf-8 -*-import numpy as npimport cv2frame=cv2.imread(r0.jpg")# setup initial location of windowwhile 1:    frame = cv2.imread(r"0.jpg")    roi = cv2.selectROI(windowName="roi", img=frame, showCrosshair=True, fromCenter=False)    x, y, w, h = roi    print(w,h)    img = frame[y:y + h, x:x + w]    cv2.imshow('a', img)    height, width = img.shape[:2]    mask = np.zeros(img.shape[:2],np.uint8)    # 背景模型    bgdModel = np.zeros((1,65),np.float64)    # 前景模型    fgdModel = np.zeros((1,65),np.float64)    rect = (w//20, h//20, width -w//10, height -  h//10)    # 使用grabCut算法    cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)    mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')    img = img*mask2[:,:,np.newaxis]    cv2.imshow('image', img)    k = cv2.waitKey(0)    cv2.destroyAllWindows()   
转载地址:http://hmik.baihongyu.com/