Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- drawn = False
- startx, starty = -1, -1
- rectangle = (0, 0, 0, 0)
- def load_and_resize(path):
- image = cv2.imread(path)
- new_size = (700, 700)
- resized_image = cv2.resize(image, new_size, interpolation=cv2.INTER_AREA)
- return resized_image
- def select_roi(event, newx, newy, flags, params):
- global startx, starty, drawn, rectangle
- if event == cv2.EVENT_LBUTTONDOWN:
- startx, starty = newx, newy
- cv2.circle(image, (startx, starty), 4, (255, 255, 120), -1)
- elif event == cv2.EVENT_LBUTTONUP:
- drawn = True
- rectangle = (startx, starty, newx - startx, newy - starty)
- print("\nROI Selected Successfully")
- def extract_foreground(image):
- global drawn
- cv2.namedWindow(winname='BG Subractor')
- cv2.setMouseCallback('BG Subractor', select_roi)
- print("\nSelect ROI from mouse pointer.")
- black_mask = np.zeros(image.shape[:2], np.uint8)
- background = np.zeros((1, 65), np.float64)
- foreground = np.zeros((1, 65), np.float64)
- while True:
- if drawn:
- print("\nPerforming Background Subtraction")
- cv2.grabCut(image, black_mask, rectangle,background, foreground,5, cv2.GC_INIT_WITH_RECT)
- #cv2.imshow('Image before multipllication',image)
- mask2 = np.where((black_mask == 2) | (black_mask == 0), 0, 1).astype('uint8')
- cv2.imshow('Mask', mask2)
- image = image * mask2[:, :, np.newaxis]
- drawn = False
- print("\nExtraction complete")
- cv2.imshow('BG Subractor', image)
- if cv2.waitKey(1) & 0xFF == 27:
- break
- cv2.destroyAllWindows()
- image = load_and_resize('img.png')
- extract_foreground(image)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement