diff --git a/opencv/imgproc.go b/opencv/imgproc.go index 35112e7..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") @@ -32,3 +40,15 @@ func Resize(src *IplImage, width, height, interpolation int) *IplImage { C.cvResize(unsafe.Pointer(src), unsafe.Pointer(dst), C.int(interpolation)) return dst } + +func Crop(src *IplImage, x, y, width, height int) *IplImage { + 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()) + Copy(src, dest, nil) + src.ResetROI() + + return dest +} 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()) + } +} diff --git a/samples/crop.go b/samples/crop.go new file mode 100644 index 0000000..6edd13d --- /dev/null +++ b/samples/crop.go @@ -0,0 +1,29 @@ +package main + +import ( + "os" + "path" + "runtime" + + opencv "github.com/lazywei/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) +}