0

I have written Python code that converts video to black and white mode. In the center of the video is a color ROI.

I don't understand how to write a loop that will make the ROI move (any kind of movement) through the video, so that the pixels in the region of interest are colored, and the rest is black and white. As I understand it, int data is needed for the loop. Therefore, I converted the width and height to int.

The available questions and examples on ROI bypass this topic.

Мy code:

import cv2
import numpy as np
import time

capture = cv2.VideoCapture('file path')
width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = capture.get(cv2.CAP_PROP_FPS)
print(fps, width, height)

file_count = 0
while capture.isOpened():
    ret, frame = capture.read()
    if ret == True:
        cv2.putText(frame, 'Doc.', (0, 340),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0))
        grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        ROI = frame[10:32, 10:40, :]
        gray2rgb = cv2.cvtColor(grayFrame, cv2.COLOR_GRAY2RGB)
        gray2rgb[10:32, 10:40, :] = ROI
    
        ##### In this place I have difficulties
        for i in range(height):
          for j in range(width - 300):
            gray2rgb[i, j + 10]
        ##### end of this cycle
    
        cv2.imshow('Look', gray2rgb)
        file_count += 1
        print('Shot {0:04d}'.format(file_count))
    
        key = cv2.waitKey(20)
        if (key == ord('q')) or key == 27:
            break

capture.release()
cv2.destroyAllWindows()
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • Shift in x direction: `ROI = frame[10:32, 10+x:40+x, :]` increment x for each new frame – fmw42 Aug 02 '22 at 16:15
  • When creating a loop, the ROI stays in place.When creating a loop, the ROI remains in place even though the program is running. `while capture.isOpened(): ret, frame = capture.read() x = 0 for i in range(width): ROI = frame[10:32, 10 + x:40 + x, :] x += 10 if ret == True: – Oleg Polyakov Aug 02 '22 at 18:00
  • Probably need to increment `gray2rgb[10:32, 10+x:40+x, :] = ROI` and put into loop.. Sorry, I have not run your code and do not follow exactly what you are trying to do. – fmw42 Aug 02 '22 at 18:29
  • You have not assigned the ROI. Change this `gray2rgb[i, j + 10]` to `gray2rgb[i, j + 10] = ROI` in your loop – fmw42 Aug 03 '22 at 00:20

1 Answers1

0

I found a solution without using the For cycle. Sorry, if I don't attach the code like that.

Мy code:

import cv2
import numpy as np
import time

capture = cv2.VideoCapture('C:\PY\l.mp4')
width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = capture.get(cv2.CAP_PROP_FPS)
print(fps, width, height)
frame_count = 0

##### Setting the initial coordinates of the zone
sq_y = 50
sq_x = 100
##### Adding a variable "speed" to "regulate" the speed of movement (optional)
speed = 5.5
##### Setting the offset length
sq_width = 100
sq_height = 50
##### Variable "reverse" for movement in the opposite direction
sq_revers_x = False
sq_revers_y = False

while capture.isOpened():
    ret, frame = capture.read()
    if ret == True:
        cv2.putText(frame, 'Doc.', (0, 340),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0))
        grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    ##### The condition for the "reverse" of the x
    if (not(sq_revers_x)):
        sq_x += round(4*speed)
    else:
        sq_x -= round(4*speed)
    #### The condition for "reverse" by y
    if (not(sq_revers_y)):
         sq_y += round(2*speed)
    else:
          sq_y -= round(2*speed)
    ##### The condition for enabling the reverse of the sqi
    if (sq_x+sq_width >= width or sq_x <= 0):
          sq_revers_x = not(sq_revers_x)
    ##### The condition for turning on the reverse by y
    if (sq_y+sq_height >= height or sq_y <= 0):
          sq_revers_y = not(sq_revers_y)
    ##### Setting the final coordinates of the zone
    sq_x_end = sq_x + sq_width
    sq_y_end = sq_y + sq_height

    ##### Specify the coordinates of the region of interest
    ROI = frame[sq_y:sq_y_end, sq_x:sq_x_end, :]

    gray2rgb = cv2.cvtColor(grayFrame, cv2.COLOR_GRAY2RGB)
    gray2rgb[sq_y:sq_y_end, sq_x:sq_x_end, :] = ROI
    cv2.imshow('Look', gray2rgb)

    frame_count += 1
    print('Shot {0:04d}'.format(frame_count))

    key = cv2.waitKey(round(1000/fps))
    if (key == ord('q')) or key == 27:
        break

capture.release()
cv2.destroyAllWindows()