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()) + } +}