凹面到凸面

2024-04-20 11:01:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我在图像中检测到一个物体,然后根据轮廓创建一个掩模。然后对面膜进行扩张和平滑处理。例如,从这样的图像:

chair

我最终得到了这样的面具:

chair mask

当我使用遮罩裁剪图像(椅子)时,我会丢失椅子腿之间的(背景)信息。为了避开这个问题,我想把这个凹的面具变成一个凸的。例如,在Photoshop中创建:

convex chair mask

我怎么能做到呢?公司名称:

代码(show_mask.py):

import sys
from pathlib import Path
from helpers_cv2 import *
import cv2
import numpy

img_path = Path(sys.argv[1])

img = cmyk_to_bgr(str(img_path))

threshed = threshold(img, 240, type=cv2.THRESH_BINARY_INV)
contours = find_contours(threshed)

mask        = mask_from_contours(img, contours)
mask_smooth = smooth_mask(mask, 51)
mask_dilate = dilate_mask(mask_smooth, 51)
mask_smooth = smooth_mask(mask_dilate, 51)

cv2.imshow("img", img)
cv2.imshow("mask_smooth", mask_smooth)

cv2.waitKey(0)
cv2.destroyAllWindows()

helpers_cv2.py

^{pr2}$

全尺寸图像和代码可在repository中找到。


Tags: path代码frompy图像importimgsys
1条回答
网友
1楼 · 发布于 2024-04-20 11:01:01

在访问了上面提到的pagema3oun之后,我设法使它工作起来。我只需在代码中添加几行:

contours = find_contours(mask_smooth)
hull = []
for i in range(len(contours)):
    hull.append(cv2.convexHull(contours[i], False))
hull = mask_from_contours(img, hull)

cv2.imshow("hull", hull)

输出图像:

convex hull

代码(show_convex_hull.py):

^{pr2}$

相关问题 更多 >