Motion detection by means of ROI grid

Hello forum, this is an easy one for experienced opencv users.
My project is a REM sleep detector. The picture provided shows the contour of my eyelid, which moved in a distinct way as my eye moves underneath it.
To capture the movement, I have drawn a grid of ROIs on the video stream, and I would like to use the countnonzero - or blob detection functions on them. As values change in a specific ROI, motion is detected as it were. I am sure there is a better way to do this, as for now this is the first method I could apply.
Problem here: my countnonzero or blob function will not use the ROI as a source but always only the entire image. How to specify an ROI of my choice as the source? ROIs are specified by means of a simple rectangle function.
Any help is appreciated, thank you

please show some code. relevant is where and how you define your ROIs, and where you count non-zeros.

related: python - How to use opencv functions on roi - Stack Overflow

Hello crackwitz, I like your name btw, roi is set up like so.
The printed result is the same whether I print nonzeros for img or for an roi, this is what leads me to think that the function does not actually draw from the roi that I entered. By all means, if you have a better solution for the problem, fire away. thanks for your help and have a good day

import cv2 as cv
cap = cv.VideoCapture(0) 
while (True):
    ret, frame =
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    ret, img = cv.threshold(gray, 50, 255, cv.THRESH_BINARY)

ul11 = (50, 230)
ul12 = (50, 270)
ul13 = (50, 310)
ul14 = (50, 350)
br11 = (120, 270)
br12 = (120, 310)
br13 = (120, 350)
br14 = (120, 390)
br15 = (120, 430)
...                 #roi in 8 columns and 5 rows
roi11 = cv.rectangle(img, ul11, br11, (100, 50, 20), 1)
roi12 = cv.rectangle(img, ul12, br12, (100, 50, 20), 1)
roi13 = cv.rectangle(img, ul13, br13, (100, 50, 20), 1)
roi14 = cv.rectangle(img, ul14, br14, (100, 50, 20), 1)

px11 = cv.countNonZero(roi11)
px12 = cv.countNonZero(roi12)
cv.imshow('window', img)

    if cv.waitKey(200) & 0xFF == ord('p'):

that is not how to do ROIs.

cv.rectangle call is a drawing call. it paints into the given picture (the picture is changed). it just happens to also return that same object (np.array).

to take a subregion of an image, use slicing.

roi11 = img[230:270, 50:120] # rows (y), then columns (x)

this information was already given as a comment to your Stack Overflow post.

1 Like