Golang : Add text to image and get OpenCV's X, Y co-ordinates example
Building on from the previous OpenCV-GTK-GUI application tutorial. For this tutorial, I need to know where to position my text on the camera feed. To be precise, I need to know the X and Y coordinates returned by Go-OpenCV's Point struct for top-left, top-right, bottom-left and bottom-right locations. Also, I need to be able to control the text position with two sliders(scrollbars or trackbars).
The code example that follows will write a simple "Hello World" string and the current time on screen (camera feed) and allow us to control the text's position.
Here you go!
package main
import (
"fmt"
"strconv"
"time"
"github.com/lazywei/go-opencv/opencv"
"github.com/mattn/go-gtk/glib"
"github.com/mattn/go-gtk/gtk"
)
var (
win = new(opencv.Window)
webCamera = new(opencv.Capture)
statusbar = new(gtk.Statusbar)
snapshotFileName string
width, height int
sliderPosX, sliderPosY int
horizontalScale = float32(1.0)
verticalScale = float32(1.0)
shear = float32(1.0)
thickness = 3
lineType = 8
textFont = opencv.InitFont(opencv.CV_FONT_HERSHEY_SIMPLEX, horizontalScale, verticalScale, shear, thickness, lineType)
)
func processFrameAndUpdate() {
for {
if webCamera.GrabFrame() {
IplImgFrame := webCamera.RetrieveFrame(1)
if IplImgFrame != nil {
//IplImgFrame = opencv.Resize(IplImgFrame, 0, height-200, opencv.CV_INTER_LINEAR)
// https://godoc.org/github.com/lazywei/go-opencv/opencv#NewScalar
redColor := opencv.NewScalar(0, 0, 255, 0) // red - (blue, green, red, alpha)
cyanColor := opencv.NewScalar(255, 255, 0, 0) // cyan - (blue, green, red, alpha)
position := opencv.Point{sliderPosX, sliderPosY}
// see https://www.socketloop.com/tutorials/golang-get-current-time
currentTime := time.Now().Local().Format("2006-01-02 15:04:05 +0800")
textFont.PutText(IplImgFrame, "Hello World! ", position, redColor)
textFont.PutText(IplImgFrame, currentTime, opencv.Point{sliderPosX, sliderPosY + int(verticalScale*30.0)}, cyanColor)
win.ShowImage(IplImgFrame)
}
}
}
}
func main() {
// a new OpenCV window
win = opencv.NewWindow("Go-OpenCV camera feed for Gtk-GUI")
defer win.Destroy()
// activate webCamera
webCamera = opencv.NewCameraCapture(opencv.CV_CAP_ANY) // autodetect
if webCamera == nil {
panic("Unable to open camera")
}
defer webCamera.Release()
// get some data from camera
width = int(webCamera.GetProperty(opencv.CV_CAP_PROP_FRAME_WIDTH))
height = int(webCamera.GetProperty(opencv.CV_CAP_PROP_FRAME_HEIGHT))
fmt.Println("Camera width : ", width)
fmt.Println("Camera height : ", height)
// open up a new "pure" OpenCV window first
go processFrameAndUpdate() // goroutine to update feed from camera
// then our "floating" GTK toolbar
gtk.Init(nil)
window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
window.SetPosition(gtk.WIN_POS_CENTER)
window.SetTitle("Golang-GTK-OpenCV-AppleBooth-Example!")
window.SetIconName("gtk-dialog-info")
window.Connect("destroy", func(ctx *glib.CallbackContext) {
println("got destroy!", ctx.Data().(string))
gtk.MainQuit()
}, "Happy coding!")
vbox := gtk.NewVBox(false, 1)
//--------------------------------------------------------
// GtkVPaned
//--------------------------------------------------------
vpaned := gtk.NewVPaned()
vbox.Add(vpaned)
//--------------------------------------------------------
// GtkFrame
//--------------------------------------------------------
frame1 := gtk.NewFrame("Adjust X & Y co-ordinates to find top-left, top-right, bottom-left & bottom-right :")
framebox1 := gtk.NewVBox(false, 1)
frame1.Add(framebox1)
//--------------------------------------------------------
// GtkScale
//--------------------------------------------------------
scaleXHBox := gtk.NewHBox(false, 1)
scaleX := gtk.NewHScaleWithRange(0, float64(width), 1)
scaleX.Connect("value-changed", func() {
//println("scale:", int(scale.GetValue()))
sliderPosX = int(scaleX.GetValue())
statusbar.Push(statusbar.GetContextId("go-gtk"), "X : "+strconv.Itoa(sliderPosX)+" Y : "+strconv.Itoa(sliderPosY))
})
scaleXHBox.Add(scaleX)
framebox1.PackStart(scaleXHBox, false, false, 0)
scaleYHBox := gtk.NewHBox(false, 1)
scaleY := gtk.NewHScaleWithRange(0, float64(height), 1)
scaleY.Connect("value-changed", func() {
//println("scale:", int(scale.GetValue()))
sliderPosY = int(scaleY.GetValue())
statusbar.Push(statusbar.GetContextId("go-gtk"), "X : "+strconv.Itoa(sliderPosX)+" Y : "+strconv.Itoa(sliderPosY))
})
scaleYHBox.Add(scaleY)
framebox1.PackStart(scaleYHBox, false, false, 0)
vpaned.Pack1(frame1, false, false)
//--------------------------------------------------------
// GtkHBox
//--------------------------------------------------------
buttons := gtk.NewHBox(false, 1)
//--------------------------------------------------------
// GtkButton
//--------------------------------------------------------
quitButton := gtk.NewButtonWithLabel("Quit")
quitButton.Clicked(func() {
gtk.MainQuit()
})
buttons.Add(quitButton)
framebox1.PackStart(buttons, false, false, 0)
//--------------------------------------------------------
// GtkVSeparator
//--------------------------------------------------------
vsep := gtk.NewVSeparator()
framebox1.PackStart(vsep, false, false, 0)
statusbar = gtk.NewStatusbar()
//context_id := statusbar.GetContextId("go-gtk")
//--------------------------------------------------------
// GtkStatusbar
//--------------------------------------------------------
framebox1.PackStart(statusbar, false, false, 0)
//--------------------------------------------------------
// Event
//--------------------------------------------------------
window.Add(vbox)
window.SetSizeRequest(600, 130)
window.ShowAll()
gtk.Main()
}
References:
https://godoc.org/github.com/lazywei/go-opencv/opencv#Point
https://www.socketloop.com/tutorials/golang-get-current-time
See also : Golang : Detect number of faces or vehicles in a photo
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.5k Facebook : Getting the friends list with PHP return JSON format
+15.7k Golang : Force download file example
+6.4k Golang : Test input string for unicode example
+10.5k Generate Random number with math/rand in Go
+5.3k Golang : Convert lines of string into list for delete and insert operation
+6.7k Golang : When to use make or new?
+10k Golang : Translate language with language package example
+8k Golang : Get today's weekday name and calculate target day distance example
+4.9k HTTP common errors and their meaning explained
+8.2k Android Studio : Rating bar example
+5.4k Unix/Linux/MacOSx : How to remove an environment variable ?
+9.1k Golang : Handle sub domain with Gin