Extract detected objects and save to different images - OpenCV Python [closed]

asked 2018-04-22 13:36:22 -0600

maia07 gravatar image

updated 2018-04-22 14:02:22 -0600

berak gravatar image

Hello,

I have this image: C:\fakepath\botoes.PNG and i want to extract all the buttons and save them in different imagens. Untill now i have this code:

import numpy as np
import cv2

img = cv2.imread('C:\\Users\\Rita\\Desktop\\ISCTE\\2_ano\\Tese\MSER\\1_Exemplo\\botoes.PNG',1)

vis = img.copy()
mser = cv2.MSER_create()
vis = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

regions, _ = mser.detectRegions(gray)

hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
cv2.polylines(vis, hulls, 1, (0, 255, 0))

cv2.imshow('img', vis)

for i, contour in enumerate(hulls):
    x,y,w,h = cv2.boundingRect(contour)
    cv2.imwrite('1_exemplo_{}.png'.format(i), img[y:y+h,x:x+w])

But it's not separate in the correct way. Does anybody know what i'm missing here in the code? Or what it's the best way to do it?

Thanks a lot.

Rita Maia

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-11-09 09:37:39.965989