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
+17.1k Golang : Check if IP address is version 4 or 6
+21.2k Golang : Encrypt and decrypt data with TripleDES
+4.6k Which content-type(MIME type) to use for JSON data
+26.4k Golang : Force your program to run with root permissions
+11.4k SSL : The certificate is not trusted because no issuer chain was provided
+7k Golang : How to detect if a sentence ends with a punctuation?
+5.4k Unix/Linux : How to find out the hard disk size?
+6.5k Golang : Find the longest line of text example
+5.1k PHP : Hide PHP version information from curl
+19.6k Golang : How to run your code only once with sync.Once object
+15.7k Golang : Generate universally unique identifier(UUID) example