gpt4 book ai didi

python - imshow ROI 的大小与 ROI.shape 不同

转载 作者:太空宇宙 更新时间:2023-11-03 22:36:25 27 4
gpt4 key购买 nike

晚上好!

我的问题如下:

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

2.当我将 roiframe 切换为 imshow() 的参数时,结果也不是正确的 roi 大小。我得到的是这个输出:

enter image description here

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]

我在这里测试静态图像:

enter image description here

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)

结果:

enter image description here

关于python - imshow ROI 的大小与 ROI.shape 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57965833/

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