From 71e2e0317eba2358e01d3b2a72f3d2cd404680d5 Mon Sep 17 00:00:00 2001 From: Vanilla Hsu Date: Mon, 29 Dec 2014 20:10:56 +0800 Subject: [PATCH 1/4] Add Crop(). --- opencv/imgproc.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/opencv/imgproc.go b/opencv/imgproc.go index 35112e7..8139454 100644 --- a/opencv/imgproc.go +++ b/opencv/imgproc.go @@ -32,3 +32,24 @@ func Resize(src *IplImage, width, height, interpolation int) *IplImage { C.cvResize(unsafe.Pointer(src), unsafe.Pointer(dst), C.int(interpolation)) return dst } + +func NewRect(x, y, width, height int) Rect { + r := C.cvRect( + C.int(x), + C.int(y), + C.int(width), + C.int(height), + ) + return Rect(r) +} + +func Crop(src *IplImage, x, y, width, height int) *IplImage { + rect := NewRect(x, y, width, height) + + src.SetROI(rect) + dest := CreateImage(width, height, src.Depth(), src.Channels()) + Copy(src, dest, nil) + src.ResetROI() + + return dest +} From 1f95ef57873df1b7a8e1c58f3701ed24d54de2bb Mon Sep 17 00:00:00 2001 From: Vanilla Hsu Date: Mon, 29 Dec 2014 20:13:47 +0800 Subject: [PATCH 2/4] add Crop() sample code. --- samples/crop.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 samples/crop.go diff --git a/samples/crop.go b/samples/crop.go new file mode 100644 index 0000000..62a1849 --- /dev/null +++ b/samples/crop.go @@ -0,0 +1,29 @@ +package main + +import ( + "os" + "path" + "runtime" + + opencv "github.com/vanillahsu/go-opencv/opencv" +) + +func main() { + _, currentfile, _, _ := runtime.Caller(0) + filename := path.Join(path.Dir(currentfile), "../images/lena.jpg") + if len(os.Args) == 2 { + filename = os.Args[1] + } + + image := opencv.LoadImage(filename) + if image == nil { + panic("LoadImage fail") + } + defer image.Release() + + crop := opencv.Crop(image, 0, 0, 50, 50) + opencv.SaveImage("/tmp/crop.jpg", crop, 0) + crop.Release() + + os.Exit(0) +} From ecf8d35122e0c8ece025baa727db410ad96af08f Mon Sep 17 00:00:00 2001 From: Vanilla Hsu Date: Mon, 29 Dec 2014 20:57:57 +0800 Subject: [PATCH 3/4] 1: add CV_INTER_* const values. 2: add imgproc_test.go --- opencv/imgproc.go | 21 ++++++++-------- opencv/imgproc_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 opencv/imgproc_test.go diff --git a/opencv/imgproc.go b/opencv/imgproc.go index 8139454..90ebb5c 100644 --- a/opencv/imgproc.go +++ b/opencv/imgproc.go @@ -16,6 +16,14 @@ import ( "unsafe" ) +const ( + CV_INTER_NN = int(C.CV_INTER_NN) + CV_INTER_LINEAR = int(C.CV_INTER_LINEAR) + CV_INTER_CUBIC = int(C.CV_INTER_CUBIC) + CV_INTER_AREA = int(C.CV_INTER_AREA) + CV_INTER_LANCZOS4 = int(C.CV_INTER_LANCZOS4) +) + func Resize(src *IplImage, width, height, interpolation int) *IplImage { if width == 0 && height == 0 { panic("Width and Height cannot be 0 at the same time") @@ -33,18 +41,9 @@ func Resize(src *IplImage, width, height, interpolation int) *IplImage { return dst } -func NewRect(x, y, width, height int) Rect { - r := C.cvRect( - C.int(x), - C.int(y), - C.int(width), - C.int(height), - ) - return Rect(r) -} - func Crop(src *IplImage, x, y, width, height int) *IplImage { - rect := NewRect(x, y, width, height) + r := C.cvRect(C.int(x), C.int(y), C.int(width), C.int(height)) + rect := Rect(r) src.SetROI(rect) dest := CreateImage(width, height, src.Depth(), src.Channels()) diff --git a/opencv/imgproc_test.go b/opencv/imgproc_test.go new file mode 100644 index 0000000..85ed22e --- /dev/null +++ b/opencv/imgproc_test.go @@ -0,0 +1,57 @@ +package opencv + +import ( + "path" + "runtime" + "testing" +) + +func TestResize(t *testing.T) { + _, currentfile, _, _ := runtime.Caller(0) + filename := path.Join(path.Dir(currentfile), "../images/lena.jpg") + + image := LoadImage(filename) + if image == nil { + t.Fatal("LoadImage fail") + } + defer image.Release() + + rimage := Resize(image, 10, 10, CV_INTER_LINEAR) + if rimage == nil { + t.Fatal("Resize fail") + } + defer rimage.Release() + + if rimage.Width() != 10 { + t.Fatalf("excepted width is 10, returned %d\n", rimage.Width()) + } + + if rimage.Height() != 10 { + t.Fatalf("excepted width is 10, returned %d\n", rimage.Height()) + } +} + +func TestCrop(t *testing.T) { + _, currentfile, _, _ := runtime.Caller(0) + filename := path.Join(path.Dir(currentfile), "../images/lena.jpg") + + image := LoadImage(filename) + if image == nil { + t.Fatal("LoadImage fail") + } + defer image.Release() + + crop := Crop(image, 0, 0, 200, 200) + if crop == nil { + t.Fatal("Crop fail") + } + defer crop.Release() + + if crop.Width() != 200 { + t.Fatalf("excepted width is 200, returned %d\n", crop.Width()) + } + + if crop.Height() != 200 { + t.Fatalf("excepted width is 200, returned %d\n", crop.Height()) + } +} From 7cacf26e8087957ed15fdb25abf8e44cbb049856 Mon Sep 17 00:00:00 2001 From: Vanilla Hsu Date: Mon, 29 Dec 2014 21:01:19 +0800 Subject: [PATCH 4/4] fix api path. --- samples/crop.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/crop.go b/samples/crop.go index 62a1849..6edd13d 100644 --- a/samples/crop.go +++ b/samples/crop.go @@ -5,7 +5,7 @@ import ( "path" "runtime" - opencv "github.com/vanillahsu/go-opencv/opencv" + opencv "github.com/lazywei/go-opencv/opencv" ) func main() {