Golang : Eroding and dilating image with OpenCV example
I'm learning OpenCV by going through some of the examples found on OpenCV website. For this post, below is an example of Erode and Dilate implementations in Golang.
In a nutshell, Erosion and Dilation are two basic morphological operations that can help in
- Removing noise from input image
- Isolation of individual elements and joining disparate elements in an image.
- Finding of intensity bumps or holes in an image
Also, erosion and dilation can help in reducing the amount of data to be processed, thus speeding up the algorithms that depend on the output images.
For more detailed information, please see the OpenCV tutorial at http://docs.opencv.org/2.4.2/doc/tutorials/imgproc/erosiondilatation/erosiondilatation.html.
An screenshot example of Erode and Dilate operations written in Golang:
You can use this image
(http://docs.opencv.org/2.4.2/images/Morphology1TutorialTheoryOriginalImage.png) for running test with the program below.
Here you go!
package main
import (
"fmt"
"github.com/lazywei/go-opencv/opencv"
"os"
)
var (
originalWindow = new(opencv.Window)
processedImageWindow = new(opencv.Window)
image = new(opencv.IplImage)
processedImage = new(opencv.IplImage)
element *opencv.IplConvKernel
kernelSlider = 5
erodeDilateSlider = 0
kernelSize = 1
)
func process() {
element = opencv.CreateStructuringElement(kernelSize, kernelSize, kernelSize/2, kernelSize/2, opencv.CV_MORPH_RECT)
defer element.ReleaseElement()
if erodeDilateSlider == 0 {
fmt.Printf("Erode by %dx%d\n", kernelSize, kernelSize)
opencv.Erode(image, processedImage, element, 1)
} else {
fmt.Printf("Dilate by %dx%d\n", kernelSize, kernelSize)
opencv.Dilate(image, processedImage, element, 1)
}
processedImageWindow.ShowImage(processedImage)
}
func kernelTrackBar(position int, param ...interface{}) {
kernelSize = position
if kernelSize%2 == 0 {
kernelSize = kernelSize + 1
}
processedImageWindow.SetTrackbarPos("Kernel Size : ", kernelSize)
process()
fmt.Printf("Kernel size = %dx%d\n", kernelSize, kernelSize)
}
func erodeDilateTrackBar(position int, param ...interface{}) {
process()
erodeDilateSlider = position
}
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()
originalWindow = opencv.NewWindow("Original Image")
defer originalWindow.Destroy()
processedImageWindow = opencv.NewWindow("Processed Image")
processedImageWindow.Move(200, 300)
defer processedImageWindow.Destroy()
// init with 5x5 kernel size
element = opencv.CreateStructuringElement(5, 5, erodeDilateSlider/2, erodeDilateSlider/2, opencv.CV_MORPH_RECT)
defer element.ReleaseElement()
processedImage = image.Clone()
// 3rd parameter iteration = number of multiple erosions
// we settle for 1
opencv.Erode(image, processedImage, element, 1)
processedImageWindow.CreateTrackbar("Kernel Size : ", 1, 21, kernelTrackBar)
processedImageWindow.CreateTrackbar("Erode(0)/Dilate(1) : ", 0, 1, erodeDilateTrackBar)
originalWindow.ShowImage(image)
processedImageWindow.ShowImage(processedImage)
for {
key := opencv.WaitKey(20)
if key == 27 {
os.Exit(0)
}
}
os.Exit(0)
}
References:
https://www.socketloop.com/tutorials/golang-gaussian-blur-on-image-and-camera-video-feed-examples
https://godoc.org/github.com/lazywei/go-opencv/opencv#Erode
http://docs.opencv.org/2.4.2/doc/tutorials/imgproc/erosiondilatation/erosiondilatation.html
http://docs.opencv.org/2.4.2/images/Morphology1TutorialTheoryOriginalImage.png
See also : Golang : Gaussian blur on image and camera video feed examples
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
+7.2k Golang : Create zip/ePub file without compression(use Store algorithm)
+5.4k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+5.8k Golang : Experimenting with the Rejang script
+31.9k Golang : Convert []string to []byte examples
+5.8k Golang : Compound interest over time example
+5.1k Golang : Generate Interleaved 2 inch by 5 inch barcode
+11.7k Golang : Determine if time variables have same calendar day
+14.2k Golang : How to check if your program is running in a terminal
+21.8k Golang : How to run Golang application such as web server in the background or as daemon?
+5.7k Golang : Use NLP to get sentences for each paragraph example
+13.9k Golang : Check if a file exist or not
+7.1k Golang : Word limiter example