diff --git a/opencv/cluster.go b/opencv/cluster.go new file mode 100644 index 0000000..989fc42 --- /dev/null +++ b/opencv/cluster.go @@ -0,0 +1,45 @@ +// Copyright 2014 . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package opencv + +//#include "opencv.h" +//#cgo linux pkg-config: opencv +//#cgo darwin pkg-config: opencv +//#cgo windows LDFLAGS: -lopencv_core242.dll -lopencv_imgproc242.dll -lopencv_photo242.dll -lopencv_highgui242.dll -lstdc++ +import "C" +import "unsafe" + +const ( + /* Select random initial centers in each attempt. */ + KMEANS_RANDOM_CENTERS = 0 + + /* Use kmeans++ center initialization by Arthur and Vassilvitskii [Arthur2007]. */ + KMEANS_PP_CENTERS = 2 +) + +/* +KMeans finds centers of k clusters in data and groups input samples around +the clusters. It returns a matrix that stores the cluster indices for every +sample, and a matrix that stores the cluster centers. +*/ +func KMeans(data *Mat, k int, termcrit TermCriteria, attempts int, rng RNG, flags int) (labels, centers *Mat) { + var compactness C.double + + labels = CreateMat(data.Rows(), 1, CV_32S) + centers = CreateMat(k, 1, data.Type()) + + C.cvKMeans2( + unsafe.Pointer(data), + C.int(k), + unsafe.Pointer(labels), + (C.CvTermCriteria)(termcrit), + C.int(attempts), + (*C.CvRNG)(&rng), + C.int(flags), + unsafe.Pointer(centers), + &compactness) + + return labels, centers +} diff --git a/opencv/cv.go b/opencv/cv.go index f26d410..3bc9400 100644 --- a/opencv/cv.go +++ b/opencv/cv.go @@ -21,6 +21,14 @@ const ( CV_BGR2GRAY = C.CV_BGR2GRAY CV_BLUR = C.CV_BLUR + + CV_8U = C.CV_8U + CV_8S = C.CV_8S + CV_16U = C.CV_16U + CV_16S = C.CV_16S + CV_32S = C.CV_32S + CV_32F = C.CV_32F + CV_64F = C.CV_64F ) /* Smoothes array (removes noise) */ @@ -38,6 +46,18 @@ func Smooth(src, dst *IplImage, smoothtype, // double param3 CV_DEFAULT(0), // double param4 CV_DEFAULT(0)); +/* +ConvertScale converts one image to another with optional linear transformation. +*/ +func ConvertScale(a, b *IplImage, scale, shift float64) { + C.cvConvertScale(unsafe.Pointer(a), unsafe.Pointer(b), C.double(scale), C.double(shift)) +} + +//CVAPI(void) cvConvertScale( const CvArr* src, +// CvArr* dst, +// double scale CV_DEFAULT(1), +// double shift CV_DEFAULT(0) ); + /* Converts input array pixels from one color space to another */ func CvtColor(src, dst *IplImage, code int) { C.cvCvtColor(unsafe.Pointer(src), unsafe.Pointer(dst), C.int(code)) diff --git a/opencv/cxcore.go b/opencv/cxcore.go index 6027e83..917e676 100644 --- a/opencv/cxcore.go +++ b/opencv/cxcore.go @@ -103,6 +103,25 @@ func (img *IplImage) GetROI() Rect { return Rect(r) } +/* +Reshape changes shape of the image without copying data. A value of `0` means +that channels or rows remain unchanged. +*/ +func (img *IplImage) Reshape(channels, rows, _type int) *Mat { + total := img.Width() * img.Height() + header := CreateMat(rows, total/rows, _type) + n := C.cvReshape(unsafe.Pointer(img), (*C.CvMat)(header), C.int(channels), C.int(rows)) + return (*Mat)(n) +} + +/* GetMat returns the matrix header for an image.*/ +func (img *IplImage) GetMat() *Mat { + var null C.int + tmp := CreateMat(img.Height(), img.Width(), CV_32S) + m := C.cvGetMat(unsafe.Pointer(img), (*C.CvMat)(tmp), &null, C.int(0)) + return (*Mat)(m) +} + // mat step const ( CV_AUTOSTEP = C.CV_AUTOSTEP @@ -160,6 +179,17 @@ func (mat *Mat) Clone() *Mat { return (*Mat)(mat_new) } +/* +Reshape changes shape of the matrix without copying data. A value of `0` means +that channels or rows remain unchanged. +*/ +func (m *Mat) Reshape(channels, rows int) *Mat { + total := m.Cols() * m.Rows() + n := CreateMat(rows, total/rows, m.Type()) + C.cvReshape(unsafe.Pointer(m), (*C.CvMat)(n), C.int(channels), C.int(rows)) + return n +} + /* Makes a new matrix from subrectangle of input array. No data is copied */ func GetSubRect(arr Arr, submat *Mat, rect Rect) *Mat { @@ -227,6 +257,32 @@ func GetDiag(arr Arr, submat *Mat, diag int) *Mat { return (*Mat)(mat_new) } +/* Get1D return a specific element from a 1-dimensional matrix. */ +func (m *Mat) Get1D(x int) Scalar { + ret := C.cvGet1D(unsafe.Pointer(m), C.int(x)) + return Scalar(ret) +} + +/* Get2D return a specific element from a 2-dimensional matrix. */ +func (m *Mat) Get2D(x, y int) Scalar { + ret := C.cvGet2D(unsafe.Pointer(m), C.int(x), C.int(y)) + return Scalar(ret) +} + +/* Get3D return a specific element from a 3-dimensional matrix. */ +func (m *Mat) Get3D(x, y, z int) Scalar { + ret := C.cvGet3D(unsafe.Pointer(m), C.int(x), C.int(y), C.int(z)) + return Scalar(ret) +} + +/* GetImage returns the image header for the matrix. */ +func (m *Mat) GetImage(channels int) *IplImage { + tmp := CreateImage(m.Cols(), m.Rows(), m.Type(), channels) + img := C.cvGetImage(unsafe.Pointer(m), (*C.IplImage)(tmp)) + + return (*IplImage)(img) +} + /* low-level scalar <-> raw data conversion functions */ func ScalarToRawData(scalar *Scalar, data unsafe.Pointer, type_, extend_to_12 int) { C.cvScalarToRawData( diff --git a/opencv/cxtype.go b/opencv/cxtype.go index 119606c..8878bb6 100644 --- a/opencv/cxtype.go +++ b/opencv/cxtype.go @@ -612,6 +612,16 @@ func ScalarAll(val0 float64) Scalar { return (Scalar)(rv) } +/* Val returns an array with the scalars values. */ +func (s Scalar) Val() [4]float64 { + return [4]float64{ + float64(s.val[0]), + float64(s.val[1]), + float64(s.val[2]), + float64(s.val[3]), + } +} + /****************************************************************************************\ * Dynamic Data structures * \****************************************************************************************/