1

I know the coordinates of rectange(x1,y1,x2,y2) I want to crop the rectangle portion

I have tried the same coordinates parameter as to draw rectangle for cropping.

black = np.zeros((1080, 720, 3), dtype = "uint8")
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    cv2.rectangle(black, (80,500), (150, 600), (0,255,0), 1, 8, 0)
    crop = frame[80:500,150:600]
    cv2.imshow("Black", black)
    cv2.imshow("crop", crop)
    cv2.imshow("Orginal", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

The output of the selection is not as rectangle drawn on the black.

1 Answers1

3

Numpy indexing works like this:

crop = frame[y1:y2, x1:x2]

So you need:

crop = frame[80:150, 500:600]

rather than:

crop = frame[80:500, 150:600]

Or maybe:

crop = frame[500:600, 80:150]

but I am not near anything with OpenCV installed to check.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • That worked to slice the frame, after processing I was wondering is there a way to (uncrop) do opposite of slicing back to normal size of frame? –  Jun 19 '19 at 11:21
  • I don’t understand, just use your original `frame` which is unaffected. – Mark Setchell Jun 19 '19 at 11:23
  • Because the processing is done only on the cropped portion `crop` of the `frame` , I want the output to be over the `black` numpy. The result isn't where roi is, it show else where on the 'black' frame. [link] imgur.com/a/iABwiU9 –  Jun 19 '19 at 11:47