|||
#import cv2 as cv #src=cv.imread('///Users/ln/Desktop/font.jpg') #cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE) #cv.imshow('input_image', src) #cv.waitKey(0) #cv.destroyAllWindows() import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread('///Users/ln/Desktop/ln2.jpg') OLD_IMG = img.copy() mask = np.zeros(img.shape[:2], np.uint8) SIZE = (1, 65) bgdModle = np.zeros(SIZE, np.float64) fgdModle = np.zeros(SIZE, np.float64) rect = (1, 1, img.shape[1], img.shape[0]) cv2.grabCut(img, mask, rect, bgdModle, fgdModle, 10, cv2.GC_INIT_WITH_RECT) mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8') img *= mask2[:, :, np.newaxis] plt.subplot(121), plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) plt.title("grabcut"), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(cv2.cvtColor(OLD_IMG, cv2.COLOR_BGR2RGB)) plt.title("original"), plt.xticks([]), plt.yticks([]) plt.show()
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-24 13:03
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社