user1050619
user1050619

Reputation: 20856

Opencv find the coordinates of a roi image

I have this image and need to coordinates of the starting point and ending point of the head(until the neck).

enter image description here

I use the below code to crop the image but get the below error :-

import cv2
img = cv2.imread("/Users/pr/images/dog.jpg")
print img.shape
crop_img = img[400:500, 500:400] # Crop from x, y, w, h -> 100, 200, 300, 400
# NOTE: its img[y: y + h, x: x + w] and *not* img[x: x + w, y: y + h]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)

Error:-

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /Users/travis/build/skvark/opencv-python/opencv/modules/highgui/src/window.cpp, line 325

Question:-

  1. How can I find the coordinates of region of interest items?

Upvotes: 1

Views: 4078

Answers (2)

Aswinth Raj
Aswinth Raj

Reputation: 11

If you are trying to find the pixel co-ordinate of image that has to be used in the img[] you can simply use ms paint to find the pixel location. for example

img[y1:y2, x1:x2], here to find the values of x1,x2,y1 and y2 you can open the image in ms paint and place you cursor on the location where you need the co-ordinates. Paint will display the co-ordinates of that pixel at the bottom left corner of you mspaint window. consider this location as (x,y).

Screenshot of using MSpaint for getting pixel location.

Upvotes: 1

Vladimir Bystricky
Vladimir Bystricky

Reputation: 1330

If you want pick rectangle: x = 100, y =200, w = 300, h = 400, you should use code:

crop_img = img[200:600, 100:300]

and if you want cut dog's head you need:

crop_img = img[0:230, 250:550]

Upvotes: 1

Related Questions