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
+5.6k Javascript : How to replace HTML inside <div>?
+9.5k Golang : Format strings to SEO friendly URL example
+4.7k Google : Block or disable caching of your website content
+5.3k PHP : Fix Call to undefined function curl_init() error
+7.8k Golang : Sort words with first uppercase letter
+7.7k Javascript : Put image into Chrome browser's console
+6.6k Android Studio : Hello World example
+12.8k Golang : Calculate elapsed years or months since a date
+15k Golang : How to get Unix file descriptor for console and file
+26k Golang : Convert(cast) string to uint8 type and back to string
+8.8k Golang : How to capture return values from goroutines?