How do you show only half the content the camera picks up?

Asked

Viewed 33 times

-1

I wonder how to show the content captured via webcam (by videoCapture) in a window that only shows half of what the camera picks up, In opencv.

1 answer

1

Just get the image size using the attribute shape and then crop the image using slice (your image is an object of numpy.ndarray, which is a kind of list). See the example below:

import cv2
camera = cv2.VideoCapture(0)
r, frame = camera.read()

height, width = frame.shape[0:2]  # Obtém altura e largura da imagem
height //= 2                      # Corta a imagem no meio

# Obtém uma parte da imagem que vai de Y[0 - height] X[0 - width]
new_frame = frame[0:height, 0:width] 

cv2.imshow("Imagem Cortada", new_frame)
  • Try running this code and I got this error " new_frame = frame[0:height, 0:width] Typeerror: Slice indices must be integers or None or have an index method"

  • Oops, I made an error in the code and fixed the problem. The error was being caused because when splitting the height the result was a float, and cannot use float’s in the Slice. Try running this code again

  • Actually this new code works, but for the sake of deepening if I wanted to take the other half the opposite side would be possible?

  • Of course, just do a simple calculation. To get the top half, you should start from ZERO point to half screen ( height / 2 ), soon, to get the other half just start from half the screen ( height / 2 ) up to the size of the maximum screen that is the height.

  • And how I would do that in the code?

  • Replace row 8 with new_frame = frame[height: height * 2, 0:width]

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.