mirror of
https://github.com/bububa/openvision.git
synced 2025-09-26 17:51:13 +08:00
67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
package segmentor
|
|
|
|
/*
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include "openvision/common/common.h"
|
|
#include "openvision/pose/segmentor.h"
|
|
*/
|
|
import "C"
|
|
import (
|
|
"unsafe"
|
|
|
|
openvision "github.com/bububa/openvision/go"
|
|
"github.com/bububa/openvision/go/common"
|
|
)
|
|
|
|
// Segmentor represents segmentor interface
|
|
type Segmentor interface {
|
|
common.Estimator
|
|
Matting(img *common.Image, out *common.Image) error
|
|
Merge(img *common.Image, bg *common.Image, out *common.Image) error
|
|
}
|
|
|
|
// Matting returns pose segment matting image
|
|
func Matting(d Segmentor, img *common.Image, out *common.Image) error {
|
|
imgWidth := img.WidthF64()
|
|
imgHeight := img.HeightF64()
|
|
data := img.Bytes()
|
|
outImgC := common.NewCImage()
|
|
defer common.FreeCImage(outImgC)
|
|
errCode := C.pose_segment_matting(
|
|
(C.IPoseSegmentor)(d.Pointer()),
|
|
(*C.uchar)(unsafe.Pointer(&data[0])),
|
|
C.int(imgWidth),
|
|
C.int(imgHeight),
|
|
(*C.Image)(unsafe.Pointer(outImgC)))
|
|
if errCode != 0 {
|
|
return openvision.DetectPoseError(int(errCode))
|
|
}
|
|
common.GoImage(outImgC, out)
|
|
return nil
|
|
}
|
|
|
|
// Merge merge pose with background
|
|
func Merge(d Segmentor, img *common.Image, bg *common.Image, out *common.Image) error {
|
|
imgWidth := img.WidthF64()
|
|
imgHeight := img.HeightF64()
|
|
data := img.Bytes()
|
|
bgWidth := bg.Width()
|
|
bgHeight := bg.Height()
|
|
bgdata := bg.Bytes()
|
|
outImgC := common.NewCImage()
|
|
defer common.FreeCImage(outImgC)
|
|
errCode := C.pose_segment_merge(
|
|
(C.IPoseSegmentor)(d.Pointer()),
|
|
(*C.uchar)(unsafe.Pointer(&data[0])),
|
|
C.int(imgWidth), C.int(imgHeight),
|
|
(*C.uchar)(unsafe.Pointer(&bgdata[0])),
|
|
C.int(bgWidth), C.int(bgHeight),
|
|
(*C.Image)(unsafe.Pointer(outImgC)))
|
|
if errCode != 0 {
|
|
return openvision.DetectPoseError(int(errCode))
|
|
}
|
|
common.GoImage(outImgC, out)
|
|
return nil
|
|
}
|