Temp

[OpenCV] Polygon Crop

ju_young 2022. 5. 9. 14:57
728x90

background를 blur 처리한 이미지를 사용하려고 한다면 다음과 같은 코드를 사용하면 된다.

img = cv2.imread(img_path)
box = <box points>

# -- background
blur_bg = cv2.blur(img, (h, w))
mask1 = np.zeros((h, w, 3), np.uint8)
mask2 = np.ones((h, w, 3), np.uint8) * 255
cv2.fillPoly(mask1, box, (255, 255, 255))

# -- indexing
img_idx = np.where(mask1 == mask2)
bg_idx = np.where(mask1 != mask2)

# -- fill box
res = np.zeros((h, w, 3), np.int64)
res[img_idx] = img[img_idx]
res[bg_idx] = blur_bg[bg_idx]
res = res[y1:y2, x1:x2, :]

 

이외에 backgroud를 white나 black으로 해준다고 하면 더 간단한 코드로 실행할 수 있을 것 같다.

728x90