在网络摄像头视频上创建多个投资回报率
Creating multiple ROI on webcam video
我正在尝试通过我的网络摄像头流创建多个 ROI。我使用了这个名为 roipoly (https://github.com/jdoepfert/roipoly.py)
的库
我修改了示例代码 (examples/multi_roi_example.py) 以在网络摄像头上工作,现在看起来像这样
import logging
import cv2
import numpy as np
from matplotlib import pyplot as plt
from roipoly import MultiRoi
logging.basicConfig(format='%(levelname)s ''%(processName)-10s : %(asctime)s '
'%(module)s.%(funcName)s:%(lineno)s %(message)s',
level=logging.INFO)
cap = cv2.VideoCapture(1)
while cap.isOpened():
# Create image
val,img =cap.read()
#img = np.ones((100, 100)) * range(0, 100)
# Show the image
fig = plt.figure()
plt.imshow(img, interpolation='nearest', cmap="Greys")
plt.title("Click on the button to add a new ROI")
# Draw multiple ROIs
multiroi_named = MultiRoi(roi_names=['My first ROI', 'My second ROI'])
# Draw all ROIs
plt.imshow(img, interpolation='nearest', cmap="Greys")
roi_names = []
for name, roi in multiroi_named.rois.items():
roi.display_roi()
roi.display_mean(img)
roi_names.append(name)
plt.legend(roi_names, bbox_to_anchor=(1.2, 1.05))
plt.show()
# Let OpenCV manage window events
key = cv2.waitKey(50)
# If ESCAPE key pressed, stop
if key == 27:
cap.release()
但问题是在绘制 ROI 后,视频流保持暂停状态,永远不会继续。如何恢复网络摄像头流及其之上的 ROI
我发现问题很少。
主要问题是 plt.show()
一直等到您关闭 window,这会阻塞其余代码。
它需要 plt.show(block=False)
,这也需要 plt.pause(0.001)
,所以它有时间在 window 中更新图像。
plt.show(block=False)
plt.pause(0.001)
另一个问题是你在每个循环中 运行 ROI - 所以你必须在 selecting
之后使用一些布尔变量来跳过它
select_roi = True
while cap.isOpened():
# ... code ...
if select_roi:
fig = plt.figure()
plt.imshow(img, interpolation='nearest', cmap="Greys")
plt.title("Click on the button to add a new ROI")
# Draw multiple ROIs
multiroi_named = MultiRoi(roi_names=['My first ROI', 'My second ROI'])
select_roi = False
# ... code ...
或者您应该 select 循环前的投资回报率
fig = plt.figure()
plt.imshow(img, interpolation='nearest', cmap="Greys")
plt.title("Click on the button to add a new ROI")
# Draw multiple ROIs
multiroi_named = MultiRoi(roi_names=['My first ROI', 'My second ROI'])
while cap.isOpened():
# ... code ...
另一个问题是 waitKey
,只有当您使用 cv2.imshow(...)
显示图像时才有效,但您不使用它,因此 waitKey
将无法获取密钥。
另一个问题是 cv2 给出颜色图像 BGR
但 matplotlib
需要 RGB
- 所以你必须转换它。
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
完整的工作代码:
带布尔变量的版本 select_roi
import cv2
from matplotlib import pyplot as plt
from roipoly import MultiRoi
#cap = cv2.VideoCapture(0) # my built-in webcam
cap = cv2.VideoCapture(1)
select_roi = True # default value as start
while cap.isOpened():
val, img = cap.read()
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
if select_roi:
# Show the image
fig = plt.figure()
plt.imshow(img, interpolation='nearest', cmap="Greys")
plt.title("Click on the button to add a new ROI")
# Draw multiple ROIs
multiroi_named = MultiRoi(roi_names=['My first ROI', 'My second ROI'])
select_roi = False
# --- after if ---
# Draw all ROIs
plt.imshow(img, interpolation='nearest', cmap="Greys")
roi_names = []
for name, roi in multiroi_named.rois.items():
roi.display_roi()
#roi.display_mean(img)
roi_names.append(name)
plt.legend(roi_names, bbox_to_anchor=(1.2, 1.05))
plt.show(block=False)
plt.pause(0.001)
循环前有 ROI 的版本
import cv2
from matplotlib import pyplot as plt
from roipoly import MultiRoi
#cap = cv2.VideoCapture(0) # my built-in webcam
cap = cv2.VideoCapture(1)
val, img = cap.read()
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Show the image
fig = plt.figure()
plt.imshow(img, interpolation='nearest', cmap="Greys")
plt.title("Click on the button to add a new ROI")
# Draw multiple ROIs
multiroi_named = MultiRoi(roi_names=['My first ROI', 'My second ROI'])
while cap.isOpened():
val, img = cap.read()
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Draw all ROIs
plt.imshow(img, interpolation='nearest', cmap="Greys")
roi_names = []
for name, roi in multiroi_named.rois.items():
roi.display_roi()
#roi.display_mean(img)
roi_names.append(name)
plt.legend(roi_names, bbox_to_anchor=(1.2, 1.05))
plt.show(block=False)
plt.pause(0.001)
顺便说一句: 如果你需要矩形 ROI 那么有 cv2.selectROI()
到 select 一个区域和 cv2.cv2.selectROIs()
到 select许多地区
results = cv2.selectROIs('image', img)
并且它不需要从 BGR
转换为 RGB
。
我正在尝试通过我的网络摄像头流创建多个 ROI。我使用了这个名为 roipoly (https://github.com/jdoepfert/roipoly.py)
的库我修改了示例代码 (examples/multi_roi_example.py) 以在网络摄像头上工作,现在看起来像这样
import logging
import cv2
import numpy as np
from matplotlib import pyplot as plt
from roipoly import MultiRoi
logging.basicConfig(format='%(levelname)s ''%(processName)-10s : %(asctime)s '
'%(module)s.%(funcName)s:%(lineno)s %(message)s',
level=logging.INFO)
cap = cv2.VideoCapture(1)
while cap.isOpened():
# Create image
val,img =cap.read()
#img = np.ones((100, 100)) * range(0, 100)
# Show the image
fig = plt.figure()
plt.imshow(img, interpolation='nearest', cmap="Greys")
plt.title("Click on the button to add a new ROI")
# Draw multiple ROIs
multiroi_named = MultiRoi(roi_names=['My first ROI', 'My second ROI'])
# Draw all ROIs
plt.imshow(img, interpolation='nearest', cmap="Greys")
roi_names = []
for name, roi in multiroi_named.rois.items():
roi.display_roi()
roi.display_mean(img)
roi_names.append(name)
plt.legend(roi_names, bbox_to_anchor=(1.2, 1.05))
plt.show()
# Let OpenCV manage window events
key = cv2.waitKey(50)
# If ESCAPE key pressed, stop
if key == 27:
cap.release()
但问题是在绘制 ROI 后,视频流保持暂停状态,永远不会继续。如何恢复网络摄像头流及其之上的 ROI
我发现问题很少。
主要问题是 plt.show()
一直等到您关闭 window,这会阻塞其余代码。
它需要 plt.show(block=False)
,这也需要 plt.pause(0.001)
,所以它有时间在 window 中更新图像。
plt.show(block=False)
plt.pause(0.001)
另一个问题是你在每个循环中 运行 ROI - 所以你必须在 selecting
之后使用一些布尔变量来跳过它select_roi = True
while cap.isOpened():
# ... code ...
if select_roi:
fig = plt.figure()
plt.imshow(img, interpolation='nearest', cmap="Greys")
plt.title("Click on the button to add a new ROI")
# Draw multiple ROIs
multiroi_named = MultiRoi(roi_names=['My first ROI', 'My second ROI'])
select_roi = False
# ... code ...
或者您应该 select 循环前的投资回报率
fig = plt.figure()
plt.imshow(img, interpolation='nearest', cmap="Greys")
plt.title("Click on the button to add a new ROI")
# Draw multiple ROIs
multiroi_named = MultiRoi(roi_names=['My first ROI', 'My second ROI'])
while cap.isOpened():
# ... code ...
另一个问题是 waitKey
,只有当您使用 cv2.imshow(...)
显示图像时才有效,但您不使用它,因此 waitKey
将无法获取密钥。
另一个问题是 cv2 给出颜色图像 BGR
但 matplotlib
需要 RGB
- 所以你必须转换它。
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
完整的工作代码:
带布尔变量的版本 select_roi
import cv2
from matplotlib import pyplot as plt
from roipoly import MultiRoi
#cap = cv2.VideoCapture(0) # my built-in webcam
cap = cv2.VideoCapture(1)
select_roi = True # default value as start
while cap.isOpened():
val, img = cap.read()
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
if select_roi:
# Show the image
fig = plt.figure()
plt.imshow(img, interpolation='nearest', cmap="Greys")
plt.title("Click on the button to add a new ROI")
# Draw multiple ROIs
multiroi_named = MultiRoi(roi_names=['My first ROI', 'My second ROI'])
select_roi = False
# --- after if ---
# Draw all ROIs
plt.imshow(img, interpolation='nearest', cmap="Greys")
roi_names = []
for name, roi in multiroi_named.rois.items():
roi.display_roi()
#roi.display_mean(img)
roi_names.append(name)
plt.legend(roi_names, bbox_to_anchor=(1.2, 1.05))
plt.show(block=False)
plt.pause(0.001)
循环前有 ROI 的版本
import cv2
from matplotlib import pyplot as plt
from roipoly import MultiRoi
#cap = cv2.VideoCapture(0) # my built-in webcam
cap = cv2.VideoCapture(1)
val, img = cap.read()
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Show the image
fig = plt.figure()
plt.imshow(img, interpolation='nearest', cmap="Greys")
plt.title("Click on the button to add a new ROI")
# Draw multiple ROIs
multiroi_named = MultiRoi(roi_names=['My first ROI', 'My second ROI'])
while cap.isOpened():
val, img = cap.read()
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Draw all ROIs
plt.imshow(img, interpolation='nearest', cmap="Greys")
roi_names = []
for name, roi in multiroi_named.rois.items():
roi.display_roi()
#roi.display_mean(img)
roi_names.append(name)
plt.legend(roi_names, bbox_to_anchor=(1.2, 1.05))
plt.show(block=False)
plt.pause(0.001)
顺便说一句: 如果你需要矩形 ROI 那么有 cv2.selectROI()
到 select 一个区域和 cv2.cv2.selectROIs()
到 select许多地区
results = cv2.selectROIs('image', img)
并且它不需要从 BGR
转换为 RGB
。