{"id":18009278,"url":"https://github.com/xlite-dev/mgmatting-toolkit","last_synced_at":"2025-07-12T06:35:06.534Z","repository":{"id":103961865,"uuid":"435052828","full_name":"xlite-dev/mgmatting-toolkit","owner":"xlite-dev","description":"🍅MGMatting with MNN/TNN/ONNXRuntime C++, GPU/CPU, support dynamic shape. (https://github.com/DefTruth/lite.ai.toolkit)","archived":false,"fork":false,"pushed_at":"2022-02-03T13:29:19.000Z","size":15509,"stargazers_count":6,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-24T15:01:55.705Z","etag":null,"topics":["cpp","matting","mgmatting","mnn","onnxruntime","tnn"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xlite-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-05T02:09:53.000Z","updated_at":"2025-03-23T08:08:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"dc6f2ce9-bb97-4ec0-91a3-31774e49aa07","html_url":"https://github.com/xlite-dev/mgmatting-toolkit","commit_stats":null,"previous_names":["xlite-dev/mgmatting-toolkit"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlite-dev%2Fmgmatting-toolkit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlite-dev%2Fmgmatting-toolkit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlite-dev%2Fmgmatting-toolkit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlite-dev%2Fmgmatting-toolkit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xlite-dev","download_url":"https://codeload.github.com/xlite-dev/mgmatting-toolkit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245670525,"owners_count":20653383,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","matting","mgmatting","mnn","onnxruntime","tnn"],"created_at":"2024-10-30T02:08:59.172Z","updated_at":"2025-07-12T06:35:06.516Z","avatar_url":"https://github.com/xlite-dev.png","language":"C++","readme":"# MGMatting.lite.ai.toolkit\n使用 🍅🍅 Lite.AI.ToolKit C++工具箱来跑MGMatting人像抠图的一些案例(https://github.com/DefTruth/lite.ai.toolkit) , 包含ONNXRuntime C++、MNN、TNN版本。\n\n\u003cdiv align='center'\u003e\n  \u003cimg src='examples/resources/input.jpg' height=\"150px\" width=\"150px\"\u003e\n  \u003cimg src='examples/resources/mask.png' height=\"150px\" width=\"150px\"\u003e\n  \u003cimg src='resources/pha.jpg' height=\"150px\" width=\"150px\"\u003e\n  \u003cimg src='resources/fgr.jpg' height=\"150px\" width=\"150px\"\u003e\n  \u003cimg src='resources/merge.jpg' height=\"150px\" width=\"150px\"\u003e\n\u003c/div\u003e    \n\n如果觉得有用，不妨给个Star⭐️🌟支持一下吧~ 🙃🤪🍀\n\n## 2. C++版本源码\n\nMGMatting C++ 版本的源码包含ONNXRuntime、MNN和TNN三个版本，源码可以在 [lite.ai.toolkit](https://github.com/DefTruth/lite.ai.toolkit) 工具箱中找到。本项目主要介绍如何基于 [lite.ai.toolkit](https://github.com/DefTruth/lite.ai.toolkit) 工具箱，直接使用MGMatting来跑人像抠图。需要说明的是，本项目是基于MacOS下编译的 [liblite.ai.toolkit.v0.1.0.dylib](https://github.com/DefTruth/yolox.lite.ai.toolkit/blob/main/lite.ai.toolkit/lib) 来实现的，对于使用MacOS的用户，可以直接下载本项目包含的*liblite.ai.toolkit.v0.1.0*动态库和其他依赖库进行使用。而非MacOS用户，则需要从[lite.ai.toolkit](https://github.com/DefTruth/lite.ai.toolkit) 中下载源码进行编译。[lite.ai.toolkit](https://github.com/DefTruth/lite.ai.toolkit) c++工具箱目前包含80+流行的开源模型，就不多介绍了，只是平时顺手捏的，整合了自己学习过程中接触到的一些模型，感兴趣的同学可以去看看。\n* [mg_matting.cpp](https://github.com/DefTruth/lite.ai.toolkit/blob/main/lite/ort/cv/mg_matting.cpp)\n* [mg_matting.h](https://github.com/DefTruth/lite.ai.toolkit/blob/main/lite/ort/cv/mg_matting.h)\n* [mnn_mg_matting.cpp](https://github.com/DefTruth/lite.ai.toolkit/blob/main/lite/mnn/cv/mnn_mg_matting.cpp)\n* [mnn_mg_matting.h](https://github.com/DefTruth/lite.ai.toolkit/blob/main/lite/mnn/cv/mnn_mg_matting.h)\n* [tnn_mg_matting.cpp](https://github.com/DefTruth/lite.ai.toolkit/blob/main/lite/tnn/cv/tnn_mg_matting.cpp)\n* [tnn_mg_matting.h](https://github.com/DefTruth/lite.ai.toolkit/blob/main/lite/tnn/cv/tnn_mg_matting.h)\n\nONNXRuntime C++、MNN和TNN版本的推理实现均已测试通过，欢迎白嫖~  \n\n\n## 3. 模型文件\n\n### 3.1 ONNX模型文件\n可以从我提供的链接下载 ([Baidu Drive](https://pan.baidu.com/s/1elUGcx7CZkkjEoYhTMwTRQ) code: 8gin) 。\n\n\n|                 Class                 |      Pretrained ONNX Files      |              Rename or Converted From (Repo)              | Size  |\n| :-----------------------------------: | :-----------------------------: | :-------------------------------------------------------: | :---: |  \n| *lite::cv::matting::MGMatting* |   MGMatting-DIM-100k.onnx   | [MGMatting](https://github.com/yucornetto/MGMatting) | 113Mb |\n| *lite::cv::matting::MGMatting* |   MGMatting-RWP-100k.onnx   | [MGMatting](https://github.com/yucornetto/MGMatting) | 113Mb |\n\n### 3.2 MNN模型文件\nMNN模型文件下载地址，([Baidu Drive](https://pan.baidu.com/s/1KyO-bCYUv6qPq2M8BH_Okg) code: 9v63) 。\n\n|                 Class                 |      Pretrained MNN Files      |              Rename or Converted From (Repo)              | Size  |\n| :-----------------------------------: | :-----------------------------: | :-------------------------------------------------------: | :---: |\n| *lite::mnn::cv::matting::MGMatting* |   MGMatting-DIM-100k.mnn   | [MGMatting](https://github.com/yucornetto/MGMatting) | 113Mb |\n| *lite::mnn::cv::matting::MGMatting* |   MGMatting-RWP-100k.mnn   | [MGMatting](https://github.com/yucornetto/MGMatting) | 113Mb |\n\n\n### 3.3 TNN模型文件\nTNN模型文件下载地址，([Baidu Drive](https://pan.baidu.com/s/1lvM2YKyUbEc5HKVtqITpcw) code: 6o6k) 。\n\n|                 Class                 |      Pretrained TNN Files      |              Rename or Converted From (Repo)              | Size  |\n| :-----------------------------------: | :-----------------------------: | :-------------------------------------------------------: | :---: |\n| *lite::tnn::cv::matting::MGMatting* |   MGMatting-DIM-100k.opt.tnnproto\u0026tnnmodel   | [MGMatting](https://github.com/yucornetto/MGMatting) | 113Mb |\n| *lite::tnn::cv::matting::MGMatting* |   MGMatting-RWP-100k.opt.tnnproto\u0026tnnmodel   | [MGMatting](https://github.com/yucornetto/MGMatting) | 113Mb |\n\n\n## 4. 接口文档\n\n在[lite.ai.toolkit](https://github.com/DefTruth/lite.ai.toolkit) 中，MGMatting的实现类为：\n\n```c++\nclass LITE_EXPORTS lite::cv::face::detect::MGMatting;\nclass LITE_EXPORTS lite::mnn::cv::face::detect::MGMatting;\nclass LITE_EXPORTS lite::tnn::cv::face::detect::MGMatting;\n```  \n\n该类型目前包含1公共接口`detect`用于进行目标检测。\n```c++\npublic:\n    /**\n     * Image Matting Using MGMatting(https://github.com/yucornetto/MGMatting)\n     * @param mat: cv::Mat BGR HWC, source image\n     * @param mask: cv::Mat Gray, guidance mask.\n     * @param guidance_threshold: int, guidance threshold..\n     * @param content: types::MattingContent to catch the detected results.\n     */\n    void detect(const cv::Mat \u0026mat, cv::Mat \u0026mask, types::MattingContent \u0026content,\n                bool remove_noise = false, unsigned int guidance_threshold = 128);\n```\n`detect`接口的输入参数说明：\n* mat: cv::Mat类型，BGR格式。\n* mask: cv::Mat类型，Gray格式，指导抠图的mask，可以是coarse-binary-map/trimap/coarse-matte中的任意一种; \n* guidance_threshold：guidance mask阈值，参考MGMatting论文和官方仓库，使用默认的128即可；\n* remove_noise：是否移检测到的除小的连通区域，默认true；\n* content: types::MattingContent类型，用来保存检测的结果，包含类型为cv::Mat的三个成员，分别是\n    * `fgr_mat`: `cv::Mat (H,W,C=3) BGR` 格式，值范围为0~255 的 `CV_8UC3`, 用于保存估计的前景\n    * `pha_mat`:` cv::Mat (H,W,C=1)` 值范围为0.~1.的 `CV_32FC1`, 用于保存估计的alpha(matte)值\n    * `merge_mat`: `cv::Mat (H,W,C=3) BGR` 格式，值范围为0~255 的 `CV_8UC3`, 用于保存根据pha融合前景背景的合成图像\n    * `flag`: bool 类型标志位，表示是否检测成功\n\n## 5. 使用案例\n这里测试使用的是MGMatting-DIM-100k版本的模型，你可以尝试使用其他版本的模型。\n\n### 5.1 ONNXRuntime版本\n```c++\n#include \"lite/lite.h\"\n\nstatic void test_default()\n{\n    std::string onnx_path = \"../hub/onnx/cv/MGMatting-DIM-100k.onnx\";\n    std::string test_img_path = \"../resources/input.jpg\";\n    std::string test_mask_path = \"../resources/mask.png\";\n    std::string save_fgr_path = \"../logs/fgr.jpg\";\n    std::string save_pha_path = \"../logs/pha.jpg\";\n    std::string save_merge_path = \"../logs/merge.jpg\";\n    \n    auto *mgmatting = new lite::cv::matting::MGMatting(onnx_path, 16); // 16 threads\n    \n    lite::types::MattingContent content;\n    cv::Mat img_bgr = cv::imread(test_img_path);\n    cv::Mat mask = cv::imread(test_mask_path, cv::IMREAD_GRAYSCALE);\n    \n    // 1. image matting.\n    mgmatting-\u003edetect(img_bgr, mask, content, true);\n    \n    if (content.flag)\n    {\n        if (!content.fgr_mat.empty()) cv::imwrite(save_fgr_path, content.fgr_mat);\n        if (!content.pha_mat.empty()) cv::imwrite(save_pha_path, content.pha_mat * 255.);\n        if (!content.merge_mat.empty()) cv::imwrite(save_merge_path, content.merge_mat);\n        std::cout \u003c\u003c \"Default Version MGMatting Done!\" \u003c\u003c std::endl;\n    }\n    \n    delete mgmatting;\n}\n```  \n\n### 5.2 MNN版本\n```c++\n#include \"lite/lite.h\"\n\nstatic void test_mnn()\n{\n#ifdef ENABLE_MNN\n    std::string mnn_path = \"../hub/mnn/cv/MGMatting-DIM-100k.mnn\";\n    std::string test_img_path = \"../resources/input.jpg\";\n    std::string test_mask_path = \"../resources/mask.png\";\n    std::string save_fgr_path = \"../logs/fgr_mnn.jpg\";\n    std::string save_pha_path = \"../logs/pha_mnn.jpg\";\n    std::string save_merge_path = \"../logs/merge_mnn.jpg\";\n    \n    auto *mgmatting = new lite::mnn::cv::matting::MGMatting(mnn_path, 16); // 16 threads\n    \n    lite::types::MattingContent content;\n    cv::Mat img_bgr = cv::imread(test_img_path);\n    cv::Mat mask = cv::imread(test_mask_path, cv::IMREAD_GRAYSCALE);\n    \n    // 1. image matting.\n    mgmatting-\u003edetect(img_bgr, mask, content, true);\n    \n    if (content.flag)\n    {\n        if (!content.fgr_mat.empty()) cv::imwrite(save_fgr_path, content.fgr_mat);\n        if (!content.pha_mat.empty()) cv::imwrite(save_pha_path, content.pha_mat * 255.);\n        if (!content.merge_mat.empty()) cv::imwrite(save_merge_path, content.merge_mat);\n        std::cout \u003c\u003c \"MNN Version MGMatting Done!\" \u003c\u003c std::endl;\n    }\n    \n    delete mgmatting;\n#endif\n}\n```  \n\n### 5.3 TNN版本\n```c++\n#include \"lite/lite.h\"\n\nstatic void test_tnn()\n{\n#ifdef ENABLE_TNN\n    std::string proto_path = \"../hub/tnn/cv/MGMatting-DIM-100k.opt.tnnproto\";\n    std::string model_path = \"../hub/tnn/cv/MGMatting-DIM-100k.opt.tnnmodel\";\n    std::string test_img_path = \"../resources/input.jpg\";\n    std::string test_mask_path = \"../resources/mask.png\";\n    std::string save_fgr_path = \"../logs/fgr_tnn.jpg\";\n    std::string save_pha_path = \"../logs/pha_tnn.jpg\";\n    std::string save_merge_path = \"../logs/merge_tnn.jpg\";\n    \n    auto *mgmatting = new lite::tnn::cv::matting::MGMatting(proto_path, model_path, 16); // 16 threads\n    \n    lite::types::MattingContent content;\n    cv::Mat img_bgr = cv::imread(test_img_path);\n    cv::Mat mask = cv::imread(test_mask_path, cv::IMREAD_GRAYSCALE);\n    \n    // 1. image matting.\n    mgmatting-\u003edetect(img_bgr, mask, content, true);\n    \n    if (content.flag)\n    {\n        if (!content.fgr_mat.empty()) cv::imwrite(save_fgr_path, content.fgr_mat);\n        if (!content.pha_mat.empty()) cv::imwrite(save_pha_path, content.pha_mat * 255.);\n        if (!content.merge_mat.empty()) cv::imwrite(save_merge_path, content.merge_mat);\n        std::cout \u003c\u003c \"TNN Version MGMatting Done!\" \u003c\u003c std::endl;\n    }\n    \n    delete mgmatting;\n#endif\n}\n```  \n\n* 输出结果为:\n\n\u003cdiv align='center'\u003e\n  \u003cimg src='examples/resources/input.jpg' height=\"150px\" width=\"150px\"\u003e\n  \u003cimg src='examples/resources/mask.png' height=\"150px\" width=\"150px\"\u003e\n  \u003cimg src='resources/pha.jpg' height=\"150px\" width=\"150px\"\u003e\n  \u003cimg src='resources/fgr.jpg' height=\"150px\" width=\"150px\"\u003e\n  \u003cimg src='resources/merge.jpg' height=\"150px\" width=\"150px\"\u003e\n\u003c/div\u003e    \n\n## 6. 编译运行\n在MacOS下可以直接编译运行本项目，无需下载其他依赖库。其他系统则需要从[lite.ai.toolkit](https://github.com/DefTruth/lite.ai.toolkit) 中下载源码先编译*lite.ai.toolkit.v0.1.0*动态库。\n```shell\ngit clone --depth=1 https://github.com/DefTruth/MGMatting.lite.ai.toolkit.git\ncd MGMatting.lite.ai.toolkit \nsh ./build.sh\n```  \n\n* CMakeLists.txt设置\n\n```cmake\ncmake_minimum_required(VERSION 3.17)\nproject(MGMatting.lite.ai.toolkit)\n\nset(CMAKE_CXX_STANDARD 11)\n\n# setting up lite.ai.toolkit\nset(LITE_AI_DIR ${CMAKE_SOURCE_DIR}/lite.ai.toolkit)\nset(LITE_AI_INCLUDE_DIR ${LITE_AI_DIR}/include)\nset(LITE_AI_LIBRARY_DIR ${LITE_AI_DIR}/lib)\ninclude_directories(${LITE_AI_INCLUDE_DIR})\nlink_directories(${LITE_AI_LIBRARY_DIR})\n\nset(OpenCV_LIBS\n        opencv_highgui\n        opencv_core\n        opencv_imgcodecs\n        opencv_imgproc\n        opencv_video\n        opencv_videoio\n        )\n# add your executable\nset(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/examples/build)\n\nadd_executable(lite_mgmatting examples/test_lite_mgmatting.cpp)\ntarget_link_libraries(lite_mgmatting\n        lite.ai.toolkit\n        onnxruntime\n        MNN  # need, if built lite.ai.toolkit with ENABLE_MNN=ON,  default OFF\n        ncnn # need, if built lite.ai.toolkit with ENABLE_NCNN=ON, default OFF\n        TNN  # need, if built lite.ai.toolkit with ENABLE_TNN=ON,  default OFF\n        ${OpenCV_LIBS})  # link lite.ai.toolkit \u0026 other libs.\n```\n\n* building \u0026\u0026 testing information:\n```shell\n[ 50%] Building CXX object CMakeFiles/lite_mgmatting.dir/examples/test_lite_mgmatting.cpp.o\n[100%] Linking CXX executable lite_mgmatting\n[100%] Built target lite_mgmatting\nTesting Start ...\nLITEORT_DEBUG LogId: ../hub/onnx/cv/MGMatting-DIM-100k.onnx\n=============== Inputs ==============\nDynamic Input: image Init [1,3,512,512]\nDynamic Input: mask Init [1,1,512,512]\n=============== Outputs ==============\nDynamic Output 0: alpha_os1\nDynamic Output 1: alpha_os4\nDynamic Output 2: alpha_os8\nDefault Version MGMatting Done!\nLITEORT_DEBUG LogId: ../hub/onnx/cv/MGMatting-DIM-100k.onnx\n=============== Inputs ==============\nDynamic Input: image Init [1,3,512,512]\nDynamic Input: mask Init [1,1,512,512]\n=============== Outputs ==============\nDynamic Output 0: alpha_os1\nDynamic Output 1: alpha_os4\nDynamic Output 2: alpha_os8\nONNXRuntime Version MGMatting Done!\nCompute Shape Error for 598\nLITEMNN_DEBUG LogId: ../hub/mnn/cv/MGMatting-DIM-100k.mnn\n=============== Input-Dims ==============\n        **Tensor shape**: 1, 3, 0, 0, \n        **Tensor shape**: 1, 1, 0, 0, \nDimension Type: (CAFFE/PyTorch/ONNX)NCHW\n=============== Output-Dims ==============\ngetSessionOutputAll done!\nOutput: alpha_os1:      **Tensor shape**: 0, 0, 0, 0, \nOutput: alpha_os4:      **Tensor shape**: 0, 0, 0, 0, \nOutput: alpha_os8:      **Tensor shape**: 0, 0, 0, 0, \n========================================\nMNN Version MGMatting Done!\nLITETNN_DEBUG LogId: ../hub/tnn/cv/MGMatting-DIM-100k.opt.tnnproto\n=============== Input-Dims ==============\nimage: [1 3 1024 1024 ]\nmask: [1 1 1024 1024 ]\nInput Data Format: NCHW\n=============== Output-Dims ==============\nalpha_os1: [1 1 1024 1024 ]\nalpha_os4: [1 1 1024 1024 ]\nalpha_os8: [1 1 1024 1024 ]\n========================================\nTNN Version MGMatting Done!\nTesting Successful !\n```  \n\n![](resources/10.jpg)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxlite-dev%2Fmgmatting-toolkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxlite-dev%2Fmgmatting-toolkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxlite-dev%2Fmgmatting-toolkit/lists"}