Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在何处添加cv2.resize以获得适当的分辨率?_Python_Opencv_Resolution - Fatal编程技术网

Python 在何处添加cv2.resize以获得适当的分辨率?

Python 在何处添加cv2.resize以获得适当的分辨率?,python,opencv,resolution,Python,Opencv,Resolution,我的代码有问题。我不知道在哪里可以放置cv.resize(),以获得需求解析。我想改变它,因为我上传的文件是全高清分辨率,我想得到更小的分辨率。我很乐意解答和解释 下面我展示我的代码: import cv2 import numpy as np cap = cv2.VideoCapture('DJI_0037.MP4') while cap.isOpened(): ret, frame = cap.read() if ret == True: frame_

我的代码有问题。我不知道在哪里可以放置
cv.resize()
,以获得需求解析。我想改变它,因为我上传的文件是全高清分辨率,我想得到更小的分辨率。我很乐意解答和解释

下面我展示我的代码:

import cv2
import numpy as np

cap = cv2.VideoCapture('DJI_0037.MP4')

while cap.isOpened():

    ret, frame = cap.read()

    if ret == True:
        frame_resize = cv2.resize(frame, (640, 480), interpolation=cv2.INTER_CUBIC)
    else:
        break

    ret, frame_resize1 = cap.read(frame_resize)
    ret, frame_resize2 = cap.read(frame_resize)  

    diff = cv2.absdiff(frame_resize1, frame_resize2) 
    gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY) 
    blur = cv2.GaussianBlur(gray, (5, 5), 0)  
    _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
    dilated = cv2.dilate(thresh, None, iterations=3)
    contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    for contour in contours: 
        (x, y, w, h) = cv2.boundingRect(contour)  
        if cv2.contourArea(contour) < 2000:
            continue
        cv2.rectangle(frame_resize1, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.putText(frame_resize1, "Status: {}".format('Movement'), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255),3) 



    cv2.imshow("feed", frame_resize1)
    frame_resize1 = frame_resize2
    ret, frame_resize2 = cap.read()

    if cv2.waitKey(40) == 27:
        break

cap.release()
cv2.destroyAllWindows()

导入cv2
将numpy作为np导入
cap=cv2.VideoCapture('DJI_0037.MP4')
而cap.isOpened():
ret,frame=cap.read()
如果ret==True:
frame_resize=cv2.resize(frame,(640480),插值=cv2.INTER_CUBIC)
其他:
打破
ret,frame_resize1=cap.read(frame_resize)
ret,frame_resize2=cap.read(frame_resize)
diff=cv2.absdiff(帧大小1,帧大小2)
灰色=cv2.CVT颜色(差异,cv2.COLOR\u BGR2GRAY)
模糊=cv2.高斯模糊(灰色,(5,5,0)
_,thresh=cv2.threshold(模糊,20255,cv2.thresh_二进制)
扩张=cv2。扩张(阈值,无,迭代次数=3)
等高线,u=cv2.找到的轮廓(放大,cv2.RETR\u树,cv2.链近似\u简单)
对于等高线中的等高线:
(x,y,w,h)=cv2.边界矩形(轮廓)
如果cv2.轮廓面积(轮廓)<2000:
持续
cv2.矩形(框大小1,(x,y),(x+w,y+h),(0,255,0),2)
putText(frame_resize1,“Status:{}.”格式('Movement'),(10,20),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),3)
cv2.imshow(“馈送”,帧大小1)
帧大小1=帧大小2
ret,frame_resize2=cap.read()
如果cv2.waitKey(40)=27:
打破
第1章释放()
cv2.destroyAllWindows()

在absdiff(两个帧)之前或imshow之前。取决于您想要做什么(节省处理时间和降低质量或只是更好地显示),非常感谢您的简单而巧妙的解释。现在,它工作得很好