在OpenCV中绘制成角度的矩形 [英] Drawing angled rectangles in OpenCV
问题描述
我正在使用OpenCV和python处理涉及人体跟踪的项目,并且正在使用HSV值查找肤色,然后在其周围绘制一个框.
I am using OpenCV and python to work on a project that involves body tracking, and I am using HSV values to find a skin tone then draw a box around it.
尽管我可以找到被跟踪的对象并在其周围画一个框,但矩形始终是垂直的,并且我想知道矩形是否存在角度,以便它们更好地显示检测到的对象,有点类似于minEnclosingCircle函数,但是使用矩形
However although I can find the tracked object and draw a box around it the rectangles are always vertical, and I would like to know if there is anyway angle the rectangles so they better show the detected object, somewhat like the minEnclosingCircle function, but using a rectangle
图像可能解释了我正在寻找的更好的东西.我得到的盒子是绿色的,而我要寻找的是黄色的.如您所见,蒙版显示和带角度的矩形也将更好地包围所选区域.我还包括原始图像.
The images probably explain what I am looking for better. The boxes that I am getting are green, and what I'm looking for I have drawn on in yellow. As you can see the mask shows and angled rectangle would also better encompass the selected area. I have also included the original image.
我的代码是:
import numpy as np
import cv2
# Input image
image = cv2.imread('TestIn.png')
# Converts to grey for better reulsts
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Converts to HSV
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# HSV values
lower_skin = np.array([5,36,53])
upper_skin = np.array([19,120,125])
mask = cv2.inRange(hsv, lower_skin, upper_skin)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# Finds contours
im2, cnts, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Draws contours
for c in cnts:
if cv2.contourArea(c) < 3000:
continue
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(image, (x,y), (x+w,y+h), (0, 255, 0), 2)
cv2.imshow('mask', mask)
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
输入图片:
输出图像(绿色的输出框,黄色的所需框):
Output image (output boxes in green, desired boxes in yellow):
推荐答案
您需要使用 cv2.boxPoints(...)
以获得表示多边形的点序列,其格式可以由其他OpenCV绘图功能使用,例如 cv2.polylines(...)
.
You need to use cv2.minAreaRect(...)
and then cv2.boxPoints(...)
to obtain a sequence of points representing the polygon in a format that can be used by other OpenCV drawing functions, such as cv2.drawContours(...)
or cv2.polylines(...)
.
基于示例在OpenCV文档中,我向您的代码中添加了一些语句以实现所需的结果:
Based on the example in OpenCV documentation I added few statements to your code to achieve the desired result:
import numpy as np
import cv2
# Input image
image = cv2.imread('oaHUs.jpg')
# Converts to grey for better reulsts
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Converts to HSV
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# HSV values
lower_skin = np.array([5,36,53])
upper_skin = np.array([19,120,125])
mask = cv2.inRange(hsv, lower_skin, upper_skin)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# Finds contours
im2, cnts, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Draws contours
for c in cnts:
if cv2.contourArea(c) < 3000:
continue
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(image, (x,y), (x+w,y+h), (0, 255, 0), 2)
## BEGIN - draw rotated rectangle
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(image,[box],0,(0,191,255),2)
## END - draw rotated rectangle
cv2.imwrite('out.png', image)
输出:
这篇关于在OpenCV中绘制成角度的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!