首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >imshow的大小与ROI.shape不同

imshow的大小与ROI.shape不同
EN

Stack Overflow用户
提问于 2019-09-17 00:19:30
回答 1查看 572关注 0票数 1

晚上好!

我的问题如下:

1. roi的形状仅为200,200。这里显示的是313,313,我不知道为什么。

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

代码语言:javascript
复制
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 ...

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-17 01:43:52

你的roi规格是不正确的。应该是y1:y2,x1:x2,而不是y1:x1,y2:x2

在这里,我测试一个静态图像:

代码语言:javascript
复制
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)
代码语言:javascript
复制
 Prints:
代码语言:javascript
复制
78 178 103 153
(100, 50, 3)

结果:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57965833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档