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) }
/* utility function: convert one image to another with optional vertical flip */ func ConvertImage(src, dst unsafe.Pointer, flags int) { C.cvConvertImage(src, dst, C.int(flags)) }