晚上!
我的问题如下:
1。roi
的形状仅应为200,200。这里显示313313,我不确定为什么。
2。当我将frame
的{{1}}切换为imshow()的参数时,结果也不是roi的正确大小。我得到的是以下输出:
roi
此文件的输出为:
from imutils.video import VideoStream
import argparse
import cv2
import numpy as np
import imutils
import time
ap = argparse.ArgumentParser(description="Pass the .mp4 file and optional buffer size as arguments.")
ap.add_argument("-video", type=str, help="Path to the video file") .
ap.add_argument("-buffer", type=int, default=64, help="Optional max buffer size")
args = vars(ap.parse_args())
video = cv2.VideoCapture(args["video"])
time.sleep(2.0)
offsetx = 38
offsety = 5
while (video.isOpened()):
ret, frame = video.read()
frame = imutils.resize(frame, width=1280)
(screenheight, screenwidth) = frame.shape[:2]
mid_y = screenheight/2
mid_x = screenwidth /2
roi_x = (mid_x + offsetx) - 100
roi_y = (mid_y + offsety) - 100
roi = frame[int(roi_y):int(roi_x),int(roi_y)+200:int(roi_x)+200]
cv2.rectangle(frame,(int(roi_x),int(roi_y)),(int(roi_x)+200,int(roi_y)+200),(0,255,0),3)
print(int(roi_x),int(roi_y),int(roi_x)+200,int(roi_y)+200)
print(roi.shape)
cv2.imshow('frame',roi)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()
答案 0 :(得分:1)
您的投资回报率规范不正确。应该是[y1:y2,x1:x2]而不是[y1:x1,y2:x2]
我在这里测试静态图像:
import cv2
img = cv2.imread('lena.jpg')
height = img.shape[0]
width = img.shape[1]
mid_y = height/2
mid_x = width/2
offset_y = -50
offset_x = -25
y1 = int(mid_y + offset_y)
x1 = int(mid_x + offset_x)
rows = 100
cols = 50
y2 = y1+rows
x2 = x1+cols
print(y1, y2, x1, x2)
roi = img[y1:y2, x1:x2]
cv2.rectangle(img, (x1,y1), (x2,y2), (0,255,0), 3)
print(roi.shape)
cv2.imshow("ROI", roi)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("lena_roi.jpg", roi)
打印:
78 178 103 153
(100, 50, 3)
结果: