Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Extract detected objects and save to different images - OpenCV Python

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

click to hide/show revision 2
None

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

berak gravatar image

Extract detected objects and save to different images - OpenCV Python

Hello,

I have this image: C:\fakepath\botoes.PNGC:\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

cv2

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

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)

cv2.COLOR_BGR2GRAY)

regions, _ = mser.detectRegions(gray)

mser.detectRegions(gray)

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

0)) cv2.imshow('img', vis)

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])

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