Ask Your Question
0

ROI region opencv c++ vs python

asked 2019-01-01 17:22:58 -0600

Sky31 gravatar image

updated 2019-01-01 18:07:17 -0600

Define ROI:

Mat roi(image,Rect(corners[0].x,corners[0].y,s,s));
if(roi.rows<1||roi.cols<1)
        return empty;

I need to find ROI using above code

This is what I'm trying to do in python

 x,y,w,h= [corners[0][0], corners[0][1],s,s]
   roi = image[y:y+h, x:x+w]

(the values of x,y,w,h) is ((1161.4393398282202, -1.0606601717798214, 2.121320343559643, 2.121320343559643) but getting error "TypeError: slice indices must be integers or None or have an __index__ method" Then I convert the value of x,y,w,h into int but doesn't work

I also tried to use cv2.selectROI(winsize,image) winsize = x,y,w,h but still dosent work getting error "TypeError: only size-1 arrays can be converted to Python scalars"

please tell me what should I do.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-01-02 01:33:26 -0600

berak gravatar image

updated 2019-01-02 01:37:51 -0600

TypeError: slice indices must be integers

indeed, numpy /python3 is quite picky about it. if your values are float, you need to cast those manually, like:

x,y,w,h= [ int(corners[0][0]), int(corners[0][1]), int(s), int(s) ]
roi = image[y:y+h, x:x+w]

also, be careful here. if your y value is -1.06, it will silently be cropped to 0 ! (while the c++ version wuld throw an exception)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-01-01 17:22:58 -0600

Seen: 1,681 times

Last updated: Jan 02 '19