将图像存储在列表中

时间:2017-10-29 02:28:02

标签: python python-3.x opencv opencv3.0

我一直在尝试将裁剪后的图像从单帧存储到列表中,但我不断收到错误。这是我的代码片段

import cv2
import numpy as np

cap = cv2.VideoCapture('/home/wael-karkoub/Desktop/Research/Videos/test.mp4')
ret = True
cropped_images = []
while ret == True:
    ret, frame = cap.read()
    height, width, channels = frame.shape
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray,(5,5),0)
    _ ,thresh = cv2.threshold(blur[0:height//2],10,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    im2, contours, hierarchy = cv2.findContours(thresh,1,2)

    for cnt in contours:
        x,y,w,h = cv2.boundingRect(cnt)
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
        image = frame[y:y+h, x:x+w]
        cv2.imshow('test',image)
        time.sleep(2)

    # cv2.imshow('Contours',frame)

    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

cap.release()
cv2.destroyAllWindows()

这是正确的做法吗?

输出= enter image description here

1 个答案:

答案 0 :(得分:0)

如果要将裁剪的图像存储到列表中,则需要进行一些分配。只需在代码中的某处添加cropped_images.append(image),最有可能在image = frame[y:y+h, x:x+w]

之后