Cropping Concave polygon from Image using Opencv python

Issue

How can I crop a concave polygon from an image. My Input image look like
.

and the coordinates of closed polygon are
[10,150],[150,100],[300,150],[350,100],[310,20],[35,10]. I want region bounded by concave polygon to be cropped using opencv. I searched for other similar questions but I did not able to find correct answer. That’s why I am asking it ? Can you help me.

Any help would be highly appreciated.!!!

Solution

Steps

1. find region using the poly points
2. create mask using the poly points
3. do mask op to crop
4. add white bg if needed

The code:

# 2018.01.17 20:39:17 CST
# 2018.01.17 20:50:35 CST
import numpy as np
import cv2

pts = np.array([[10,150],[150,100],[300,150],[350,100],[310,20],[35,10]])

## (1) Crop the bounding rect
rect = cv2.boundingRect(pts)
x,y,w,h = rect
croped = img[y:y+h, x:x+w].copy()

pts = pts - pts.min(axis=0)

cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)

## (3) do bit-op

## (4) add the white background
bg = np.ones_like(croped, np.uint8)*255
dst2 = bg+ dst

cv2.imwrite("croped.png", croped)