imshow ROI 的大小不同于 ROI.shape

imshow ROI has different size than ROI.shape

大家晚上好!

我的问题如下:

1.The roi 的形状应该仅为 200,200。这里显示 313,313,我不确定为什么。

2.When 我将 frame 切换为 roi 作为 imshow() 的参数,结果也不是正确的 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()

这个文件的输出是:

(313, 313, 3) 578 265 778 465 ...

您的 roi 规格不正确。应该是 [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)

结果: