Skip to content

Commit

Permalink
add SeamlessClone function
Browse files Browse the repository at this point in the history
  • Loading branch information
lijingfeng authored and deadprogram committed Dec 13, 2020
1 parent b090d9e commit 4e522f1
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ package gocv
#cgo !windows pkg-config: opencv4
#cgo CXXFLAGS: --std=c++11
#cgo windows CPPFLAGS: -IC:/opencv/build/install/include
#cgo windows LDFLAGS: -LC:/opencv/build/install/x64/mingw/lib -lopencv_core450 -lopencv_face450 -lopencv_videoio450 -lopencv_imgproc450 -lopencv_highgui450 -lopencv_imgcodecs450 -lopencv_objdetect450 -lopencv_features2d450 -lopencv_video450 -lopencv_dnn450 -lopencv_xfeatures2d450 -lopencv_plot450 -lopencv_tracking450 -lopencv_img_hash450 -lopencv_calib3d450 -lopencv_bgsegm450
#cgo windows LDFLAGS: -LC:/opencv/build/install/x64/mingw/lib -lopencv_core450 -lopencv_face450 -lopencv_videoio450 -lopencv_imgproc450 -lopencv_highgui450 -lopencv_imgcodecs450 -lopencv_objdetect450 -lopencv_features2d450 -lopencv_video450 -lopencv_dnn450 -lopencv_xfeatures2d450 -lopencv_plot450 -lopencv_tracking450 -lopencv_img_hash450 -lopencv_calib3d450 -lopencv_bgsegm450 -lopencv_photo450
*/
import "C"
6 changes: 6 additions & 0 deletions photo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "photo.h"

void SeamlessClone(Mat src, Mat dst, Mat mask, Point p, Mat blend, int flags) {
cv::Point pt(p.x, p.y);
cv::seamlessClone(*src, *dst, *mask, pt, *blend, flags);
}
36 changes: 36 additions & 0 deletions photo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package gocv

/*
#include <stdlib.h>
#include "photo.h"
*/
import "C"
import "image"

//SeamlessCloneFlags seamlessClone algorithm flags
type SeamlessCloneFlags int

const (
// NormalClone The power of the method is fully expressed when inserting objects with complex outlines into a new background.
NormalClone SeamlessCloneFlags = iota

// MixedClone The classic method, color-based selection and alpha masking might be time consuming and often leaves an undesirable halo. Seamless cloning, even averaged with the original image, is not effective. Mixed seamless cloning based on a loose selection proves effective.
MixedClone

// MonochromeTransfer Monochrome transfer allows the user to easily replace certain features of one object by alternative features.
MonochromeTransfer
)

// SeamlessClone blend two image by Poisson Blending.
//
// For further details, please see:
// https://docs.opencv.org/master/df/da0/group__photo__clone.html#ga2bf426e4c93a6b1f21705513dfeca49d
//
func SeamlessClone(src, dst, mask Mat, p image.Point, blend *Mat, flags SeamlessCloneFlags) {
cp := C.struct_Point{
x: C.int(p.X),
y: C.int(p.Y),
}

C.SeamlessClone(src.p, dst.p, mask.p, cp, blend.p, C.int(flags))
}
18 changes: 18 additions & 0 deletions photo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef _OPENCV3_PHOTO_H_
#define _OPENCV3_PHOTO_H_

#ifdef __cplusplus
#include <opencv2/opencv.hpp>

extern "C" {
#endif

#include "core.h"

void SeamlessClone(Mat src, Mat dst, Mat mask, Point p, Mat blend, int flags);

#ifdef __cplusplus
}
#endif

#endif //_OPENCV3_PHOTO_H
13 changes: 13 additions & 0 deletions photo_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gocv

func (c SeamlessCloneFlags) String() string {
switch c {
case NormalClone:
return "normal-clone"
case MixedClone:
return "mixed-clone"
case MonochromeTransfer:
return "monochrome-transfer"
}
return ""
}
23 changes: 23 additions & 0 deletions photo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gocv

import (
"image"
"testing"
)

func TestSeamlessClone(t *testing.T) {
src := NewMatWithSize(20, 20, MatTypeCV8UC3)
defer src.Close()
dst := NewMatWithSize(30, 30, MatTypeCV8UC3)
defer dst.Close()
blend := NewMatWithSize(dst.Rows(), dst.Cols(), dst.Type())
defer blend.Close()
mask := src.Clone()
defer mask.Close()

center := image.Point{dst.Cols() / 2, dst.Rows() / 2}
SeamlessClone(src, dst, mask, center, &blend, NormalClone)
if blend.Empty() || dst.Rows() != blend.Rows() || dst.Cols() != blend.Cols() {
t.Error("Invlalid SeamlessClone test")
}
}

0 comments on commit 4e522f1

Please sign in to comment.