func calcDeltaEnergy(flow *C.IplImage, config *Configuration) float64 { var i C.int var dx, dy, mx, my float64 totalPixels := flow.width * flow.height // Determine mean movement vector. for i = 0; i < totalPixels; i++ { value := C.cvGet2D(unsafe.Pointer(flow), i/flow.width, i%flow.width) mx += float64(value.val[0]) my += float64(value.val[1]) } mx = math.Abs(mx / float64(totalPixels)) my = math.Abs(my / float64(totalPixels)) // Accumulate the change in flow across all the pixels. for i = 0; i < totalPixels; i++ { // Remove the mean movement vector to compenstate for the sculpture that might be swaying in the wind. value := C.cvGet2D(unsafe.Pointer(flow), i/flow.width, i%flow.width) dx += math.Max((math.Abs(float64(value.val[0])) - mx), 0.0) dy += math.Max((math.Abs(float64(value.val[1])) - my), 0.0) } // average out the magnitude of dx and dy across the whole image. dx = dx / float64(totalPixels) dy = dy / float64(totalPixels) // The magnitude of accumulated flow forms our change in energy for the frame. deltaE := math.Sqrt((dx * dx) + (dy * dy)) fmt.Printf("INFO: f:%f m:[%f,%f]\n", deltaE, mx, my) // Clamp the energy to start at 0 for 'still' frames with little/no motion. deltaE = math.Max(0.0, (deltaE - config.MovementThreshold)) // Scale the flow to be less than 0.1 for 'active' frames with lots of motion. deltaE = deltaE / config.OpticalFlowScale return deltaE }
func (img *Image) ScalarAt(pos ...int) Scalar { n := len(pos) var s C.CvScalar if n == 1 { s = C.cvGet1D(img.ptr, C.int(pos[0])) } else if n == 2 { s = C.cvGet2D(img.ptr, C.int(pos[0]), C.int(pos[1])) } else if n == 3 { s = C.cvGet3D(img.ptr, C.int(pos[0]), C.int(pos[1]), C.int(pos[2])) } return Scalar{float64(s.val[0]), float64(s.val[1]), float64(s.val[2]), float64(s.val[3])} }
/* Get2D return a specific element from a 2-dimensional matrix. */ func (m *Mat) Get2D(x, y int) Scalar { ret := C.cvGet2D(unsafe.Pointer(m), C.int(x), C.int(y)) return Scalar(ret) }
/* Get2D return a specific element from a 2-dimensional matrix. */ func (img *IplImage) Get2D(x, y int) Scalar { ret := C.cvGet2D(unsafe.Pointer(img), C.int(y), C.int(x)) return Scalar(ret) }