Skip to content

[C API] Add c apis for matting #2497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions c_api/fastdeploy_capi/vision.h
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
#include "fastdeploy_capi/vision/detection/contrib/yolo/model.h"
#include "fastdeploy_capi/vision/ocr/ppocr/model.h"
#include "fastdeploy_capi/vision/segmentation/ppseg/model.h"
#include "fastdeploy_capi/vision/matting/ppmatting/model.h"
#include "fastdeploy_capi/vision/result.h"
#include "fastdeploy_capi/vision/visualize.h"
#endif
77 changes: 77 additions & 0 deletions c_api/fastdeploy_capi/vision/matting/ppmatting/model.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "fastdeploy_capi/vision/matting/ppmatting/model.h"

#include "fastdeploy_capi/internal/types_internal.h"

#ifdef __cplusplus
extern "C" {
#endif

FD_C_PPMattingWrapper* FD_C_CreatePPMattingWrapper(
const char* model_file, const char* params_file, const char* config_file,
FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper,
const FD_C_ModelFormat model_format) {
auto& runtime_option = CHECK_AND_CONVERT_FD_TYPE(RuntimeOptionWrapper,
fd_c_runtime_option_wrapper);
FD_C_PPMattingWrapper* fd_c_ppmatting_wrapper =
new FD_C_PPMattingWrapper();
fd_c_ppmatting_wrapper->matting_model =
std::unique_ptr<fastdeploy::vision::matting::PPMatting>(
new fastdeploy::vision::matting::PPMatting(
std::string(model_file), std::string(params_file),
std::string(config_file), *runtime_option,
static_cast<fastdeploy::ModelFormat>(model_format)));
return fd_c_ppmatting_wrapper;
}

void FD_C_DestroyPPMattingWrapper(
FD_C_PPMattingWrapper* fd_c_ppmatting_wrapper) {
delete fd_c_ppmatting_wrapper;
}

FD_C_Bool FD_C_PPMattingWrapperPredict(
FD_C_PPMattingWrapper* fd_c_ppmatting_wrapper, FD_C_Mat img,
FD_C_MattingResult* fd_c_matting_result) {
cv::Mat* im = reinterpret_cast<cv::Mat*>(img);
auto& ppmatting = CHECK_AND_CONVERT_FD_TYPE(
PPMattingWrapper, fd_c_ppmatting_wrapper);
FD_C_MattingResultWrapper* fd_c_matting_result_wrapper =
FD_C_CreateMattingResultWrapper();
auto& matting_result = CHECK_AND_CONVERT_FD_TYPE(
MattingResultWrapper, fd_c_matting_result_wrapper);

bool successful = ppmatting->Predict(im, matting_result.get());
if (successful) {
FD_C_MattingResultWrapperToCResult(fd_c_matting_result_wrapper,
fd_c_matting_result);
}
FD_C_DestroyMattingResultWrapper(fd_c_matting_result_wrapper);
return successful;
}

FD_C_Bool FD_C_PPMattingWrapperInitialized(
FD_C_PPMattingWrapper* fd_c_ppmatting_wrapper) {
auto& ppmatting = CHECK_AND_CONVERT_FD_TYPE(
PPMattingWrapper, fd_c_ppmatting_wrapper);
return ppmatting->Initialized();
}




#ifdef __cplusplus
}
#endif
76 changes: 76 additions & 0 deletions c_api/fastdeploy_capi/vision/matting/ppmatting/model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "fastdeploy_capi/core/fd_common.h"
#include "fastdeploy_capi/core/fd_type.h"
#include "fastdeploy_capi/runtime/runtime_option.h"
#include "fastdeploy_capi/vision/result.h"

typedef struct FD_C_PPMattingWrapper FD_C_PPMattingWrapper;

#ifdef __cplusplus
extern "C" {
#endif

/** \brief Create a new FD_C_PPMattingWrapper object
*
* \param[in] model_file Path of model file, e.g PPMatting-512/model.pdmodel
* \param[in] params_file Path of parameter file, e.g PPMatting-512/model.pdiparams, if the model format is ONNX, this parameter will be ignored
* \param[in] config_file Path of configuration file for deployment, e.g PPMatting-512/infer_cfg.yml
* \param[in] fd_c_runtime_option_wrapper RuntimeOption for inference, the default will use cpu, and choose the backend defined in `valid_cpu_backends`
* \param[in] model_format Model format of the loaded model, default is Paddle format
*
* \return Return a pointer to FD_C_PPMattingWrapper object
*/

FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_PPMattingWrapper*
FD_C_CreatePPMattingWrapper(
const char* model_file, const char* params_file, const char* config_file,
FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper,
const FD_C_ModelFormat model_format);

/** \brief Destroy a FD_C_PPMattingWrapper object
*
* \param[in] fd_c_ppmatting_wrapper pointer to FD_C_PPMattingWrapper object
*/

FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroyPPMattingWrapper(
__fd_take FD_C_PPMattingWrapper* fd_c_ppmatting_wrapper);

/** \brief Predict the segmentation result for an input image
*
* \param[in] fd_c_ppmatting_wrapper pointer to FD_C_PPMattingWrapper object
* \param[in] img pointer to cv::Mat image
* \param[in] fd_c_matting_result pointer to FD_C_SegmentationResult object, which stores the result.
*/
FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_PPMattingWrapperPredict(
__fd_keep FD_C_PPMattingWrapper* fd_c_ppmatting_wrapper,
FD_C_Mat img, FD_C_MattingResult* fd_c_matting_result);

/** \brief Check if the model is initialized successfully
*
* \param[in] fd_c_ppmatting_wrapper pointer to FD_C_PPMattingWrapper object
*
* \return Return a bool of value true if initialized successfully
*/

FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_PPMattingWrapperInitialized(
__fd_keep FD_C_PPMattingWrapper* fd_c_ppmatting_wrapper);


#ifdef __cplusplus
} // extern "C"
#endif
122 changes: 122 additions & 0 deletions c_api/fastdeploy_capi/vision/result.cc
Original file line number Diff line number Diff line change
@@ -541,6 +541,128 @@ void FD_C_SegmentationResultStr(
return;
}

// Matting Results

FD_C_MattingResultWrapper* FD_C_CreateMattingResultWrapper() {
FD_C_MattingResultWrapper* fd_c_matting_result_wrapper =
new FD_C_MattingResultWrapper();
fd_c_matting_result_wrapper->matting_result =
std::unique_ptr<fastdeploy::vision::MattingResult>(
new fastdeploy::vision::MattingResult());
return fd_c_matting_result_wrapper;
}

void FD_C_DestroyMattingResultWrapper(
__fd_take FD_C_MattingResultWrapper*
fd_c_matting_result_wrapper) {
delete fd_c_matting_result_wrapper;
}

FD_C_MattingResult* FD_C_CreatePPMattingResult() {
FD_C_MattingResult* fd_c_matting_result =
new FD_C_MattingResult();
return fd_c_matting_result;
}

void FD_C_DestroyPPMattingResult(
__fd_take FD_C_MattingResult* fd_c_matting_result) {
if (fd_c_matting_result == nullptr) return;
// delete alpha
delete[] fd_c_matting_result->alpha.data;
// delete foreground
delete[] fd_c_matting_result->foreground.data;
// delete shape
delete[] fd_c_matting_result->shape.data;
delete fd_c_matting_result;
}

void FD_C_MattingResultWrapperToCResult(
__fd_keep FD_C_MattingResultWrapper* fd_c_matting_result_wrapper,
__fd_keep FD_C_MattingResult* fd_c_matting_result) {
auto& matting_result = CHECK_AND_CONVERT_FD_TYPE(
MattingResultWrapper, fd_c_matting_result_wrapper);

// copy alpha
fd_c_matting_result->alpha.size =
matting_result->alpha.size();
fd_c_matting_result->alpha.data =
new float[fd_c_matting_result->alpha.size];
memcpy(fd_c_matting_result->alpha.data,
matting_result->alpha.data(),
sizeof(float) * fd_c_matting_result->alpha.size);
// copy foreground
fd_c_matting_result->foreground.size =
matting_result->foreground.size();
fd_c_matting_result->foreground.data =
new float[fd_c_matting_result->foreground.size];
memcpy(fd_c_matting_result->foreground.data,
matting_result->foreground.data(),
sizeof(float) * fd_c_matting_result->foreground.size);
// copy shape
fd_c_matting_result->shape.size = matting_result->shape.size();
fd_c_matting_result->shape.data =
new int64_t[fd_c_matting_result->shape.size];
memcpy(fd_c_matting_result->shape.data,
matting_result->shape.data(),
sizeof(int64_t) * fd_c_matting_result->shape.size);
// copy contain_foreground
fd_c_matting_result->contain_foreground =
matting_result->contain_foreground;
// copy type
fd_c_matting_result->type =
static_cast<FD_C_ResultType>(matting_result->type);
return;
}

FD_C_MattingResultWrapper* FD_C_CreateMattingResultWrapperFromCResult(
__fd_keep FD_C_MattingResult* fd_c_matting_result) {
FD_C_MattingResultWrapper* fd_c_matting_result_wrapper =
FD_C_CreateMattingResultWrapper();
auto& matting_result = CHECK_AND_CONVERT_FD_TYPE(
MattingResultWrapper, fd_c_matting_result_wrapper);

// copy alpha
matting_result->alpha.resize(
fd_c_matting_result->alpha.size);
memcpy(matting_result->alpha.data(),
fd_c_matting_result->alpha.data,
sizeof(float) * fd_c_matting_result->alpha.size);

// copy foreground
matting_result->foreground.resize(
fd_c_matting_result->foreground.size);
memcpy(matting_result->foreground.data(),
fd_c_matting_result->foreground.data,
sizeof(float) * fd_c_matting_result->foreground.size);

// copy shape
matting_result->shape.resize(fd_c_matting_result->shape.size);
memcpy(matting_result->shape.data(),
fd_c_matting_result->shape.data,
sizeof(int64_t) * fd_c_matting_result->shape.size);

// copy contain_score_map
matting_result->contain_foreground =
fd_c_matting_result->contain_foreground;
// copy type
matting_result->type = static_cast<fastdeploy::vision::ResultType>(
fd_c_matting_result->type);

return fd_c_matting_result_wrapper;
}

void FD_C_PPMattingResultStr(
FD_C_MattingResult* fd_c_matting_result, char* str_buffer) {
FD_C_MattingResultWrapper* fd_c_matting_result_wrapper =
FD_C_CreateMattingResultWrapperFromCResult(fd_c_matting_result);
auto& matting_result = CHECK_AND_CONVERT_FD_TYPE(
MattingResultWrapper, fd_c_matting_result_wrapper);
std::string information = matting_result->Str();
std::strcpy(str_buffer, information.c_str());
FD_C_DestroyMattingResultWrapper(fd_c_matting_result_wrapper);
return;
}

#ifdef __cplusplus
}
#endif
77 changes: 77 additions & 0 deletions c_api/fastdeploy_capi/vision/result.h
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ typedef struct FD_C_ClassifyResultWrapper FD_C_ClassifyResultWrapper;
typedef struct FD_C_DetectionResultWrapper FD_C_DetectionResultWrapper;
typedef struct FD_C_OCRResultWrapper FD_C_OCRResultWrapper;
typedef struct FD_C_SegmentationResultWrapper FD_C_SegmentationResultWrapper;
typedef struct FD_C_MattingResultWrapper FD_C_MattingResultWrapper;

#ifdef __cplusplus
extern "C" {
@@ -94,6 +95,18 @@ typedef struct FD_C_OneDimSegmentationResult {
FD_C_SegmentationResult* data;
} FD_C_OneDimSegmentationResult;

typedef struct FD_C_MattingResult {
FD_C_OneDimArrayFloat alpha;
FD_C_OneDimArrayFloat foreground;
FD_C_OneDimArrayInt64 shape;
FD_C_Bool contain_foreground;
FD_C_ResultType type;
} FD_C_MattingResult;

typedef struct FD_C_OneDimPPMattingResult {
size_t size;
FD_C_MattingResult* data;
} FD_C_OneDimPPMattingResult;

// Classification Results

@@ -356,6 +369,70 @@ FD_C_SegmentationResultStr(
__fd_keep FD_C_SegmentationResult* fd_c_segmentation_result, char* str_buffer);


// Matting Results

/** \brief Create a new FD_C_MattingResultWrapper object
*
* \return Return a pointer to FD_C_MattingResultWrapper object
*/

FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_MattingResultWrapper*
FD_C_CreateMattingResultWrapper();

/** \brief Destroy a FD_C_MattingResultWrapper object
*
* \param[in] fd_c_matting_result_wrapper pointer to FD_C_MattingResultWrapper object
*/

FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroyMattingResultWrapper(
__fd_take FD_C_MattingResultWrapper* fd_c_matting_result_wrapper);

/** \brief Create a new FD_C_MattingResult object
*
* \return Return a pointer to FD_C_MattingResult object
*/
FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_MattingResult*
FD_C_CreatePPMattingResult();

/** \brief Destroy a FD_C_MattingResult object
*
* \param[in] fd_c_matting_result pointer to FD_C_MattingResult object
*/

FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroyPPMattingResult(
__fd_take FD_C_MattingResult* fd_c_matting_result);

/** \brief Get a FD_C_MattingResult object from FD_C_MattingResultWrapper object
*
* \param[in] fd_c_matting_result_wrapper pointer to FD_C_MattingResultWrapper object
* \param[out] fd_c_matting_result pointer to FD_C_MattingResult object used to store data
*/
FASTDEPLOY_CAPI_EXPORT extern __fd_give void
FD_C_MattingResultWrapperToCResult(
__fd_keep FD_C_MattingResultWrapper* fd_c_matting_result_wrapper,
__fd_keep FD_C_MattingResult* fd_c_matting_result);

/** \brief Create a new FD_C_MattingResultWrapper object from FD_C_MattingResult object
*
* \param[in] fd_c_matting_result pointer to FD_C_MattingResult object
* \return Return a pointer to FD_C_MattingResultWrapper object
*/

FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_MattingResultWrapper*
FD_C_CreateMattingResultWrapperFromCResult(
__fd_keep FD_C_MattingResult* fd_c_matting_result);

/** \brief Print PPmattingResult formated information
*
* \param[in] fd_c_matting_result pointer to FD_C_MattingResult object
* \param[out] str_buffer used to store string
*/

FASTDEPLOY_CAPI_EXPORT extern __fd_give void
FD_C_PPMattingResultStr(
__fd_keep FD_C_MattingResult* fd_c_matting_result, char* str_buffer);



#ifdef __cplusplus
} // extern "C"
9 changes: 9 additions & 0 deletions c_api/fastdeploy_capi/vision/types_internal.cc
Original file line number Diff line number Diff line change
@@ -35,6 +35,9 @@ DECL_AND_IMPLEMENT_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(OCRResult,
DECL_AND_IMPLEMENT_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(
SegmentationResult, fd_segmentation_result_wrapper, segmentation_result)

// MattingResult
DECL_AND_IMPLEMENT_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(
MattingResult, fd_matting_result_wrapper, matting_result)
// Models:

// Classification
@@ -189,6 +192,12 @@ DECL_AND_IMPLEMENT_PIPELINE_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(
DECL_AND_IMPLEMENT_SEGMENTATION_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(
PaddleSegModel, fd_paddleseg_model_wrapper, segmentation_model);

// Matting models

// PPMatting
DECL_AND_IMPLEMENT_MATTING_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(
PPMatting, fd_ppmatting_wrapper, matting_model);

#endif

} // namespace fastdeploy
36 changes: 35 additions & 1 deletion c_api/fastdeploy_capi/vision/types_internal.h
Original file line number Diff line number Diff line change
@@ -35,6 +35,8 @@
#include "fastdeploy/vision/ocr/ppocr/ppocr_v4.h"
#include "fastdeploy/vision/ocr/ppocr/ppstructurev2_table.h"
#include "fastdeploy/vision/segmentation/ppseg/model.h"
#include "fastdeploy/vision/matting/ppmatting/ppmatting.h"


#define DEFINE_RESULT_WRAPPER_STRUCT(typename, varname) typedef struct FD_C_##typename##Wrapper { \
std::unique_ptr<fastdeploy::vision::typename> varname; \
@@ -61,6 +63,10 @@
std::unique_ptr<fastdeploy::vision::segmentation::typename> varname; \
} FD_C_##typename##Wrapper

#define DEFINE_MATTING_MODEL_WRAPPER_STRUCT(typename, varname) typedef struct FD_C_##typename##Wrapper { \
std::unique_ptr<fastdeploy::vision::matting::typename> varname; \
} FD_C_##typename##Wrapper

// ------------- belows are wrapper struct define --------------------- //

// Results:
@@ -78,6 +84,9 @@ DEFINE_RESULT_WRAPPER_STRUCT(OCRResult, ocr_result);
// Segmentation Result
DEFINE_RESULT_WRAPPER_STRUCT(SegmentationResult, segmentation_result);

// Matting Result
DEFINE_RESULT_WRAPPER_STRUCT(MattingResult, matting_result);

// Models:

// Classification
@@ -199,6 +208,11 @@ DEFINE_PIPELINE_MODEL_WRAPPER_STRUCT(PPStructureV2Table, ppstructurev2table_mode
// PaddleSegModel
DEFINE_SEGMENTATION_MODEL_WRAPPER_STRUCT(PaddleSegModel, segmentation_model);

// Matting models

// PPMatting
DEFINE_MATTING_MODEL_WRAPPER_STRUCT(PPMatting, matting_model);

// ------------- belows are function declaration for get ptr from wrapper --------------------- //

#define DECLARE_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(typename, varname) std::unique_ptr<fastdeploy::vision::typename>& \
@@ -226,6 +240,9 @@ FD_C_CheckAndConvert##typename##Wrapper( \
FD_C_CheckAndConvert##typename##Wrapper( \
FD_C_##typename##Wrapper* varname)

#define DECLARE_MATTING_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(typename, varname) std::unique_ptr<fastdeploy::vision::matting::typename>& \
FD_C_CheckAndConvert##typename##Wrapper( \
FD_C_##typename##Wrapper* varname)

namespace fastdeploy {

@@ -243,10 +260,13 @@ DECLARE_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(DetectionResult,
DECLARE_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(OCRResult,
fd_ocr_result_wrapper);

// SegementationResult
// SegmentationResult
DECLARE_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(SegmentationResult,
fd_segmentation_result_wrapper);

// MattingResult
DECLARE_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(MattingResult,
fd_Matting_result_wrapper);

// Models:

@@ -416,6 +436,12 @@ DECLARE_PIPELINE_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(PPStructureV2Table, fd_ppst
DECLARE_SEGMENTATION_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(
PaddleSegModel, fd_paddleseg_model_wrapper);

// Matting models

// PPMatting
DECLARE_MATTING_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(
PPMatting, fd_ppmatting_wrapper);

} // namespace fastdeploy


@@ -467,3 +493,11 @@ FD_C_CheckAndConvert##typename##Wrapper( \
"The pointer of " #var_wrapper_name " shouldn't be nullptr."); \
return var_wrapper_name->var_ptr_name; \
}

#define DECL_AND_IMPLEMENT_MATTING_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(typename, var_wrapper_name, var_ptr_name) std::unique_ptr<fastdeploy::vision::matting::typename>& \
FD_C_CheckAndConvert##typename##Wrapper( \
FD_C_##typename##Wrapper* var_wrapper_name) { \
FDASSERT(var_wrapper_name != nullptr, \
"The pointer of " #var_wrapper_name " shouldn't be nullptr."); \
return var_wrapper_name->var_ptr_name; \
}
36 changes: 36 additions & 0 deletions c_api/fastdeploy_capi/vision/visualize.cc
Original file line number Diff line number Diff line change
@@ -114,6 +114,42 @@ FD_C_Mat FD_C_VisSegmentation(FD_C_Mat im,
return new cv::Mat(result);
}

FD_C_Mat FD_C_VisMatting(FD_C_Mat im,
FD_C_MattingResult* fd_c_matting_result,
bool transparent_background,
float transparent_threshold,
bool remove_small_connected_area) {
FD_C_MattingResultWrapper* fd_c_matting_result_wrapper =
FD_C_CreateMattingResultWrapperFromCResult(fd_c_matting_result);
auto& matting_result = CHECK_AND_CONVERT_FD_TYPE(
MattingResultWrapper, fd_c_matting_result_wrapper);
cv::Mat result = fastdeploy::vision::VisMatting(
*(reinterpret_cast<cv::Mat*>(im)),
*matting_result,
transparent_background,
transparent_threshold,
remove_small_connected_area);
FD_C_DestroyMattingResultWrapper(fd_c_matting_result_wrapper);
return new cv::Mat(result);
}

FD_C_Mat FD_C_SwapBackground(FD_C_Mat im,
FD_C_Mat background,
FD_C_MattingResult* fd_c_matting_result,
bool remove_small_connected_area) {
FD_C_MattingResultWrapper* fd_c_matting_result_wrapper =
FD_C_CreateMattingResultWrapperFromCResult(fd_c_matting_result);
auto& matting_result = CHECK_AND_CONVERT_FD_TYPE(
MattingResultWrapper, fd_c_matting_result_wrapper);
cv::Mat result = fastdeploy::vision::SwapBackground(
*(reinterpret_cast<cv::Mat*>(im)),
*(reinterpret_cast<cv::Mat*>(background)),
*matting_result,
remove_small_connected_area);
FD_C_DestroyMattingResultWrapper(fd_c_matting_result_wrapper);
return new cv::Mat(result);
}

#ifdef __cplusplus
}
#endif
30 changes: 30 additions & 0 deletions c_api/fastdeploy_capi/vision/visualize.h
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
#include "fastdeploy_capi/core/fd_common.h"
#include "fastdeploy_capi/core/fd_type.h"
#include "fastdeploy_capi/vision/result.h"
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
@@ -101,6 +102,35 @@ FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_Mat FD_C_VisSegmentation(FD_C_Mat i
float weight);


/** \brief Show the visualized results for matting models
*
* \param[in] im the input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format
* \param[in] result the result produced by model
* \param[in] transparent_background if transparent_background==true, the background will with transparent color
* \param[in] transparent_threshold since the alpha value in MattringResult is a float between [0, 1], transparent_threshold is used to filter background pixel
* \param[in] remove_small_connected_area if remove_small_connected_area==true, the visualized result will not include the small connected areas
* \return cv::Mat type stores the visualized results
*/
FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_Mat FD_C_VisMatting(FD_C_Mat im,
FD_C_MattingResult* result,
bool transparent_background,
float transparent_threshold,
bool remove_small_connected_area);

/** \brief Swap the image background with MattingResult
*
* \param[in] im the input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format
* \param[in] background the background image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format
* \param[in] result the MattingResult produced by model
* \param[in] remove_small_connected_area if remove_small_connected_area==true, the visualized result will not include the small connected areas
* \return cv::Mat type stores the visualized results
*/
FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_Mat FD_C_SwapBackground(FD_C_Mat im,
FD_C_Mat background,
FD_C_MattingResult* result,
bool remove_small_connected_area);


#ifdef __cplusplus
} // extern "C"
#endif