OPENCV:如何在Python中绘制一个旋转的边界盒
我目前正在使用OpenCV-Python进行绘画识别项目。 目前,我能够在相当的情况下检测到大多数绘画,但是边界框是包括很多背景的矩形。 这是因为cv2.boundingRect()函数找到了具有垂直投影(AFAIK)的边界矩形。但是,我想找到最好的边界框,而无需检测到任何背景。
我的代码的主要部分:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
hue, saturation, value = cv2.split(hsv)
blurred_sat = cv2.GaussianBlur(saturation, (5, 5), 0)
edges = cv2.Canny(blurred_sat, 45, 100)
kernel = np.ones((3, 3), np.uint8)
dilate = cv2.dilate(edges, kernel, iterations=6)
erode = cv2.erode(dilate, kernel, iterations=2)
contours, hierarchy = cv2.findContours(erode, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
# cv2.drawContours(frame, contours, -1, (255, 255, 0), 3)
for contour in contours:
area = cv2.contourArea(contour)
if area >= 3000:
x, y, w, h = cv2.boundingRect(contour)
subImg = frame[y:y+h, x:x+w]
cv2.rectangle(frame,
(x, y), (x + w, y + h),
(0, 255, 0),
2)
I am currently doing a project on painting recognition using opencv-python.
Right now I am able to detect most of the paintings decently however the bounding boxes are rectangles that include a lot of background.
This is because the cv2.boundingRect() function finds the bounding rectangle with a perpendicular projection (afaik). However I want to find the best bounding box without detecting any background.
The main part of my code:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
hue, saturation, value = cv2.split(hsv)
blurred_sat = cv2.GaussianBlur(saturation, (5, 5), 0)
edges = cv2.Canny(blurred_sat, 45, 100)
kernel = np.ones((3, 3), np.uint8)
dilate = cv2.dilate(edges, kernel, iterations=6)
erode = cv2.erode(dilate, kernel, iterations=2)
contours, hierarchy = cv2.findContours(erode, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
# cv2.drawContours(frame, contours, -1, (255, 255, 0), 3)
for contour in contours:
area = cv2.contourArea(contour)
if area >= 3000:
x, y, w, h = cv2.boundingRect(contour)
subImg = frame[y:y+h, x:x+w]
cv2.rectangle(frame,
(x, y), (x + w, y + h),
(0, 255, 0),
2)
Current output image (video)
Desired output image with desired bounding box in red
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查找和排序轮廓后,您需要使用
cv2.Minarearect()
函数。这绘制了一个矩形,将每个轮廓包裹在每个轮廓的区域中:注意:
cv2.minarearect()
返回旋转矩形的属性(中心,尺寸,尺寸,和旋转角)。cv2.boxpoints()
用于获得矩形的顶点。然后将其传递到cv2.drawContours()
绘制它们中。看看
文档
更新:
以下代码近似于四边形轮廓并在
frage
上绘制。变量value
确定您希望近似值的强度:After finding and sorting the contours, you need to use
cv2.minAreaRect()
function. This draws a rectangle enclosing each contour with the least area:Note:
cv2.minAreaRect()
returns properties of the rotated rectangle (center, dimensions and angle of rotation).cv2.boxPoints()
is used to obtain the vertices of the rectangle. This is then passed intocv2.drawContours()
to draw them.Have a look at the documentation
For details on the algorithm:
Update:
The following code approximates a quadrilateral around a contour and draws it on
frame
. The variablevalue
determines how strong you want your approximations to be: