2017-10-16 122 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.