AttributeError:模块“ cv2”没有属性“ selectROI”

时间:2019-06-18 23:37:23

标签: python opencv opencv3.0 roi

我已经重新安装了带有contribs,ffmpeg和numpy的OpenCV 3.2。但是,当我尝试使用功能selectROI时,出现属性错误,我无法弄清原因!

我试图重新安装opencv和opencv-contrib,但是似乎没有任何改变。

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

我只是希望我可以选择一个投资回报率并存储尺寸!

2 个答案:

答案 0 :(得分:0)

enter image description here

您始终可以创建自己的自定义ROI选择器

""

答案 1 :(得分:0)

对Nathancy的回应的改编对我更有效,

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