我一直試圖將裁剪後的圖像從單幀存儲到列表中,但我不斷收到錯誤。這是我的代碼片段將圖像存儲在列表中
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
你得到了什麼樣的錯誤?在這段代碼中,它看起來並不像你實際上曾經存儲過任何圖像。 –
@Caleb_McCreary嘿,我剛剛更新了代碼到我的完整代碼 –
你能打印圖像的大小控制檯? – Micka