我在图像中检测到一个物体,然后根据轮廓创建一个掩模。然后对面膜进行扩张和平滑处理。例如,从这样的图像:
我最终得到了这样的面具:
当我使用遮罩裁剪图像(椅子)时,我会丢失椅子腿之间的(背景)信息。为了避开这个问题,我想把这个凹的面具变成一个凸的。例如,在Photoshop中创建:
我怎么能做到呢?公司名称:
代码(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()
^{pr2}$
全尺寸图像和代码可在repository中找到。
在访问了上面提到的pagema3oun之后,我设法使它工作起来。我只需在代码中添加几行:
输出图像:
代码(show_convex_hull.py):
^{pr2}$相关问题 更多 >
编程相关推荐