Ask Your Question

Why does cv2.selectROI in python is not working as expected?

asked 2017-10-25 10:39:46 -0500

HotSince7cc gravatar image

updated 2017-10-25 11:47:27 -0500

berak gravatar image

Hi, I'm using OpenCV 3.3.0-dev with Python 3.5
I'm trying to make use of the function with the following example:

import cv2
img = cv2.imread('starwars.jpg')

r = cv2.selectROI("Image", img, False, False)

cv2.imshow('image', r)


It gives me some irrelevant white space image(It doesn't let me upload a screenshot here)
The output is this thing -> (138, 165, 263, 224) when you print it on the console

Do I need the contrib modules for Python or? The cv.cv2.selectROI method is stated in the High-level GUI module.

edit retag flag offensive close merge delete


(It doesn't let me upload a screenshot here)

if it was a karma problem, consider it fixed. please try again.

berak gravatar imageberak ( 2017-10-25 10:45:36 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2017-10-25 11:06:52 -0500

berak gravatar image

updated 2017-10-26 03:36:50 -0500

selectROI returns a rectangle, not a cropped image. (expectation mismatch)

>>> help(cv2.selectROI)
Help on built-in function selectROI:

    selectROI(windowName, img[, showCrosshair[, fromCenter]]) -> retval
    .   @brief Selects ROI on the given image.
    .   Function creates a window and allows user to select a ROI using mouse.
    .   Controls: use `space` or `enter` to finish selection, use key `c` to cancel selection (function will return the zero cv::Rect).
    .   @Param windowName name of the window where selection process will be shown.
    .   @Param img image to select a ROI.
    .   @Param showCrosshair if true crosshair of selection rectangle will be shown.
    .   @Param fromCenter if true center of selection will match initial mouse position. In opposite case a corner of
    .   selection rectangle will correspont to the initial mouse position.
    .   @return selected ROI or empty rect if selection canceled.
    .   @note The function sets it's own mouse callback for specified window using cv::setMouseCallback(windowName, ...).
    .   After finish of work an empty callback will be set for the used window.

    selectROI(img[, showCrosshair[, fromCenter]]) -> retval
    .   @overload

what you probably wanted is (wild guess):

(x,y,w,h) = cv2.selectROI(winname, img)
cropped = img[y:y+h , x:x+w] # both opencv and numpy are "row-major", so y goes first
edit flag offensive delete link more



Thanks, this is what I need. If you can just edit:
cropped = img[y,y+h : x,x+w] -> to cropped = img[y: y + h, x: x + w]

HotSince7cc gravatar imageHotSince7cc ( 2017-10-26 03:13:45 -0500 )edit

uuaa, ofc. ;)

berak gravatar imageberak ( 2017-10-26 03:36:10 -0500 )edit

answered 2017-10-25 22:08:29 -0500

supra56 gravatar image

updated 2017-10-25 22:09:59 -0500

This will worked for me. I'm using raspberry pi 3, OpenCV 3.3.0 and python 3.5.

import cv2

if __name__ == '__main__' :

# Read image
img = cv2.imread("starwars.jpg")

# Select ROI
r = cv2.selectROI("Image", img, False, False)

# Crop image
imCrop = img[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]

# Display cropped image
cv2.imshow("Image", imCrop)
edit flag offensive delete link more


just curious, why did it need the int() conversion ?

berak gravatar imageberak ( 2017-10-26 02:50:17 -0500 )edit

I dunno why. When I tried animation and games when using selectROI, then I got error. That why I used int conversion.

You can try without int conversion.

supra56 gravatar imagesupra56 ( 2017-10-26 04:47:38 -0500 )edit

Question Tools

1 follower


Asked: 2017-10-25 10:39:46 -0500

Seen: 21,030 times

Last updated: Oct 26 '17