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)
}
Exemple #2
0
func calibrate(videoFile string, config Configuration) {
	// It takes a little while for the white balance to stabalise on the logitech.
	// So grab a frame, wait to stabalise for white balance to stabalise, then grab again
	// for and save as the calibration frame.
	camera, err := getVideoSource(videoFile)
	if err != nil {
		// No valid video source, abort.
		log.Printf("ERROR: Unable to get video source")
		log.Print(err)
		return
	}
	calibrationFrame := C.cvQueryFrame(camera)
	time.Sleep(1250 * time.Millisecond)
	C.cvReleaseCapture(&camera)

	camera, err = getVideoSource(videoFile)
	if err != nil {
		// No valid video source, abort
		log.Printf("ERROR: Unable to get video source")
		log.Print(err)
		return
	}
	defer C.cvReleaseCapture(&camera)

	// Build the calibration image from the first frame that comes off the camera.
	calibrationFrame = C.cvQueryFrame(camera)
	fileName := "calibrationFrame.jpg"
	file := C.CString(fileName)
	C.cvSaveImage(file, unsafe.Pointer(calibrationFrame), nil)
	C.free(unsafe.Pointer(file))

	// Broadcast calibration results to the mothership.
	f, err := os.Open(fileName)
	if err != nil {
		log.Printf("ERROR: Unable to open calibration frame to broadcast")
		log.Print(err)
		return
	}
	defer f.Close()
	post(fileName, config.MothershipAddress+"/scout_api/calibrated", config.UUID, f)
}
Exemple #3
0
/* Just a combination of cvGrabFrame and cvRetrieveFrame
   !!!DO NOT RELEASE or MODIFY the retrieved frame!!!      */
func (capture *Capture) QueryFrame() *IplImage {
	rv := C.cvQueryFrame((*C.CvCapture)(capture))
	return (*IplImage)(rv)
}
Exemple #4
0
func (cap *Capture) QueryFrame() (*Image, error) {
	iplImage := C.cvQueryFrame(cap.handler)
	return imageFromIplImage(iplImage)
}