user9503597
user9503597

Reputation:

Converting OpenCV cv.Rectangle(img, pt1, pt2) into NumPy array with Python

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.

Upvotes: 1

Views: 926

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

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.

Upvotes: 3

Related Questions