Skip to content

Instantly share code, notes, and snippets.

@uiur
Created July 4, 2016 10:14
Show Gist options
  • Save uiur/15d513f866989a1d25afaf3064fa49b5 to your computer and use it in GitHub Desktop.
Save uiur/15d513f866989a1d25afaf3064fa49b5 to your computer and use it in GitHub Desktop.
extract regions and save by MSER
import sys
import numpy as np
import cv2
def extract_region(img, region):
top_left = np.min(region, axis=0)
bottom_right = np.max(region, axis=0)
margin = 2
region_of_image = img.copy()[top_left[1]-margin:bottom_right[1]+margin, top_left[0]-margin:bottom_right[0]+margin]
return region_of_image
mser = cv2.MSER(_min_area=10, _max_area=500)
img = cv2.imread(sys.argv[1])
blue, green, red = cv2.split(img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
vis = img.copy()
regions = mser.detect(gray)
i = 0
for region in regions:
roi = extract_region(img, region)
cv2.imwrite('tmp/%d.png' % (i), roi)
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment