Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: module 'cv2' has no attribute 'selectROI'

I've got a fresh install of OpenCV 3.2 with contribs, ffmpeg, and numpy. However, when I try to use the function selectROI I get an attribute error and I cannot figure out why!!!

I've tried to reinstall opencv and opencv-contrib however it doesn't seem to change anything.

import numpy as np
import ffmpy
import cv2
import os

def main():
   ...
            r=0
            cap = cv2.VideoCapture(filename)
           ...
            while cap.grab():
               ...
                if (frame_count>=next_valid):
                    # Initialisation of supporting variables
                    flag, frame = cap.retrieve()
                    if (go_around==0):
                        # Select ROI
                        r = cv2.selectROI(frame)
                    # Cropping and Brightening
                    imCrop = im[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
                   ...
main()

I just wish I could make a selectable ROI and store the dimensions!

like image 798
Federico Tondolo Avatar asked Feb 06 '26 14:02

Federico Tondolo


1 Answers

An adaptation of nathancy's response that worked better for me,

class SelectROI(object):
    def __init__(self, name, im):
        self.image = im
        self.winname = name

        cv2.namedWindow(name)
        self.coords = []
        self.dragging = False
        self._update()

    def _mouse_cb(self, event, x, y, flags, parameters):
        # Record starting (x,y) coordinates on left mouse button click
        if event == cv2.EVENT_LBUTTONDOWN:
            self.coords[:] = [(x, y)]
            self.dragging = True

        elif event == 0 and self.dragging:
            self.coords[1:] = [(x, y)]

        # Record ending (x,y) coordintes on left mouse bottom release
        elif event == cv2.EVENT_LBUTTONUP:
            self.coords[1:] = [(x, y)]
            self.dragging = False
            xs, ys = list(zip(*self.coords))
            self.coords = [(min(xs), min(ys)),
                           (max(xs), max(ys))]
            print('roi:', self.coords)

        # Clear drawing boxes on right mouse button click
        elif event == cv2.EVENT_RBUTTONDOWN:
            self.coords = []
            self.dragging = False

        self._update()

    def _update(self):
        im = self.image.copy()
        if len(self.coords) == 2:
            cv2.rectangle(im, self.coords[0], self.coords[1], (0, 255, 0), 2)
        cv2.imshow(self.winname, im)

    def __call__(self):
        cv2.setMouseCallback(self.winname, self._mouse_cb)
        cv2.waitKey()
        cv2.destroyWindow(self.winname)
        return self.coords if self.coords else None

def select_roi(name, im):
    s=SelectROI(name, im)
    return s()
like image 70
lericson Avatar answered Feb 08 '26 04:02

lericson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!