使用OpenCV Python裁剪凹多边形图像

29
我如何从图像中剪裁一个凹多边形。 我的输入图像看起来像是 this。 并且封闭多边形的坐标为 [10,150],[150,100],[300,150],[350,100],[310,20],[35,10]。我想使用opencv剪裁由凹多边形界定的区域。我搜索了其他类似的问题,但没有找到正确的答案。这就是为什么我要问它?能帮帮我吗?
非常感谢任何帮助!

你能发原始图片吗? - api55
3个回答

62

步骤

  1. 使用多边形点找到区域
  2. 使用多边形点创建遮罩层
  3. 进行遮罩操作以进行裁剪
  4. 如有需要,添加白色背景

代码:

# 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)

源图片:

输入图片说明

结果:

输入图片说明


如何在裁剪后将背景中的黑色区域更改为“白色区域”? - Himanshu Tiwari
能否将没有背景的图像保存下来?我的意思是只保存裁剪后的区域。。? - AbuOmair
透明背景怎么样,而不是黑色或白色?@AbuOmair 有什么运气吗? - Nourdine

18

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

  1. 使用图像创建蒙版

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

  2. 将蒙版应用于原始图像

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

  3. 可选择性地裁剪图像以缩小大小

    rect = cv2.boundingRect(points) # 返回矩形的(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)

我尝试了你的代码,但是我得到的输出是裁剪的凸形状而不是凹形状。我的问题已经通过@Silencer的答案解决了。谢谢你的回答。P.S. - 无法在评论中插入图片!! - Himanshu Tiwari
1
@HimanshuTiwari 我不明白...这应该适用于任何多边形,无论是凸多边形还是凹多边形...基本上两个答案都几乎相同,我用随机图像测试了我的代码,得到了与Silencer相同的结果...好吧,如果你能解决它,那就一切都好。 - api55
抱歉我犯了一个错误,但现在我得到了正确的输出。 - Himanshu Tiwari
@HimanshuTiwari 没关系 :) 有两个可能的结果可以选择总是很好的 :) - api55
2
@HimanshuTiwari 即使有两个可供选择的答案,它们都应该被接受并点赞。我发现它们都很有用且易于阅读,所以两个答案都加一分。 - SKR
显示剩余5条评论

0

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接