Merge pull request #39 from vanillahsu/master
add opencv.Crop(), and imgproc_test.go
This commit is contained in:
commit
1b6fcd3cff
3 changed files with 106 additions and 0 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
57
opencv/imgproc_test.go
Normal file
57
opencv/imgproc_test.go
Normal file
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
29
samples/crop.go
Normal file
29
samples/crop.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue