func CaptureFromCam(device int) (cap Capture, err error) { cap.handler = C.cvCaptureFromCAM(C.int(device)) if cap.handler == nil { err = errors.New("Could not open capture device") } return }
func dendriteCam(deltaE chan float32, config Configuration) { camera := C.cvCaptureFromCAM(-1) // Shutdown dendrite if no camera detected. if camera == nil { fmt.Printf("WARNING: No camera detected. Shutting down DendriteCam\n") return } C.cvSetCaptureProperty(camera, C.CV_CAP_PROP_FRAME_WIDTH, 160) C.cvSetCaptureProperty(camera, C.CV_CAP_PROP_FRAME_HEIGHT, 120) // Capture original frame. prev := C.cvCloneImage(C.cvQueryFrame(camera)) // Save out the first frame for debuging purposes. file := C.CString("frame.png") C.cvSaveImage(file, unsafe.Pointer(prev), nil) C.free(unsafe.Pointer(file)) flow := C.cvCreateImage(C.cvSize(prev.width, prev.height), C.IPL_DEPTH_32F, 2) prevG := C.cvCreateImage(C.cvSize(prev.width, prev.height), C.IPL_DEPTH_8U, 1) nextG := C.cvCreateImage(C.cvSize(prev.width, prev.height), C.IPL_DEPTH_8U, 1) C.cvConvertImage(unsafe.Pointer(prev), unsafe.Pointer(prevG), 0) for { C.cvGrabFrame(camera) // Capture the new frame and convert it to grayscale. next := C.cvCloneImage(C.cvQueryFrame(camera)) C.cvConvertImage(unsafe.Pointer(prev), unsafe.Pointer(prevG), 0) C.cvConvertImage(unsafe.Pointer(next), unsafe.Pointer(nextG), 0) C.cvCalcOpticalFlowFarneback(unsafe.Pointer(prevG), unsafe.Pointer(nextG), unsafe.Pointer(flow), 0.5, 2, 5, 2, 5, 1.1, 0) deltaE <- float32(calcDeltaEnergy(flow, &config)) C.cvReleaseImage(&prev) prev = next } // Never reached - but here in comments for completeness. //C.cvReleaseImage(&prev) //C.cvReleaseImage(&nextG) //C.cvReleaseImage(&prevG) //C.cvReleaseImage(&flow) //C.cvReleaseCapture(&camera) }
func NewCaptureFromCAM(index int) *Capture { cap := C.cvCaptureFromCAM(C.int(index)) return (*Capture)(cap) }