matting之trimap生成_膨胀腐蚀

article/2025/9/28 4:09:21

在抠图技术中三分图(trimap)经常被用到,通常使用的方法是膨胀腐蚀(一般在去除噪声的时候先腐蚀再膨胀)。

1.

import os
import numpy as np
import cv2def random_dilate(alpha, low=1, high=5, mode='constant'):"""Dilation. erode"""iterations = np.random.randint(1, 20)erode_ksize = np.random.randint(low=low, high=high)dilate_ksize = np.random.randint(low=low, high=high)erode_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (erode_ksize, erode_ksize)) # 椭圆 (尺寸)dilate_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (dilate_ksize, dilate_ksize))alpha_eroded = cv2.erode(alpha, erode_kernel, iterations=iterations)alpha_dilated = cv2.dilate(alpha, dilate_kernel, iterations=iterations)if mode == 'constant':alpha_noise = 128 * np.ones_like(alpha)alpha_noise[alpha_eroded >= 255] = 255 ###250alpha_noise[alpha_dilated <= 0] = 0else:value = np.random.randint(low=100, high=255)alpha_noise = value * ((alpha_dilated - alpha_eroded) / 255.)alpha_noise += alpha_erodedreturn alpha_noise

2.

def erode_dilate(msk, struc="ELLIPSE", size=(10, 10)):if struc == "RECT":kernel = cv2.getStructuringElement(cv2.MORPH_RECT, size)elif struc == "CORSS":kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, size)else:kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, size)#msk = msk.astype(np.float32)#print(msk)msk = msk / 255#print(msk.shape)#msk = msk.astype(np.uint8)# val in 0 or 255iterations = 3dilated = cv2.dilate(msk, kernel, iterations=iterations) * 255eroded = cv2.erode(msk, kernel, iterations=iterations) * 255 # 腐蚀# 一般在去噪声时先用腐蚀再用膨胀。# cnt1 = len(np.where(msk >= 0)[0])# cnt2 = len(np.where(msk == 0)[0])# cnt3 = len(np.where(msk == 1)[0])# #print("all:{} bg:{} fg:{}".format(cnt1, cnt2, cnt3))# assert(cnt1 == cnt2 + cnt3) # 边缘模糊所以存在两者之间的像素## cnt1 = len(np.where(dilated >= 0)[0])# cnt2 = len(np.where(dilated == 0)[0])# cnt3 = len(np.where(dilated == 255)[0])# #print("all:{} bg:{} fg:{}".format(cnt1, cnt2, cnt3))# assert(cnt1 == cnt2 + cnt3)## cnt1 = len(np.where(eroded >= 0)[0])# cnt2 = len(np.where(eroded == 0)[0])# cnt3 = len(np.where(eroded == 255)[0])# #print("all:{} bg:{} fg:{}".format(cnt1, cnt2, cnt3))# assert(cnt1 == cnt2 + cnt3)res = dilated.copy()#res[((dilated == 255) & (msk == 0))] = 128res[((dilated == 255) & (eroded == 0))] = 128 # alphareturn res

一些细节上的处理,导致结果有些不同,根据数据的实际情况,灵活运用,酌情修改


更新

from scipy.ndimage import morphology    
def getTrimap(self, alpha):fg = np.array(np.equal(alpha, 255).astype(np.float32))unknown = np.array(np.not_equal(alpha, 0).astype(np.float32))  # unknown = alpha > 0unknown = unknown - fgunknown = morphology.distance_transform_edt(unknown == 0) <= np.random.randint(1, 20)trimap = fgtrimap[unknown] = 0.5# print(trimap[:, :, :1].shape)return trimap[:, :, :1]

二次更新:

from scipy.ndimage import grey_dilation, grey_erosion
low, high = 2, 8  ### 根据具体情况再定义
d_size = np.random.randint(low=low, high=high)
e_size = np.random.randint(low=low, high=high)
matte = label / 255.  # $numpy array of your matte(with values between[0, 1])$trimap = (matte >= 0.9).astype('float32')
not_bg = (matte > 0).astype('float32')trimap[np.where((grey_dilation(not_bg, size=(d_size, d_size)) - grey_erosion(trimap, size=(e_size, e_size))) != 0)] = 0.5

Reference:

Python - OpenCV 之图像形态学(膨胀与腐蚀)


http://chatgpt.dhexx.cn/article/42hp2VZO.shtml

相关文章

抠图算法-Alpha Matting

目录 概述graph cutAlpha Matting 概述 对于抠图&#xff0c;比较简单的方法是图像分割&#xff0c;这是很老的方法&#xff0c;但这其实算不上真正意义的抠图&#xff0c;因为他的主要目的是用于图像之间块与块的分割。典型的就是grabcut算法&#xff0c;opencv上面有相应的优…

Background Matting V2 学习

论文&#xff1a; [2012.07810] Real-Time High-Resolution Background Matting (arxiv.org) GitHub项目源码&#xff1a;GitHub - PeterL1n/BackgroundMattingV2: Real-Time High-Resolution Background Matting 目录 论文学习 方法设计&#xff1a; 网络模型&#xff1a; 训…

matting系列论文笔记(三):Boosting Semantic Human Matting with Coarse Annotations

matting系列论文笔记&#xff08;三&#xff09;&#xff1a;Boosting Semantic Human Matting with Coarse Annotations 论文链接: CVPR2020 Boosting Semantic Human Matting with Coarse Annotations [1] 代码&#xff1a;暂无 文章目录 matting系列论文笔记&#xff08;三…

Image Matting 图像抠图技术与深度学习抠图

Image Matting: 图像抠图技术是指从静态图像或者视频序列中抽取感兴趣目标的过程&#xff0c;在ps和视频编辑中有重要的应用。 1.Image Matting Matting 技术可以表示为下面的图&#xff0c;与语义分割不同&#xff0c;它可以针对感兴趣前景物体进行细节处理、包括细微的毛发和…

论文阅读——Deep Image Matting

一、摘要 强调image matting&#xff08;抠图&#xff09;的现实意义。已有的算法在前景和背景颜色相似或者拥有复杂的纹理时表现较差&#xff0c;主要原因有两个&#xff0c;一个是只运用到低维特征&#xff0c;另一个是缺少高维语境。所以这篇论文提出了深度模型算法可以解决…

【CVPR2022】Boosting Robustness of Image Matting with Context Assembling and Strong Data Augmentation

Boosting Robustness of Image Matting with Context Assembling and Strong Data Augmentation 中文题目 利用上下文组合和强数据增强的增强鲁棒图像抠图 paper&#xff1a;https://openaccess.thecvf.com/content/CVPR2022/papers/Dai_Boosting_Robustness_of_Image_Mattin…

图像抠图Image Matting算法调研

目录 1.Trimap和Strokes 2. 相关数据集 3.论文算法调研 3.1 Deep Image Matting 3.2 Semantic Image Matting 3.3 Background Matting 3.4 Background Matting V2 3.5 Semantic Human Matting 3.6 HAttMatting 3.7 MMNet&#xff1a;Towards Real-Time Automatic Por…

抠图技术及方法简介(Image Matting Overview)

之前接触过语义分割&#xff0c;所以在刚接触图像抠图时以为两者是差不多。语义分割是端到端的&#xff0c;对像素按照语义进行多分类&#xff0c;而抠图就是按照前景和背景进行二分类嘛&#xff1f;实际上这是错误的理解。语义分割重在对每个像素的语义理解&#xff0c;将相同…

Portrait Matting

文章作者为 Google Research 的软件工程师 Sergio Orts Escolano 和 Jana Ehman&#xff0c;文章发表于 2022 年 1 月 24 日。 Portrait Matting 网络 抠图是提取精确的 alpha 遮罩的过程&#xff0c;抠图假设图像是前景和背景图像的合成&#xff0c;因此每个像素的强度是前景…

Background Matting详解

转自&#xff1a;https://zhuanlan.zhihu.com/p/148265115?from_voters_pagetruehttps://www.aiuai.cn/aifarm1462.html 使用人工智能技术实现类似PhotoShop等工具的抠图功能是一个非常有趣且有科研前景的一个方向。和分割算法只有 和 两个值相比&#xff0c;抠图得到的边缘…

【Matting】MODNet:实时人像抠图模型-onnx python部署

上一篇博客【Matting】MODNet&#xff1a;实时人像抠图模型-笔记分析了MODNet的原理&#xff0c;本篇博客将使用python部署MODNet官方提供的onnx模型&#xff0c;其效果如下&#xff1a; 在线人像抠图体验&#xff1a;CV案例 相关部署链接&#xff1a; 【Matting】MODNet&…

【笔记】Robust High-Resolution Video Matting with Temporal Guidance

Robust High-Resolution Video Matting with Temporal Guidance算法笔记 一、算法简介二、网络结构三、训练1、数据集2、训练过程3、损失函数 Robust High-Resolution Video Matting with Temporal Guidance 论文地址 RobustVideoMatting 代码地址 Robust High-Resolution Vide…

【Matting】MODNet:实时人像抠图模型-笔记

paper&#xff1a;MODNet: Real-Time Trimap-Free Portrait Matting via Objective Decomposition (AAAI 2022) github&#xff1a;https://github.com/ZHKKKe/MODNet 抠图在线体验&#xff1a;CV案例 部署教程&#xff1a; 【Matting】MODNet&#xff1a;实时人像抠图模型…

Image Matting 客观评价指标、数据集及主观评价

Image Matting 客观评价指标、数据集及主观评价 2021.7更新 PPM-100数据集已经开放&#xff0c;GitHub&#xff0c;详情见下文章节2.4 目录 Image Matting 客观评价指标、数据集及主观评价2021.7更新 客观评价指标1. 精度1.1 SAD1.2 MSE 均方误差1.3 Gradient error1.4 Conne…

深度学习(7)之图像抠图 Image Matting算法调研

目录 1.Trimap和Strokes 2. 相关数据集 3.论文算法调研 3.1 Deep Image Matting 3.2 Semantic Image Matting 3.3 Background Matting 3.4 Background Matting V2 3.5 Semantic Human Matting 3.6 HAttMatting 3.7 MMNet&#xff1a;Towards Real-Time Automatic Portrait Matt…

【SHM】Semantic Human Matting抠图算法调试

前言&#xff1a; 2018年阿里的论文《Semantatic Human Matting》给出了抠图领域的一个新方法&#xff0c;可惜阿里并没有公布源码&#xff0c;而牛人在Github上对这个论文进行了复现&#xff0c;我也是依赖Github上的工程进行钻研&#xff0c;而在调试的过程中&#xff0c;发…

[Matting]论文阅读:Deep Image Matting 详细解读

[Matting]论文阅读&#xff1a;Deep Image Matting 详细解读 一 、摘要 任务二、方法2.1 第一部分&#xff08;Matting encoder-decoder stage&#xff09;2.2 第二部分&#xff08;Matting refinement stage&#xff09;2.3 数据部分&#xff08;Composed Datasets&#xff09…

【CVPR2022】MatteFormer: Transformer-Based Image Matting via Prior-Tokens

MatteFormer: Transformer-Based Image Matting via Prior-Tokens 中文题目: 借助先验Token的基于Transformer的图像抠图 paper&#xff1a;https://arxiv.org/pdf/2203.15662v1.pdf code&#xff1a;https://github.com/webtoon/matteformer 摘要 本文提出了一个基于Tran…

Image Matting代码和算法效果总结

本文参考了&#xff1a;http://blog.leanote.com/post/610167078qq.com/Image-Matting。作者给出了大部分matting-code的链接&#xff0c;说明也比较细致、系统&#xff0c;在这里向作者表示由衷地感谢&#xff01;以下是博客的原文&#xff1a; 肖总博客&#xff1a;http://3…

matting笔记_一周小结

去年刚入坑的旧笔记&#xff0c;刚翻出来… 1. 利用神经网络做抠图的入坑之作《Deep Image Matting》 详情见之前的笔记 matting系列论文笔记&#xff08;一&#xff09;&#xff1a;Deep Image Matting 由于image matting的工作没有特别好的综述&#xff0c;有的综述也不是…