Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

Obtain region of interest with Python / OpenCV

I am working on a video.mp4 file and I would like to apply some conditions for half of the frame and other conditions for the other half. One of the frames is as shown:

1 frame

The idea was to process a region of interest from y[0:500] and all x-axis (i.e: x[0:1920]) being one region and the other region from y[500:1080] and x[0:1920].

I tried to set one region of interest this way:

import cv2

#LOAD VIDEO PATH
vid = cv2.VideoCapture('video.mp4')
ret, frame = vid.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
print(gray[:,0].shape)
print(gray[0,:].shape)

for x in len(range(gray[0:1920])):
    for y in len(range(gray[0:500])):
        sub_image[x,y] = gray

plt.figure(figsize=(14, 7))
plt.imshow(sub_image)

But I kept on getting the error:

error: OpenCV(4.4.0) C:UsersappveyorAppDataLocalTemp1pip-req-build-6sxsq0tpopencvmodulesimgprocsrccolor.cpp:182:
error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' on line 5 (ie converting BGR TO GRAY) 

Any ideas on how I can proceed?

question from:https://stackoverflow.com/questions/65863952/obtain-region-of-interest-with-python-opencv

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
import cv2
vid = cv2.VideoCapture('video.mp4')
ret, frame = vid.read()

h,w,d=frame.shape
h2=int(h/2)
w2=int(w/2)

roi_up=frame[0:h2,:]
roi_dw=frame[h2:,:]

roi_up = 255 - roi_up

cv2.imshow('a',roi_up)
cv2.imshow('b',roi_down)
cv2.imshow('c',frame)
cv2.waitkey(0)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...