Skip to content

Commit

Permalink
Use Close() function for cleaning, thanks @maruel for the suggestion
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Sep 27, 2017
1 parent ece17da commit cb163df
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 52 deletions.
12 changes: 6 additions & 6 deletions core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Mat Mat_New() {
return new cv::Mat();
}

// Mat_Delete deletes an existing Mat
void Mat_Delete(Mat m) {
// Mat_Close deletes an existing Mat
void Mat_Close(Mat m) {
delete m;
}

Expand All @@ -16,6 +16,10 @@ int Mat_Empty(Mat m) {
return m->empty();
}

void Rects_Close(struct Rects rs) {
delete rs.rects;
}

void DrawRectsToImage(Mat img, struct Rects rects) {
for (int i = 0; i < rects.length; ++i) {
Rect r = rects.rects[i];
Expand All @@ -33,7 +37,3 @@ struct ByteArray toByteArray(const char* buf, int len) {
memcpy(ret.data, buf, len);
return ret;
}

void Rects_Delete(struct Rects rs) {
delete rs.rects;
}
8 changes: 4 additions & 4 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func NewMat() Mat {
return Mat{p: C.Mat_New()}
}

// Delete object.
func (m *Mat) Delete() {
C.Mat_Delete(m.p)
// Close the Mat object.
func (m *Mat) Close() {
C.Mat_Close(m.p)
m.p = nil
}

// Delete object.
// Ptr returns the Mat's underlying object pointer.
func (m *Mat) Ptr() C.Mat {
return m.p
}
Expand Down
4 changes: 2 additions & 2 deletions core.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ extern "C" {
struct ByteArray toByteArray(const char* buf, int len);
void ByteArray_Release(struct ByteArray buf);

void Rects_Delete(struct Rects rs);
void Rects_Close(struct Rects rs);
void DrawRectsToImage(Mat img, struct Rects rects);

Mat Mat_New();
void Mat_Delete(Mat m);
void Mat_Close(Mat m);
int Mat_Empty(Mat m);

#ifdef __cplusplus
Expand Down
5 changes: 3 additions & 2 deletions examples/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ import (
func main() {
deviceID := 0
webcam := opencv3.NewVideoCapture()
defer webcam.Delete()
defer webcam.Close()

if ok := webcam.OpenDevice(int(deviceID)); !ok {
fmt.Printf("error opening device: %v\n", deviceID)
}

// streaming, capture from webcam
buf := opencv3.NewMat()
defer buf.Delete()
defer buf.Close()

fmt.Printf("start reading camera device: %v\n", deviceID)
for {
if ok := webcam.Read(buf); !ok {
Expand Down
7 changes: 4 additions & 3 deletions examples/capwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ import (
func main() {
deviceID := 0
webcam := opencv3.NewVideoCapture()
defer webcam.Delete()
defer webcam.Close()

if ok := webcam.OpenDevice(int(deviceID)); !ok {
fmt.Printf("error opening device: %v\n", deviceID)
return
}

window := opencv3.NewWindow("Capture")

defer window.Close()

img := opencv3.NewMat()
defer img.Delete()
defer img.Close()

fmt.Printf("start reading camera device: %v\n", deviceID)
for {
Expand Down
7 changes: 5 additions & 2 deletions examples/facedetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ func main() {
xmlFile := os.Args[2]

webcam := opencv3.NewVideoCapture()
defer webcam.Delete()
defer webcam.Close()

if ok := webcam.OpenDevice(deviceID); !ok {
fmt.Printf("error opening device: %v\n", deviceID)
return
}

window := opencv3.NewWindow("Capture")
defer window.Close()

img := opencv3.NewMat()
defer img.Delete()
defer img.Close()

classifier := opencv3.NewCascadeClassifier()
defer classifier.Close()

classifier.Load(xmlFile)

fmt.Printf("start reading camera device: %v\n", deviceID)
Expand Down
7 changes: 4 additions & 3 deletions examples/pvl_facedetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ func main() {
deviceID, _ := strconv.Atoi(os.Args[1])

webcam := opencv3.NewVideoCapture()
defer webcam.Delete()
defer webcam.Close()

if ok := webcam.OpenDevice(deviceID); !ok {
fmt.Printf("error opening device: %v\n", deviceID)
return
}

window := opencv3.NewWindow("Capture")

defer window.Close()

img := opencv3.NewMat()
defer img.Delete()
defer img.Close()

fd := pvl.NewFaceDetector()
defer fd.Close()
Expand Down
4 changes: 2 additions & 2 deletions examples/saveimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func main() {
saveFile := os.Args[2]

webcam := opencv3.NewVideoCapture()
defer webcam.Delete()
defer webcam.Close()

if ok := webcam.OpenDevice(deviceID); !ok {
fmt.Printf("error opening device: %v\n", deviceID)
}

img := opencv3.NewMat()
defer img.Delete()
defer img.Close()

if ok := webcam.Read(img); !ok {
fmt.Printf("cannot read device %d\n", deviceID)
Expand Down
6 changes: 3 additions & 3 deletions examples/savevideo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ func main() {
saveFile := os.Args[2]

webcam := opencv3.NewVideoCapture()
defer webcam.Delete()
defer webcam.Close()

writer := opencv3.NewVideoWriter()
defer writer.Delete()
defer writer.Close()

if ok := webcam.OpenDevice(deviceID); !ok {
fmt.Printf("error opening device: %v\n", deviceID)
}

img := opencv3.NewMat()
defer img.Delete()
defer img.Close()

if ok := webcam.Read(img); !ok {
fmt.Printf("cannot read device %d\n", deviceID)
Expand Down
2 changes: 0 additions & 2 deletions examples/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package main

import (
"os"
"time"

opencv3 ".."
)
Expand All @@ -26,6 +25,5 @@ func main() {
for {
window.IMShow(img)
opencv3.WaitKey(1)
time.Sleep(100 * time.Microsecond)
}
}
2 changes: 1 addition & 1 deletion highgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ void Window_New(const char* winname, int flags) {
cv::namedWindow(winname, flags);
}

void Window_Delete(const char* winname) {
void Window_Close(const char* winname) {
cv::destroyWindow(winname);
}

Expand Down
6 changes: 3 additions & 3 deletions highgui.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func NewWindow(name string) *Window {
return &Window{name: name}
}

// Delete a specific Window
func (w *Window) Delete() {
// Close deletes a specific Window
func (w *Window) Close() {
cName := C.CString(w.name)
defer C.free(unsafe.Pointer(cName))

C.Window_Delete(cName)
C.Window_Close(cName)
}

// IMShow takes an image Mat and displays it in the Window
Expand Down
2 changes: 1 addition & 1 deletion highgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern "C" {

// Window
void Window_New(const char* winname, int flags);
void Window_Delete(const char* winname);
void Window_Close(const char* winname);
void Window_IMShow(const char* winname, Mat mat);
int Window_WaitKey(int);

Expand Down
2 changes: 1 addition & 1 deletion objdetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CascadeClassifier CascadeClassifier_New() {
return new cv::CascadeClassifier();
}

void CascadeClassifier_Delete(CascadeClassifier cs) {
void CascadeClassifier_Close(CascadeClassifier cs) {
delete cs;
}

Expand Down
12 changes: 6 additions & 6 deletions objdetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func NewCascadeClassifier() CascadeClassifier {
return CascadeClassifier{p: C.CascadeClassifier_New()}
}

// Delete CascadeClassifier's pointer.
func (c *CascadeClassifier) Delete() {
C.CascadeClassifier_Delete(c.p)
// Close deletes the CascadeClassifier's pointer.
func (c *CascadeClassifier) Close() {
C.CascadeClassifier_Close(c.p)
c.p = nil
}

Expand All @@ -37,7 +37,7 @@ func (c *CascadeClassifier) Load(name string) bool {
// multi results addressed with rectangle.
func (c *CascadeClassifier) DetectMultiScale(img Mat) []Rect {
ret := C.CascadeClassifier_DetectMultiScale(c.p, img.p)
defer C.Rects_Delete(ret)
defer C.Rects_Close(ret)

cArray := ret.rects
length := int(ret.length)
Expand All @@ -46,10 +46,10 @@ func (c *CascadeClassifier) DetectMultiScale(img Mat) []Rect {
Len: length,
Cap: length,
}
goSlice := *(*[]C.Rect)(unsafe.Pointer(&hdr))
s := *(*[]C.Rect)(unsafe.Pointer(&hdr))

rects := make([]Rect, length)
for i, r := range goSlice {
for i, r := range s {
rects[i] = Rect{
X: int(r.x),
Y: int(r.y),
Expand Down
2 changes: 1 addition & 1 deletion objdetect.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ typedef void* CascadeClassifier;

// CascadeClassifier
CascadeClassifier CascadeClassifier_New();
void CascadeClassifier_Delete(CascadeClassifier cs);
void CascadeClassifier_Close(CascadeClassifier cs);
int CascadeClassifier_Load(CascadeClassifier cs, const char* name);
struct Rects CascadeClassifier_DetectMultiScale(CascadeClassifier cs, Mat img);

Expand Down
4 changes: 2 additions & 2 deletions videoio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VideoCapture VideoCapture_New() {
return new cv::VideoCapture();
}

void VideoCapture_Delete(VideoCapture v) {
void VideoCapture_Close(VideoCapture v) {
delete v;
}

Expand Down Expand Up @@ -44,7 +44,7 @@ VideoWriter VideoWriter_New() {
return new cv::VideoWriter();
}

void VideoWriter_Delete(VideoWriter vw) {
void VideoWriter_Close(VideoWriter vw) {
delete vw;
}

Expand Down
12 changes: 6 additions & 6 deletions videoio.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func NewVideoCapture() VideoCapture {
return VideoCapture{p: C.VideoCapture_New()}
}

// Delete object.
func (v *VideoCapture) Delete() {
C.VideoCapture_Delete(v.p)
// Close VideoCapture object.
func (v *VideoCapture) Close() {
C.VideoCapture_Close(v.p)
v.p = nil
}

Expand Down Expand Up @@ -76,9 +76,9 @@ func NewVideoWriter() VideoWriter {
return VideoWriter{p: C.VideoWriter_New()}
}

// Delete object.
func (vw *VideoWriter) Delete() {
C.VideoWriter_Delete(vw.p)
// Close VideoWriter object.
func (vw *VideoWriter) Close() {
C.VideoWriter_Close(vw.p)
vw.p = nil
}

Expand Down
4 changes: 2 additions & 2 deletions videoio.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ typedef void* VideoWriter;

// VideoCapture
VideoCapture VideoCapture_New();
void VideoCapture_Delete(VideoCapture v);
void VideoCapture_Close(VideoCapture v);
int VideoCapture_Open(VideoCapture v, const char* uri);
int VideoCapture_OpenDevice(VideoCapture v, int device);
void VideoCapture_Release(VideoCapture v);
Expand All @@ -29,7 +29,7 @@ void VideoCapture_Grab(VideoCapture v, int skip);

// VideoWriter
VideoWriter VideoWriter_New();
void VideoWriter_Delete(VideoWriter vw);
void VideoWriter_Close(VideoWriter vw);
void VideoWriter_Open(VideoWriter vw, const char* name, double fps, int width,
int height);
void VideoWriter_OpenWithMat(VideoWriter vw, const char* name, double fps,
Expand Down

0 comments on commit cb163df

Please sign in to comment.