Ask Your Question
1

[SOLVED]How to Save Each ROI Seperately

asked 2019-10-11 04:42:36 -0600

murkoc gravatar image

updated 2019-10-15 08:19:36 -0600

supra56 gravatar image

Hello,

I have below code:

import cv2
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

n_rows = 3
n_images_per_row = 3
while(True):

    # Capture frame-by-frame
    ret, frame = cap.read()
    height, width, ch = frame.shape
    roi_height = int(height / n_rows)
    roi_width = int(width / n_images_per_row)
    images = []
    for x in range(0, n_rows):
    for y in range(0,n_images_per_row):
    tmp_image=frame[x*roi_height:(x+1)*roi_height, y*roi_width:(y+1)*roi_width]
    images.append(tmp_image)
    # Display the resulting sub-frame
    for x in range(0, n_rows):
    for y in range(0, n_images_per_row):
    cv2.imshow(str(1+y+x*n_images_per_row), images[x*n_images_per_row+y])
    cv2.moveWindow(str(x * n_images_per_row + y + 1), 100 + (y * roi_width), 50 + (x * roi_height))

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

    cv2.imshow(cap.release())

cv2.destroyAllWindows()

I would like to save each frame seperately.

edit retag flag offensive close merge delete

Comments

sorry, but i could not fix the broken indentation ;(

berak gravatar imageberak ( 2019-10-11 04:55:53 -0600 )edit

just need to change the lines with the for loops.

murkoc gravatar imagemurkoc ( 2019-10-11 07:09:08 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
-1

answered 2019-10-14 07:41:13 -0600

nicolasabril gravatar image

If I understand your question correctly, you want to make a copy of the array slices, and not just have a view?

If so, replace tmp_image=frame[x*roi_height:(x+1)*roi_height, y*roi_width:(y+1)*roi_width] with tmp_image = np.copy(frame[x*roi_height:(x+1)*roi_height, y*roi_width:(y+1)*roi_width]) or something similar.

edit flag offensive delete link more
1

answered 2019-10-14 10:49:31 -0600

supra56 gravatar image

updated 2019-10-15 08:09:05 -0600

WOOOooow! I loved scribble puzzle. I solved your problem and save pics too. I added some enumerate to save pic.

import cv2
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

n_rows = 3
n_images_per_row = 3
while(True):

    # Capture frame-by-frame
    ret, frame = cap.read()
    height, width, ch = frame.shape
    roi_height = int(height / n_rows)
    roi_width = int(width / n_images_per_row)
    images = []
    for x in range(0, n_rows):
        for y in range(0,n_images_per_row):
            tmp_image=frame[x*roi_height:(x+1)*roi_height, y*roi_width:(y+1)*roi_width]
            images.append(tmp_image)
    # Display the resulting sub-frame
    for x in range(0, n_rows):
        for y in range(0, n_images_per_row):
            cv2.imshow(str(1+y+x*n_images_per_row), images[x*n_images_per_row+y])
            cv2.moveWindow(str(x * n_images_per_row + y + 1), 100 + (y * roi_width), 50 + (x * roi_height))            

    for i, face in enumerate(images):
            cv2.imwrite("{}.jpg".format(i), face)       

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
        cv2.imshow(cap.release())

cv2.destroyAllWindows()

Another thing is you don't needed another iterator. You can reduce from 35 to 30 lines.

import cv2
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

n_rows = 3
n_images_per_row = 3
while(True):

    # Capture frame-by-frame
    ret, frame = cap.read()
    height, width, ch = frame.shape
    roi_height = int(height / n_rows)
    roi_width = int(width / n_images_per_row)
    images = []
    for x in range(0, n_rows):
        for y in range(0,n_images_per_row):
            tmp_image=frame[x*roi_height:(x+1)*roi_height, y*roi_width:(y+1)*roi_width]
            images.append(tmp_image)
            cv2.imshow(str(1+y+x*n_images_per_row), images[x*n_images_per_row+y])
            cv2.moveWindow(str(x * n_images_per_row + y + 1), 100 + (y * roi_width), 50 + (x * roi_height)) 

    for i, face in enumerate(images):
            cv2.imwrite("{}.jpg".format(i), face)       

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
        cv2.imshow(cap.release())

cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

Enjoyed yours scribble puzzle.

supra56 gravatar imagesupra56 ( 2019-10-14 10:53:02 -0600 )edit
1

perfect thank you!!!

murkoc gravatar imagemurkoc ( 2019-10-15 01:25:47 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-10-11 04:42:36 -0600

Seen: 507 times

Last updated: Oct 15 '19