Ask Your Question
0

How to get region of image (ROI) of an object?

asked 2017-12-10 16:32:57 -0600

Ansem gravatar image

hello, my question is, if i use template matching and the image of interest is detected (or use a color filter and detect a red object for example), how can i get/obtain the region or ubication of the image?, for example img[280:340, 330:390].

pd: im using python 3 with opencv in a raspberry pi 3 model b

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-12-11 06:26:47 -0600

supra56 gravatar image

OpenCV 3.3.1, raspberry pi3 model b.

import cv2
image = cv2.imread("beatles.jpg")
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
edged = cv2.Canny(image, 10, 250)
(ret, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
idx = 0
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    if w>50 and h>50:
        idx+=1
        new_img=image[y:y+h,x:x+w]
        cv2.imwrite(str(idx) + '.png', new_img)
cv2.imshow("im",image)
cv2.waitKey(0)

I do have another roi using mouseevent function

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2017-12-10 16:32:57 -0600

Seen: 3,273 times

Last updated: Dec 11 '17