Compare commits

..

No commits in common. "1d8dd6c04e813a368aa334242b4c73a68cb61eb2" and "94deb59c3d5a972cfa9453f223b2ffc5f56285a6" have entirely different histories.

8 changed files with 19 additions and 266 deletions

View file

@ -24,11 +24,11 @@ func GcvInitCameraMatrix2D(objPts, imgPts *mat64.Dense, dims [2]int,
imgPtsVec := NewGcvPoint2f32Vector(int64(nObjPts)) imgPtsVec := NewGcvPoint2f32Vector(int64(nObjPts))
for j := 0; j < nObjPts; j++ { for j := 0; j < nObjPts; j++ {
objPtsVec.Set(j, NewGcvPoint3f32(mat64.Col(nil, j, objPts)...)) objPtsVec.Set(j, NewGcvPoint3f32(objPts.Col(nil, j)...))
} }
for j := 0; j < nObjPts; j++ { for j := 0; j < nObjPts; j++ {
imgPtsVec.Set(j, NewGcvPoint2f32(mat64.Col(nil, j, imgPts)...)) imgPtsVec.Set(j, NewGcvPoint2f32(imgPts.Col(nil, j)...))
} }
_imgSize := NewGcvSize2i(dims[0], dims[1]) _imgSize := NewGcvSize2i(dims[0], dims[1])
@ -52,11 +52,11 @@ func GcvCalibrateCamera(objPts, imgPts, camMat, distCoeffs *mat64.Dense,
imgPtsVec := NewGcvPoint2f32Vector(int64(nObjPts)) imgPtsVec := NewGcvPoint2f32Vector(int64(nObjPts))
for j := 0; j < nObjPts; j++ { for j := 0; j < nObjPts; j++ {
objPtsVec.Set(j, NewGcvPoint3f32(mat64.Col(nil, j, objPts)...)) objPtsVec.Set(j, NewGcvPoint3f32(objPts.Col(nil, j)...))
} }
for j := 0; j < nObjPts; j++ { for j := 0; j < nObjPts; j++ {
imgPtsVec.Set(j, NewGcvPoint2f32(mat64.Col(nil, j, imgPts)...)) imgPtsVec.Set(j, NewGcvPoint2f32(imgPts.Col(nil, j)...))
} }
_camMat := Mat64ToGcvMat(camMat) _camMat := Mat64ToGcvMat(camMat)

View file

@ -24,7 +24,7 @@ func TestGcvInitCameraMatrix2D(t *testing.T) {
-0.226950, 0.942377, -0.899869, -0.226950, 0.942377, -0.899869,
-1.148912, 0.093725, 0.634745, -1.148912, 0.093725, 0.634745,
}) })
objPts.Clone(objPts.T()) objPts.TCopy(objPts)
imgPts := mat64.NewDense(10, 2, []float64{ imgPts := mat64.NewDense(10, 2, []float64{
-0.384281, -0.299055, -0.384281, -0.299055,
@ -38,15 +38,15 @@ func TestGcvInitCameraMatrix2D(t *testing.T) {
0.631444, -0.340257, 0.631444, -0.340257,
-0.647580, 0.502113, -0.647580, 0.502113,
}) })
imgPts.Clone(imgPts.T()) imgPts.TCopy(imgPts)
camMat := GcvInitCameraMatrix2D(objPts, imgPts, [2]int{1920, 1080}, 1) camMat := GcvInitCameraMatrix2D(objPts, imgPts, [2]int{1920, 1080}, 1)
assert.InDeltaSlice(t, []float64{1.47219772e+03, 0.00000000e+00, 9.59500000e+02}, assert.InDeltaSlice(t, []float64{1.47219772e+03, 0.00000000e+00, 9.59500000e+02},
mat64.Row(nil, 0, camMat), DELTA) camMat.Row(nil, 0), DELTA)
assert.InDeltaSlice(t, []float64{0.00000000e+00, 1.47219772e+03, 5.39500000e+02}, assert.InDeltaSlice(t, []float64{0.00000000e+00, 1.47219772e+03, 5.39500000e+02},
mat64.Row(nil, 1, camMat), DELTA) camMat.Row(nil, 1), DELTA)
assert.InDeltaSlice(t, []float64{0.00000000e+00, 0.00000000e+00, 1.00000000e+00}, assert.InDeltaSlice(t, []float64{0.00000000e+00, 0.00000000e+00, 1.00000000e+00},
mat64.Row(nil, 2, camMat), DELTA) camMat.Row(nil, 2), DELTA)
} }
func TestGcvCalibrateCamera(t *testing.T) { func TestGcvCalibrateCamera(t *testing.T) {
@ -62,7 +62,7 @@ func TestGcvCalibrateCamera(t *testing.T) {
-0.226950, 0.942377, -0.899869, -0.226950, 0.942377, -0.899869,
-1.148912, 0.093725, 0.634745, -1.148912, 0.093725, 0.634745,
}) })
objPts.Clone(objPts.T()) objPts.TCopy(objPts)
imgPts := mat64.NewDense(10, 2, []float64{ imgPts := mat64.NewDense(10, 2, []float64{
-0.384281, -0.299055, -0.384281, -0.299055,
@ -76,7 +76,7 @@ func TestGcvCalibrateCamera(t *testing.T) {
0.631444, -0.340257, 0.631444, -0.340257,
-0.647580, 0.502113, -0.647580, 0.502113,
}) })
imgPts.Clone(imgPts.T()) imgPts.TCopy(imgPts)
camMat := GcvInitCameraMatrix2D(objPts, imgPts, [2]int{1920, 1080}, 1) camMat := GcvInitCameraMatrix2D(objPts, imgPts, [2]int{1920, 1080}, 1)
@ -85,12 +85,12 @@ func TestGcvCalibrateCamera(t *testing.T) {
camMat, rvec, tvec := GcvCalibrateCamera( camMat, rvec, tvec := GcvCalibrateCamera(
objPts, imgPts, camMat, distCoeffs, [2]int{1920, 1080}, 14575) objPts, imgPts, camMat, distCoeffs, [2]int{1920, 1080}, 14575)
assert.InDeltaSlice(t, []float64{-46.15296606, 0., 959.5}, mat64.Row(nil, 0, camMat), DELTA) assert.InDeltaSlice(t, []float64{-46.15296606, 0., 959.5}, camMat.Row(nil, 0), DELTA)
assert.InDeltaSlice(t, []float64{0., -46.15296606, 539.5}, mat64.Row(nil, 1, camMat), DELTA) assert.InDeltaSlice(t, []float64{0., -46.15296606, 539.5}, camMat.Row(nil, 1), DELTA)
assert.InDeltaSlice(t, []float64{0., 0., 1.}, mat64.Row(nil, 2, camMat), DELTA) assert.InDeltaSlice(t, []float64{0., 0., 1.}, camMat.Row(nil, 2), DELTA)
assert.InDeltaSlice(t, []float64{-0.98405029, -0.93443411, -0.26304667}, mat64.Col(nil, 0, rvec), DELTA) assert.InDeltaSlice(t, []float64{-0.98405029, -0.93443411, -0.26304667}, rvec.Col(nil, 0), DELTA)
assert.InDeltaSlice(t, []float64{0.6804739, 0.47530207, -0.04833094}, mat64.Col(nil, 0, tvec), DELTA) assert.InDeltaSlice(t, []float64{0.6804739, 0.47530207, -0.04833094}, tvec.Col(nil, 0), DELTA)
} }
func TestGcvRodrigues(t *testing.T) { func TestGcvRodrigues(t *testing.T) {
@ -101,7 +101,7 @@ func TestGcvRodrigues(t *testing.T) {
}) })
rmat := GcvRodrigues(rvec) rmat := GcvRodrigues(rvec)
assert.InDeltaSlice(t, []float64{0.59922526, 0.57799222, -0.55394411}, mat64.Row(nil, 0, rmat), DELTA) assert.InDeltaSlice(t, []float64{0.59922526, 0.57799222, -0.55394411}, rmat.Row(nil, 0), DELTA)
assert.InDeltaSlice(t, []float64{0.20413818, 0.558743, 0.80382452}, mat64.Row(nil, 1, rmat), DELTA) assert.InDeltaSlice(t, []float64{0.20413818, 0.558743, 0.80382452}, rmat.Row(nil, 1), DELTA)
assert.InDeltaSlice(t, []float64{0.77411672, -0.5947531, 0.21682264}, mat64.Row(nil, 2, rmat), DELTA) assert.InDeltaSlice(t, []float64{0.77411672, -0.5947531, 0.21682264}, rmat.Row(nil, 2), DELTA)
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.1 KiB

View file

@ -142,11 +142,6 @@ func (img *IplImage) Get3D(x, y, z int) Scalar {
return Scalar(ret) return Scalar(ret)
} }
/* Sets every element of an array to a given value. */
func (img *IplImage) Set(value Scalar) {
C.cvSet(unsafe.Pointer(img), (C.CvScalar)(value), nil)
}
/* Set1D sets a particular element in the image */ /* Set1D sets a particular element in the image */
func (img *IplImage) Set1D(x int, value Scalar) { func (img *IplImage) Set1D(x int, value Scalar) {
C.cvSet1D(unsafe.Pointer(img), C.int(x), (C.CvScalar)(value)) C.cvSet1D(unsafe.Pointer(img), C.int(x), (C.CvScalar)(value))
@ -545,25 +540,6 @@ func (src *IplImage) EqualizeHist(dst *IplImage) {
/****************************************************************************************\ /****************************************************************************************\
* Dynamic data structures * * Dynamic data structures *
\****************************************************************************************/ \****************************************************************************************/
func (seq *Seq) Release() {
C.cvReleaseMemStorage(&seq.storage)
}
func (seq *Seq) Total() int {
return (int)(seq.total)
}
func (seq *Seq) HNext() *Seq {
return (*Seq)(seq.h_next)
}
func (seq *Seq) VNext() *Seq {
return (*Seq)(seq.v_next)
}
func (seq *Seq) Storage() *MemStorage {
return (*MemStorage)(seq.storage)
}
/****************************************************************************************\ /****************************************************************************************\
* Drawing * * Drawing *

View file

@ -595,18 +595,6 @@ type Box2D struct {
angle float32 angle float32
} }
func (box *Box2D) Size() Size2D32f {
return box.size
}
func (box *Box2D) Center() Point2D32f {
return box.center
}
func (box *Box2D) Angle() float32 {
return box.angle
}
type LineIterator C.CvLineIterator type LineIterator C.CvLineIterator
/************************************* CvSlice ******************************************/ /************************************* CvSlice ******************************************/
@ -617,12 +605,6 @@ const (
CV_WHOLE_SEQ_END_INDEX = C.CV_WHOLE_SEQ_END_INDEX CV_WHOLE_SEQ_END_INDEX = C.CV_WHOLE_SEQ_END_INDEX
) )
/* Equivalent to the C constant CV_WHOLE_SEQ */
func WholeSeq() Slice {
slice := C.cvSlice(C.int(0), C.CV_WHOLE_SEQ_END_INDEX)
return (Slice)(slice)
}
/************************************* CvScalar *****************************************/ /************************************* CvScalar *****************************************/
type Scalar C.CvScalar type Scalar C.CvScalar
@ -679,18 +661,6 @@ type Graph C.CvGraph
type Chain C.CvChain type Chain C.CvChain
type Contour C.CvContour type Contour C.CvContour
const (
CV_RETR_EXTERNAL = C.CV_RETR_EXTERNAL
CV_RETR_LIST = C.CV_RETR_LIST
CV_RETR_CCOMP = C.CV_RETR_CCOMP
CV_RETR_TREE = C.CV_RETR_TREE
CV_CHAIN_APPROX_NONE = C.CV_CHAIN_APPROX_NONE
CV_CHAIN_APPROX_SIMPLE = C.CV_CHAIN_APPROX_SIMPLE
CV_CHAIN_APPROX_TC89_L1 = C.CV_CHAIN_APPROX_TC89_L1
CV_CHAIN_APPROX_TC89_KCOS = C.CV_CHAIN_APPROX_TC89_KCOS
)
/****************************************************************************************\ /****************************************************************************************\
* Sequence types * * Sequence types *
\****************************************************************************************/ \****************************************************************************************/
@ -724,15 +694,6 @@ const (
type AttrList C.CvAttrList type AttrList C.CvAttrList
/****************************************************************************************/
/* Structural Analysis and Shape Descriptors */
/****************************************************************************************/
/* For use in ApproxPoly */
const (
CV_POLY_APPROX_DP = C.CV_POLY_APPROX_DP
)
/*****************************************************************************\ /*****************************************************************************\
* --- END --- * * --- END --- *
\*****************************************************************************/ \*****************************************************************************/

View file

@ -52,78 +52,3 @@ func Crop(src *IplImage, x, y, width, height int) *IplImage {
return dest return dest
} }
/* Returns a Seq of countours in an image, detected according to the parameters.
Caller must Release() the Seq returned */
func (image *IplImage) FindContours(mode, method int, offset Point) *Seq {
storage := C.cvCreateMemStorage(0)
header_size := (C.size_t)(unsafe.Sizeof(C.CvContour{}))
var seq *C.CvSeq
C.cvFindContours(
unsafe.Pointer(image),
storage,
&seq,
C.int(header_size),
C.int(mode),
C.int(method),
C.cvPoint(C.int(offset.X), C.int(offset.Y)))
return (*Seq)(seq)
}
//cvDrawContours(CvArr* img, CvSeq* contour, CvScalar externalColor, CvScalar holeColor, int maxLevel, int thickness=1, int lineType=8
func DrawContours(image *IplImage, contours *Seq, externalColor, holeColor Scalar, maxLevel, thickness, lineType int, offset Point) {
C.cvDrawContours(
unsafe.Pointer(image),
(*C.CvSeq)(contours),
(C.CvScalar)(externalColor),
(C.CvScalar)(holeColor),
C.int(maxLevel),
C.int(thickness),
C.int(lineType),
C.cvPoint(C.int(offset.X), C.int(offset.Y)))
}
// CvSeq* cvApproxPoly(const void* src_seq, int header_size, CvMemStorage* storage, int method, double eps, int recursive=0 )
func ApproxPoly(src *Seq, header_size int, storage *MemStorage, method int, eps float64, recursive int) *Seq {
seq := C.cvApproxPoly(
unsafe.Pointer(src),
C.int(header_size),
(*C.CvMemStorage)(storage),
C.int(method),
C.double(eps),
C.int(recursive))
return (*Seq)(seq)
}
// cvArcLength(const void* curve, CvSlice slice=CV_WHOLE_SEQ, int is_closed=-1 )
func ArcLength(curve *Seq, slice Slice, is_closed bool) float64 {
is_closed_int := 0
if is_closed {
is_closed_int = 1
}
return float64(C.cvArcLength(unsafe.Pointer(curve),
(C.CvSlice)(slice),
C.int(is_closed_int)))
}
func ContourPerimeter(curve *Seq) float64 {
return ArcLength(curve, WholeSeq(), true)
}
// double cvContourArea(const CvArr* contour, CvSlice slice=CV_WHOLE_SEQ, int oriented=0 )
func ContourArea(contour *Seq, slice Slice, oriented int) float64 {
return float64(C.cvContourArea(
unsafe.Pointer(contour),
(C.CvSlice)(slice),
C.int(oriented)))
}
/* points can be either CvSeq* or CvMat* */
func FitEllipse2(points unsafe.Pointer) Box2D {
box := C.cvFitEllipse2(points)
center := Point2D32f{float32(box.center.x), float32(box.center.y)}
size := Size2D32f{float32(box.size.width), float32(box.size.height)}
angle := float32(box.angle)
return Box2D{center, size, angle}
}

View file

@ -1,10 +1,8 @@
package opencv package opencv
import ( import (
"os"
"path" "path"
"runtime" "runtime"
"syscall"
"testing" "testing"
) )
@ -57,56 +55,3 @@ func TestCrop(t *testing.T) {
t.Fatalf("excepted width is 200, returned %d\n", crop.Height()) t.Fatalf("excepted width is 200, returned %d\n", crop.Height())
} }
} }
func TestFindContours(t *testing.T) {
_, currentfile, _, _ := runtime.Caller(0)
filename := path.Join(path.Dir(currentfile), "../images/pic5.png")
image := LoadImage(filename)
if image == nil {
t.Fatal("LoadImage fail")
}
defer image.Release()
grayscale := CreateImage(image.Width(), image.Height(), IPL_DEPTH_8U, 1)
CvtColor(image, grayscale, CV_BGR2GRAY)
defer grayscale.Release()
edges := CreateImage(grayscale.Width(), grayscale.Height(), grayscale.Depth(), grayscale.Channels())
defer edges.Release()
Canny(grayscale, edges, 50, 200, 3)
seq := edges.FindContours(CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point{0, 0})
defer seq.Release()
contours := CreateImage(grayscale.Width(), grayscale.Height(), grayscale.Depth(), grayscale.Channels())
white := NewScalar(255, 255, 255, 0)
contours.Set(white)
black := NewScalar(0, 0, 0, 0)
red := NewScalar(0, 255, 0, 0)
for ; seq != nil; seq = seq.HNext() {
DrawContours(contours, seq, red, black, 0, 2, 8, Point{0, 0})
}
filename = path.Join(path.Dir(currentfile), "../images/pic5_contours.png")
// Uncomment this code to create the test image "../images/shapes_contours.png"
// It is part of the repo, and what this test compares against
//
//SaveImage(filename), contours, 0)
tempfilename := path.Join(os.TempDir(), "pic5_contours.png")
defer syscall.Unlink(tempfilename)
SaveImage(tempfilename, contours, 0)
// Compare actual image with expected image
same, err := BinaryCompare(filename, tempfilename)
if err != nil {
t.Fatal(err)
}
if !same {
t.Error("Expected contour file != actual contour file")
}
}

View file

@ -1,54 +0,0 @@
package main
import (
"fmt"
"os"
"path"
"github.com/lazywei/go-opencv/opencv"
)
func main() {
win := opencv.NewWindow("Go-OpenCV Webcam Face Detection")
defer win.Destroy()
cap := opencv.NewCameraCapture(0)
if cap == nil {
panic("cannot open camera")
}
defer cap.Release()
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
cascade := opencv.LoadHaarClassifierCascade(path.Join(cwd, "haarcascade_frontalface_alt.xml"))
fmt.Println("Press ESC to quit")
for {
if cap.GrabFrame() {
img := cap.RetrieveFrame(1)
if img != nil {
faces := cascade.DetectObjects(img)
for _, value := range faces {
opencv.Circle(img,
opencv.Point{
value.X() + (value.Width() / 2),
value.Y() + (value.Height() / 2),
},
value.Width()/2,
opencv.ScalarAll(255.0), 1, 1, 0)
}
win.ShowImage(img)
} else {
fmt.Println("nil image")
}
}
key := opencv.WaitKey(1)
if key == 27 {
os.Exit(0)
}
}
}