gpt4 book ai didi

python - 属性错误 : module 'cv2' has no attribute 'selectROI'

转载 作者:太空宇宙 更新时间:2023-11-03 23:07:09 26 4
gpt4 key购买 nike

我已经全新安装了 OpenCV 3.2,其中包含 contribs、ffmpeg 和 numpy。但是,当我尝试使用函数 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()

我只是希望我可以选择 ROI 并存储尺寸!

最佳答案

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

关于python - 属性错误 : module 'cv2' has no attribute 'selectROI' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56658331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com