如何使用OPencv和python抓取不规则区域?

时间:2019-06-19 22:56:42

标签: python-3.x image-processing opencv3.0

我想知道是否可以使用任何功能来抓住和倾斜区域。

我正在关注下一篇文章Drawing angled rectangles in OpenCV 我想知道如何仅抓住黄色区域并在另一个窗口中显示。 下面的代码是我从帖子中获取的。

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) #I WANT TO GRAB THIS AREA
    ## END - draw rotated rectangle

cv2.imwrite('out.png', image)

0 个答案:

没有答案