2017-10-16 121 views
1

我有这个图像,需要坐标头的起点和终点(直到颈部)。Opencv找到一个roi图像的坐标

enter image description here

我用下面的代码来裁剪图像,但得到下面的错误: -

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) 

错误: -

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 

问题: -

  1. 如何找到坐标感兴趣区域的项目?
+0

不应'x + w'大于'x'? – zindarod

回答

1

如果你想挑的矩形:x = 100, y =200, w = 300, h = 400,你应该使用代码:

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

,如果你想砍狗的头,你需要:

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

如果你想找到必须在img []中使用的图像的像素坐标,您可以简单地使用ms paint来查找像素位置。例如

img [y1:y2,x1:x2],在这里可以找到x1,x2,y1和y2的值,您可以在ms paint中打开图像并将光标放在需要co的位置-ordinates。 Paint将在mspaint窗口的左下角显示该像素的坐标。考虑这个位置为(x,y)。

Screenshot of using MSpaint for getting pixel location.