func ProcessRoi(img *opencv.IplImage, gradeOfCanny int) *opencv.IplImage { var roi = img.GetROI() w := roi.Width() h := roi.Height() // Create the output image cedge := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 3) //defer cedge.Release() // Convert to grayscale gray := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 1) edge := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 1) //newimage := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 1) defer gray.Release() defer edge.Release() opencv.CvtColor(img, gray, opencv.CV_BGR2GRAY) opencv.Smooth(gray, edge, opencv.CV_BLUR, 3, 3, 0, 0) opencv.Not(gray, edge) // Run the edge detector on grayscale opencv.Canny(gray, edge, float64(gradeOfCanny), float64(gradeOfCanny*3), 3) opencv.Zero(cedge) // copy edge points opencv.Copy(img, cedge, edge) return cedge }
func ProcessImage(img *opencv.IplImage, win *opencv.Window, pos int) error { w := img.Width() h := img.Height() // Create the output image cedge := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 3) defer cedge.Release() // Convert to grayscale gray := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 1) edge := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 1) defer gray.Release() defer edge.Release() opencv.CvtColor(img, gray, opencv.CV_BGR2GRAY) opencv.Smooth(gray, edge, opencv.CV_BLUR, 3, 3, 0, 0) opencv.Not(gray, edge) // Run the edge detector on grayscale opencv.Canny(gray, edge, float64(pos), float64(pos*3), 3) opencv.Zero(cedge) // copy edge points opencv.Copy(img, cedge, edge) win.ShowImage(img) return nil }
func main() { _, currentfile, _, _ := runtime.Caller(0) cascade := path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml") window := opencv.NewWindowDriver() camera := opencv.NewCameraDriver(0) work := func() { var image *cv.IplImage camera.On(opencv.Frame, func(data interface{}) { image = data.(*cv.IplImage) }) gobot.Every(500*time.Millisecond, func() { if image != nil { i := image.Clone() faces := opencv.DetectFaces(cascade, i) i = opencv.DrawRectangles(i, faces, 0, 255, 0, 5) window.ShowImage(i) } }) } robot := gobot.NewRobot("faceBot", []gobot.Connection{}, []gobot.Device{window, camera}, work, ) robot.Start() }
func ToByteBuffer(image *opencv.IplImage) []byte { buf := new(bytes.Buffer) err := jpeg.Encode(buf, image.ToImage(), nil) if err != nil { panic(err) } return buf.Bytes() }
func (detector *FaceDetector) DetectGlasses(image *opencv.IplImage, roi pixelArea) (leftEyes, rightEyes pixelCoords) { var topFace opencv.Rect var eyes pixelCoords topFace.Init(roi.X(), roi.Y()+int(float64(roi.Height())*0.20), roi.Width(), int(float64(roi.Height())/2)) image.SetROI(topFace) for _, eye := range detector.glassesCascade.DetectObjects(image) { leftEyes = append(leftEyes, pixelCoord{ x: eye.X() + topFaceLeft.X(), y: eye.Y() + topFaceLeft.Y(), width: eye.Width(), height: eye.Height(), }) } fmt.Println(len(leftEyes), " left eyes found") image.SetROI(topFaceRight) for _, eye := range detector.glassesCascade.DetectObjects(image) { rightEyes = append(rightEyes, pixelCoord{ x: eye.X() + topFaceRight.X(), y: eye.Y() + topFaceRight.Y(), width: eye.Width(), height: eye.Height(), }) } fmt.Println(len(rightEyes), " right eyes found") image.ResetROI() return }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) _, currentfile, _, _ := runtime.Caller(0) cascade := path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml") window := opencv.NewWindowDriver() camera := opencv.NewCameraDriver("tcp://192.168.1.1:5555") ardroneAdaptor := ardrone.NewAdaptor() drone := ardrone.NewDriver(ardroneAdaptor) work := func() { detect := false drone.TakeOff() var image *cv.IplImage camera.On(opencv.Frame, func(data interface{}) { image = data.(*cv.IplImage) if !detect { window.ShowImage(image) } }) drone.On(ardrone.Flying, func(data interface{}) { gobot.After(1*time.Second, func() { drone.Up(0.2) }) gobot.After(2*time.Second, func() { drone.Hover() }) gobot.After(5*time.Second, func() { detect = true gobot.Every(300*time.Millisecond, func() { drone.Hover() i := image faces := opencv.DetectFaces(cascade, i) biggest := 0 var face *cv.Rect for _, f := range faces { if f.Width() > biggest { biggest = f.Width() face = f } } if face != nil { opencv.DrawRectangles(i, []*cv.Rect{face}, 0, 255, 0, 5) centerX := float64(image.Width()) * 0.5 turn := -(float64(face.X()) - centerX) / centerX fmt.Println("turning:", turn) if turn < 0 { drone.Clockwise(math.Abs(turn * 0.4)) } else { drone.CounterClockwise(math.Abs(turn * 0.4)) } } window.ShowImage(i) }) gobot.After(20*time.Second, func() { drone.Land() }) }) }) } robot := gobot.NewRobot("face", []gobot.Connection{ardroneAdaptor}, []gobot.Device{window, camera, drone}, work, ) robot.Start() }
func (detector *FaceDetector) DetectFacialFeatures(image *opencv.IplImage, faceCoords []pixelCoord) faces { var faces = make(faces, 0) for _, faceCoord := range faceCoords { //Lets find us some eyes leftEyes, rightEyes := detector.DetectEyes(image, &faceCoord) if len(leftEyes) == 0 || len(rightEyes) == 0 { //No eyes found, lets flop the picture and check again fmt.Println("Flopping Picture to look for eyes") floppedImage := opencv.DecodeImageMem(FlopImage(ToByteBuffer(image))) floppedFaceCoords := faceCoord floppedFaceCoords.Flop(pixelCoord{width: image.Width(), height: image.Height()}) floppedLeftEyes, floppedRightEyes := detector.DetectEyes(floppedImage, &floppedFaceCoords) if len(leftEyes) == 0 { floppedRightEyes.FlopEmAll(pixelCoord{width: image.Width(), height: image.Height()}) leftEyes = append(leftEyes, floppedRightEyes...) } if len(rightEyes) == 0 { floppedLeftEyes.FlopEmAll(pixelCoord{width: image.Width(), height: image.Height()}) leftEyes = append(rightEyes, floppedLeftEyes...) } } leftEye, rightEye := FindBestEyes(leftEyes, rightEyes) faces = append(faces, face{ coord: faceCoord, eye_left: leftEye, eye_right: rightEye, }) } fmt.Println(faces) return faces }
func ToImage(img *opencv.IplImage) image.Image { out := image.NewNRGBA(image.Rect(0, 0, img.Width(), img.Height())) if img.Depth() != opencv.IPL_DEPTH_8U { return nil // TODO return error } for y := 0; y < img.Height(); y++ { for x := 0; x < img.Width(); x++ { s := img.Get2D(x, y).Val() b, g, r, a := s[2], s[1], s[0], s[3] c := color.NRGBA{uint8(b), uint8(g), uint8(r), uint8(a)} out.Set(x, y, c) } } return out }
func HoughCirclesWithParams(img *opencv.IplImage, dp float64, min_dist float64, param_1 float64, param_2 float64, min_radius int, maxradius int) []opencv.CircleStruct { var gray = opencv.CreateImage(img.Width(), img.Height(), opencv.IPL_DEPTH_8U, 1) opencv.CvtColor(img, gray, opencv.CV_BGR2GRAY) return opencv.HoughCircles(gray, dp, min_dist, param_1, param_2, 1, 500) }
func HoughCircles1(img *opencv.IplImage) []opencv.CircleStruct { var gray = opencv.CreateImage(img.Width(), img.Height(), opencv.IPL_DEPTH_8U, 1) opencv.CvtColor(img, gray, opencv.CV_BGR2GRAY) return opencv.HoughCircles(gray, 15, 100, 75, 5, 25, 500) }
func cloneGreyscale(im *opencv.IplImage) *opencv.IplImage { w, h := im.Width(), im.Height() g := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 1) opencv.CvtColor(im, g, opencv.CV_BGR2GRAY) return g }