【问题标题】:Cropping Concave polygon from Image using Opencv python使用 Opencv python 从图像中裁剪凹多边形
【发布时间】:2018-06-26 08:07:36
【问题描述】:

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

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

任何帮助将不胜感激。!!!

【问题讨论】:

  • 可以发原图吗?

标签: python opencv image-processing crop


【解决方案1】:

您可以分 3 步完成:

  1. 从图像中创建一个蒙版

    掩码 = np.zeros((高度, 宽度)) 点 = 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) 裁剪 = 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 的回答解决了我的问题。也谢谢你的回答。附言- 无法在评论中插入图片!!
  • @HimanshuTiwari 我不明白......这应该适用于任何多边形凸面或凹面......而且基本上两个答案几乎相同,我用随机图像测试了我的代码,我得到了与消音器相同的结果...哦,如果您设法解决它,那么一切都很好
  • 对不起,我做错了。但现在我得到了正确的输出。
  • @HimanshuTiwari 没关系 :) 有 2 个可能的结果可供选择总是好的 :)
  • @HimanshuTiwari 即使有两个答案可供选择,也值得接受并竖起大拇指。我发现它们都很有用且可读性很好,因此对它们都 +1。
【解决方案2】:

步骤

  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)

源图片:

结果:

【讨论】:

  • 裁剪后如何将背景中的黑色区域变为“白色区域”?
  • 是否可以在没有背景的情况下保存图像?我的意思是只保存那个裁剪区域.. ?
  • 透明背景而不是黑色或白色怎么样? @AbuOmair 运气好吗?
猜你喜欢
  • 2020-03-05
  • 1970-01-01
  • 2012-12-15
  • 2014-08-03
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多