222*_*222 3 python opencv angle bounding-box
我试图理顺这封信,但我不确定如何使用边界矩形这样做.
这是我到目前为止所拥有的:
import cv2
import numpy as np
img = cv2.imread('rtes.jpg',0)
ret,thresh = cv2.threshold(img,127,255,0)
ret,contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
M = cv2.moments(cnt)
print M
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
#cnt = contours[2]
#cv2.drawContours(img, [cnt], 0, (0,255,0), 3)
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img,[box],0,(255,0,0),2)
cv2.imshow('img',img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
我已经研究过OpenCV文档以使其正常工作但无法使其正常工作.我试图最终编写一个循环,它将尝试4个角度,看看它是否笔直.
cv2.minAreaRect(cnt)
Run Code Online (Sandbox Code Playgroud)
返回Box2D对象,该对象已存储旋转信息.
(x,y),(width,height),theta
Run Code Online (Sandbox Code Playgroud)
其中θ是Box2D对象的水平和第一侧所包围的角度,在您的示例中是内轮廓的较短边.
这取决于你想要达到的最终结果,但是如果你只想尝试旋转这个特定的例子,你可以这样做:
rows, cols = img.shape
rect = cv2.minAreaRect(cnt)
center = rect[0]
angle = rect[2]
rot = cv2.getRotationMatrix2D(center, angle-90, 1)
print(rot)
img = cv2.warpAffine(img, rot, (rows,cols))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4912 次 |
| 最近记录: |