Golang : Gaussian blur on image and camera video feed examples
The following code examples demonstrate how to use Golang and OpenCV to apply Gaussian blur filter on static image and video feed from a web camera. The code are pretty straight forward and I've written these examples to help myself to understand how image filtering works. Hope you can find them useful as well.
Here you go!
Gaussian blur on static image:
package main
import (
"fmt"
"github.com/lazywei/go-opencv/opencv"
"os"
)
var (
originalWindow = new(opencv.Window)
blurredWindow = new(opencv.Window)
blurredImage = new(opencv.IplImage)
image = new(opencv.IplImage)
slider = 50
)
func trackBar(position int, param ...interface{}) {
kernelSize := position
// don't want 0x0 kernel size
if kernelSize%2 == 0 {
kernelSize = kernelSize + 1
}
blurredWindow.SetTrackbarPos("Kernel Size : ", kernelSize)
sigma := float64(0.3)*float64(float64(kernelSize-1)*float64(0.5)-1) + float64(0.8)
// From http://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html?highlight=smooth#cv.Smooth
// Assuming param1 and param2 is to specify the kernel size
// param3 for sigma
opencv.Smooth(image, blurredImage, opencv.CV_GAUSSIAN, kernelSize, kernelSize, sigma, 0)
blurredWindow.ShowImage(blurredImage)
fmt.Printf("Kernel size = %dx%d\n", kernelSize, kernelSize)
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage : %s <image filename>\n", os.Args[0])
os.Exit(0)
}
imageFileName := os.Args[1]
fmt.Println("Loading image from ", imageFileName)
fmt.Println("Press ESC key to quit")
image = opencv.LoadImage(imageFileName)
if image == nil {
panic("LoadImage failed")
}
defer image.Release()
blurredImage = image.Clone()
originalWindow = opencv.NewWindow("Original Image")
defer originalWindow.Destroy()
blurredWindow = opencv.NewWindow("Gaussian Blurred")
blurredWindow.Move(200, 200)
defer blurredWindow.Destroy()
// start with kernel size 1x1
blurredWindow.CreateTrackbar("Kernel Size : ", 1, slider, trackBar)
originalWindow.ShowImage(image)
blurredWindow.ShowImage(blurredImage)
for {
key := opencv.WaitKey(20)
if key == 27 {
os.Exit(0)
}
}
os.Exit(0)
}
Gaussian blur on video feed from web camera:
package main
import (
"fmt"
"github.com/lazywei/go-opencv/opencv"
"os"
)
var (
originalWindow = new(opencv.Window)
blurredWindow = new(opencv.Window)
webCamFrameImage = new(opencv.IplImage)
webCamera = new(opencv.Capture)
slider = 50
kernelSize int
)
func BlurImage(image *opencv.IplImage) error {
// don't want 0x0 kernel size
if kernelSize%2 == 0 {
kernelSize = kernelSize + 1
}
sigma := float64(0.3)*float64(float64(kernelSize-1)*float64(0.5)-1) + float64(0.8)
blurredImage := image.Clone()
defer blurredImage.Release()
// From http://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html?highlight=smooth#cv.Smooth
// Assuming param1 and param2 is to specify the kernel size
// param3 for sigma
opencv.Smooth(image, blurredImage, opencv.CV_GAUSSIAN, kernelSize, kernelSize, sigma, 0)
blurredWindow.ShowImage(blurredImage)
blurredWindow.SetTrackbarPos("Kernel Size : ", kernelSize)
fmt.Printf("Kernel size = %dx%d\n", kernelSize, kernelSize)
return nil
}
func trackBar(position int, param ...interface{}) {
kernelSize = position
}
func main() {
// activate webCamera
webCamera = opencv.NewCameraCapture(opencv.CV_CAP_ANY) // autodetect
if webCamera == nil {
panic("Unable to open camera")
}
defer webCamera.Release()
fmt.Println("Press ESC button to quit. Must be on either one of the windows, not in terminal...")
originalWindow = opencv.NewWindow("Original Image")
defer originalWindow.Destroy()
blurredWindow = opencv.NewWindow("Gaussian Blurred")
blurredWindow.Move(200, 200)
defer blurredWindow.Destroy()
// start with kernel size 1x1
blurredWindow.CreateTrackbar("Kernel Size : ", 1, slider, trackBar)
for {
key := opencv.WaitKey(20)
if key == 27 {
os.Exit(0)
}
if webCamera.GrabFrame() {
webCamFrameImage = webCamera.RetrieveFrame(1)
if webCamFrameImage != nil {
originalWindow.ShowImage(webCamFrameImage)
BlurImage(webCamFrameImage)
}
}
}
os.Exit(0)
}
Image credits:
References:
https://socketloop.com/tutorials/golang-put-utf8-text-on-opencv-video-capture-image-frame
See also : Golang : Select region of interest with mouse click and crop from image
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
+6.2k Golang : Break string into a slice of characters example
+19.5k Golang : Count JSON objects and convert to slice/array
+28.9k Golang : missing Git command
+15.4k Golang : How to login and logout with JWT example
+7.9k Golang : Get final or effective URL with Request.URL example
+12.4k Golang : Transform comma separated string to slice example
+19.5k Golang : Append content to a file
+21.9k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+9.1k Golang : Find the length of big.Int variable example
+10.9k Golang : Calculate Relative Strength Index(RSI) example
+26k Golang : Get executable name behind process ID example
+9.1k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases