Drawing angled rectangles in OpenCV

后端 未结 1 1275
伪装坚强ぢ
伪装坚强ぢ 2021-02-08 07:11

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.

However although I

相关标签:
1条回答
  • 2021-02-08 08:11

    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(...).


    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)
    

    Output:

    0 讨论(0)
提交回复
热议问题