使用 Opencv 从图像中裁剪凹多边形 python

Cropping Concave polygon from Image using Opencv python

如何从图像中裁剪凹多边形。我的输入图像看起来像 .

封闭多边形的坐标是 [10,150]、[150,100]、[300,150]、[350,100]、[310,20]、[35,10]。我希望使用 opencv 裁剪由凹多边形包围的区域。我搜索了其他类似的问题,但找不到正确的答案。这就是我问的原因?你能帮帮我吗

非常感谢任何帮助。!!!

您可以通过 3 个步骤完成:

  1. 根据图像创建蒙版

    mask = np.zeros((高度, 宽度)) 点数 = np.array([[[10,150],[150,100],[300,150],[350,100],[310,20],[35,10]]]) cv2.fillPoly(遮罩, 点, (255))

  2. 对原始图像应用蒙版

    res = cv2.bitwise_and(img,img,mask = mask)

  3. 您可以选择删除裁剪图像以获得更小的图像

    rect = cv2.boundingRect(points) # returns (x,y,w,h) 的矩形 cropped = res[rect[1]: rect[1] + rect[3], rect[0]: rect[0] + rect[2]]

有了这个,你应该在最后裁剪图像

更新

为了完整起见,这里是完整的代码:

import numpy as np
import cv2

img = cv2.imread("test.png")
height = img.shape[0]
width = img.shape[1]

mask = np.zeros((height, width), dtype=np.uint8)
points = np.array([[[10,150],[150,100],[300,150],[350,100],[310,20],[35,10]]])
cv2.fillPoly(mask, points, (255))

res = cv2.bitwise_and(img,img,mask = mask)

rect = cv2.boundingRect(points) # returns (x,y,w,h) of the rect
cropped = res[rect[1]: rect[1] + rect[3], rect[0]: rect[0] + rect[2]]

cv2.imshow("cropped" , cropped )
cv2.imshow("same size" , res)
cv2.waitKey(0)

对于彩色背景版本,使用如下代码:

import numpy as np
import cv2

img = cv2.imread("test.png")
height = img.shape[0]
width = img.shape[1]

mask = np.zeros((height, width), dtype=np.uint8)
points = np.array([[[10,150],[150,100],[300,150],[350,100],[310,20],[35,10]]])
cv2.fillPoly(mask, points, (255))

res = cv2.bitwise_and(img,img,mask = mask)

rect = cv2.boundingRect(points) # returns (x,y,w,h) of the rect
im2 = np.full((res.shape[0], res.shape[1], 3), (0, 255, 0), dtype=np.uint8 ) # you can also use other colors or simply load another image of the same size
maskInv = cv2.bitwise_not(mask)
colorCrop = cv2.bitwise_or(im2,im2,mask = maskInv)
finalIm = res + colorCrop
cropped = finalIm[rect[1]: rect[1] + rect[3], rect[0]: rect[0] + rect[2]]

cv2.imshow("cropped" , cropped )
cv2.imshow("same size" , res)
cv2.waitKey(0)

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

代码:

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

img = cv2.imread("test.png")
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()

## (2) make mask
pts = pts - pts.min(axis=0)

mask = np.zeros(croped.shape[:2], np.uint8)
cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)

## (3) do bit-op
dst = cv2.bitwise_and(croped, croped, mask=mask)

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


cv2.imwrite("croped.png", croped)
cv2.imwrite("mask.png", mask)
cv2.imwrite("dst.png", dst)
cv2.imwrite("dst2.png", dst2)

源图片:

结果:

对于模糊图像背景版本,使用如下代码:

    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, :]