1

I am drawing a cv2 rectangle by using below line

cv2.rectangle(rightImg, (x, y), (x + w, y + h), (0, 0, 255), 2)

Now values are

x = 93
y = 62
w = 6
h = 3

Now I want to crop that part of the rectangle. Does below line of code make sense:

cropImg = rightImg[y:x, y+h:x+w]

or

cropImg = rightImg[y+h:x+w, y:x]

I have tried both of the above but its not cropping the exact area. What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
S Andrew
  • 5,592
  • 27
  • 115
  • 237

3 Answers3

1

you should try

cropImg=rightImg[y:y+h,x:x+w]. 
1

This is already answered here:
How to crop an image in OpenCV using Python

crop_img = rightImg[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)

Be careful of marked as duplicates.

asrulsibaoel
  • 500
  • 1
  • 7
  • 14
-1

It may look a bit weird but you have to write the y coordinates first.

cropImg = rightImg[y:y+h, x:x+w]

It will crop the image at where the rectangle is.