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 CaptureFrames() { var cam = opencv.NewCameraCapture(0) defer cam.Release() frames := []*opencv.IplImage{} //var writter = opencv.NewVideoWriter("test.avi", int(opencv.FOURCC('D','I','V','3')),20, 50, 50, 1) win := opencv.NewWindow("Go-OpenCV Webcam123") defer win.Destroy() for framesCounter := 0; framesCounter <= 25; framesCounter++ { log.Print(framesCounter) if cam.GrabFrame() || framesCounter > 25 { // var frame = cam.RetrieveFrame(1) // var img = ProcessImage(frame, 50) // frames = append(frames, img) var frame = cam.RetrieveFrame(1) win.ShowImage(frame) // var img3 = frame.ToImage() var imageHeader = opencv.CreateImage(frame.Width(), frame.Height(), frame.Depth(), frame.Channels()) for i := 0; i < frame.Width(); i++ { for j := 0; j < frame.Height(); j++ { scalar := frame.Get2D(i, j) var bgra = scalar.Val() var newscalar = opencv.NewScalar(bgra[0], bgra[1], bgra[2], 1) imageHeader.Set2D(i, j, newscalar) } } // var img3Opencv = opencv.FromImage(img3) var copy = opencv.CreateImage(frame.Width(), frame.Height(), frame.Depth(), frame.Channels()) opencv.Copy(frame, copy, nil) // frames = append(frames, img3Opencv) //win.ShowImage(imageHeader) } } var videoWritter = NewVideoWriter(opencv.FOURCC('D', 'I', 'V', '3'), 20, opencv.Size{frames[0].Width(), frames[0].Height()}) videoWritter.SaveAsVideo(frames, "test.avi") }
func TrackEyes(srcFile, destFile string) { var video = opencv.NewFileCapture(srcFile) var fourcc = opencv.FOURCC('m', 'p', '4', 'v') var fps = float32(video.GetProperty(opencv.CV_CAP_PROP_FPS)) var size = opencv.Size{int(video.GetProperty(opencv.CV_CAP_PROP_FRAME_WIDTH)), int(video.GetProperty(opencv.CV_CAP_PROP_FRAME_HEIGHT))} if video == nil { panic("no file found with name: " + srcFile) } defer video.Release() win := opencv.NewWindow("Go-OpenCV Webcam") defer win.Destroy() _, currentfile, _, _ := runtime.Caller(0) var cascade = path.Join(path.Dir(currentfile), "/../cascades/haarcascade_eye_tree_eyeglasses.xml") var eyeMarker = NewEyeMarker(cascade) var imgs = []*opencv.IplImage{} var imgs2 = []*opencv.IplImage{} for { if video.GrabFrame() { img := video.RetrieveFrame(1) if img != nil { var copy = opencv.CreateImage(img.Width(), img.Height(), img.Depth(), img.Channels()) opencv.Copy(img.Clone(), copy, nil) var markedImg = eyeMarker.MarkEyes(copy) imgs = append(imgs, markedImg) //win.ShowImage(imgs[len(imgs)-1]) } else { fmt.Println("Image ins nil") } } else { break } key := opencv.WaitKey(10) if key == 27 { os.Exit(0) } } for i := 0; i < len(imgs2); i++ { win.ShowImage(imgs2[i]) } log.Print("before write") NewVideoWriter(uint32(fourcc), fps, size).SaveAsVideo(imgs, "test.avi") os.Exit(0) }
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 main() { filename := "../images/fruits.jpg" if len(os.Args) == 2 { filename = os.Args[1] } img0 := opencv.LoadImage(filename) if img0 == nil { panic("LoadImage fail") } defer img0.Release() fmt.Print("Hot keys: \n", "\tESC - quit the program\n", "\tr - restore the original image\n", "\ti or ENTER - run inpainting algorithm\n", "\t\t(before running it, paint something on the image)\n", ) img := img0.Clone() inpainted := img0.Clone() inpaint_mask := opencv.CreateImage(img0.Width(), img0.Height(), 8, 1) opencv.Zero(inpaint_mask) //opencv.Zero( inpainted ) win := opencv.NewWindow("image") defer win.Destroy() prev_pt := opencv.Point{-1, -1} win.SetMouseCallback(func(event, x, y, flags int, param ...interface{}) { if img == nil { os.Exit(0) } if event == opencv.CV_EVENT_LBUTTONUP || (flags&opencv.CV_EVENT_FLAG_LBUTTON) == 0 { prev_pt = opencv.Point{-1, -1} } else if event == opencv.CV_EVENT_LBUTTONDOWN { prev_pt = opencv.Point{x, y} } else if event == opencv.CV_EVENT_MOUSEMOVE && (flags&opencv.CV_EVENT_FLAG_LBUTTON) != 0 { pt := opencv.Point{x, y} if prev_pt.X < 0 { prev_pt = pt } rgb := opencv.ScalarAll(255.0) opencv.Line(inpaint_mask, prev_pt, pt, rgb, 5, 8, 0) opencv.Line(img, prev_pt, pt, rgb, 5, 8, 0) prev_pt = pt win.ShowImage(img) } }) win.ShowImage(img) opencv.WaitKey(0) win2 := opencv.NewWindow("inpainted image") defer win2.Destroy() win2.ShowImage(inpainted) for { key := opencv.WaitKey(20) if key == 27 { os.Exit(0) } else if key == 'r' { opencv.Zero(inpaint_mask) opencv.Copy(img0, img, nil) win.ShowImage(img) } else if key == 'i' || key == '\n' { opencv.Inpaint(img, inpaint_mask, inpainted, 3, opencv.CV_INPAINT_TELEA, ) win2.ShowImage(inpainted) } } os.Exit(0) }
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 main() { filename := "../images/lena.jpg" if len(os.Args) == 2 { filename = os.Args[1] } image := opencv.LoadImage(filename) if image == nil { panic("LoadImage fail") } defer image.Release() w := image.Width() h := image.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(image, gray, opencv.CV_BGR2GRAY) win := opencv.NewWindow("Edge") defer win.Destroy() win.SetMouseCallback(func(event, x, y, flags int, param ...interface{}) { fmt.Printf("event = %d, x = %d, y = %d, flags = %d\n", event, x, y, flags, ) }) win.CreateTrackbar("Thresh", 1, 100, func(pos int, param ...interface{}) { edge_thresh := pos 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(edge_thresh), float64(edge_thresh*3), 3) opencv.Zero(cedge) // copy edge points opencv.Copy(image, cedge, edge) win.ShowImage(cedge) fmt.Printf("pos = %d\n", pos) }) win.ShowImage(image) for { key := opencv.WaitKey(20) if key == 27 { os.Exit(0) } } os.Exit(0) }
//v.processors = append(v.processors, processor1) v.processors = append(v.processors, MarkEyesCirclesProcessor) } var MarkEyesRectProcessor = func(v *VideoProcessor) { } var MarkEyesCirclesProcessor = func(v *VideoProcessor) { _, currentfile, _, _ := runtime.Caller(0) var cascade = path.Join(path.Dir(currentfile), "/../cascades/haarcascade_eye_tree_eyeglasses.xml") var eyeMarker = NewEyeMarker(cascade) var copy = opencv.CreateImage(v.currentFrameIn.Width(), v.currentFrameIn.Height(), v.currentFrameIn.Depth(), v.currentFrameIn.Channels()) var copy2 = opencv.CreateImage(v.currentFrameIn.Width(), v.currentFrameIn.Height(), v.currentFrameIn.Depth(), v.currentFrameIn.Channels()) opencv.Copy(v.currentFrameIn, copy, nil) opencv.Copy(v.currentFrameIn, copy2, nil) var rois = eyeMarker.MarkEyesRects(copy) if len(rois) > 0 { copy.SetROI(*rois[0]) copy = ProcessRoi(copy, 75) //var gray = opencv.CreateImage(copy.Width(),copy.Height(),opencv.IPL_DEPTH_8U,1) //copy.SetROI(*rois[0]) var circels = HoughCircles1(copy) log.Print(circels) for i := 0; i < len(circels); i++ {
func MarkRoi() { // // var fourcc = opencv.FOURCC('m','p','4','v') // var fps = float32(video.GetProperty(opencv.CV_CAP_PROP_FPS)) // var size = opencv.Size{int(video.GetProperty(opencv.CV_CAP_PROP_FRAME_WIDTH)), // int(video.GetProperty(opencv.CV_CAP_PROP_FRAME_HEIGHT))}; // // log.Print("fourcc: ", fourcc) // log.Print("fps: ", fps) // log.Print("size: ", size) // // defer video.Release() win := opencv.NewWindow("Go-OpenCV Webcam") defer win.Destroy() win2 := opencv.NewWindow("rect cam: ") win2.Move(400, 0) defer win2.Destroy() _, currentfile, _, _ := runtime.Caller(0) var cascade = path.Join(path.Dir(currentfile), "/../cascades/haarcascade_eye_tree_eyeglasses.xml") var eyeMarker = NewEyeMarker(cascade) var cap = NewVideoCapture(path.Join(path.Dir(currentfile), "/../eyevid1.mp4")) //var cap = NewVideoCapture("") var rois []*opencv.Rect for { var frame = cap.GetFrame() if frame == nil { break } img := frame var copy = opencv.CreateImage(img.Width(), img.Height(), opencv.IPL_DEPTH_8U, 3) opencv.Copy(img, copy, nil) rois = eyeMarker.MarkEyesRects(copy) if len(rois) > 0 { log.Print(rois[0]) copy.SetROI(*rois[0]) copy = ProcessRoi(copy, 25) //var gray = opencv.CreateImage(copy.Width(),copy.Height(),opencv.IPL_DEPTH_8U,1) //copy.SetROI(*rois[0]) var circels = HoughCircles1(copy) log.Print(circels) for i := 0; i < len(circels); i++ { var center = opencv.Point{rois[0].X() + int(circels[i].X), rois[0].Y() + int(circels[0].Y)} //var center = opencv.Point{int(circels[i].X),int(circels[0].Y)} opencv.Circle(img, center, int(circels[i].R), opencv.NewScalar(255, 0, 0, 1), 1, 1, 0) //src *IplImage, dp float64, min_dist float64, param1 float64, param2 float64, min_radius int, max_radius int } copy.ResetROI() } win.ShowImage(img) win2.ShowImage(copy) key := opencv.WaitKey(10) if key == 27 { os.Exit(0) } } os.Exit(0) }
func ProcessImageTest() { log.Print("Try to open webcam.") cap := opencv.NewCameraCapture(0) if cap == nil { panic("can not open camera") } log.Print("webcame opened.") var outputFileName = "testvideo.mpeg" var width = 640 var height = 480 var fps = 20.0 var fourcc = opencv.FOURCC('m', 'p', '4', 'v') var noOfCapturedFrames = 100 cap.SetProperty(opencv.CV_CAP_PROP_FRAME_WIDTH, float64(width)) cap.SetProperty(opencv.CV_CAP_PROP_FRAME_HEIGHT, float64(height)) cap.SetProperty(opencv.CV_CAP_PROP_FPS, fps) log.Print("capturing ", noOfCapturedFrames, " frames and save them to the following file: ", outputFileName) log.Print("width: ", width) log.Print("height: ", height) log.Print("fps: ", fps) log.Print("fourcc: ", fourcc) var frames []*opencv.IplImage for len(frames) < noOfCapturedFrames { if cap.GrabFrame() { img := cap.RetrieveFrame(1) if img != nil { var frame = cap.RetrieveFrame(1) var copy = opencv.CreateImage(int(width), int(height), frame.Depth(), frame.Channels()) defer copy.Release() opencv.Copy(frame, copy, nil) frames = append(frames, copy) } else { fmt.Println("Image ins nil") } } } cap.Release() win := opencv.NewWindow("Go-OpenCV Webcam") defer win.Destroy() for i := 0; i < len(frames); i++ { log.Print("displaying frame no: ", i) win.ShowImage(frames[i]) key := opencv.WaitKey(10) if key != 0 { log.Print(key) } } log.Print("start writing frames to file.") var videoWritter = NewVideoWriter(fourcc, float32(fps), opencv.Size{int(width), int(height)}) videoWritter.SaveAsVideo(frames, outputFileName) log.Print("created file with name: ", outputFileName) os.Exit(0) }
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 }