Golang : Hue, Saturation and Value(HSV) with OpenCV example
Problem:
BGR colorspace has pixels intensity information encoded into the blue, green and red channels. You want to convert an image from RGB values to their HSV(hue, saturation, value) colorspace. How to do that in Golang and OpenCV?
Solution:
Use the opencv.CvtColor()
function and set the third parameter to 40. Such as opencv.CvtColor(webCamFrameImage, webCamFrameImageHSV, 40)
. Why 40? It is not documented in Go-OpenCV godoc, but the integer is 40 for CV_BGR2HSV according to http://docs.opencv.org/3.1.0/df/d4e/groupimgprocc.html
Example of video feed converted to HSV from BGR:
Here you go!
package main
import (
"fmt"
"github.com/lazywei/go-opencv/opencv"
"os"
)
var (
originalWindow = new(opencv.Window)
segmentationWindow = new(opencv.Window)
webCamFrameImage = new(opencv.IplImage)
webCamera = new(opencv.Capture)
)
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("Video feed from camera")
defer originalWindow.Destroy()
segmentationWindow = opencv.NewWindow("Segmentation")
segmentationWindow.Move(500, 100)
defer segmentationWindow.Destroy()
for {
key := opencv.WaitKey(20)
if key == 27 {
os.Exit(0)
}
if webCamera.GrabFrame() {
webCamFrameImage = webCamera.RetrieveFrame(1)
if webCamFrameImage != nil {
// my webcamera is HD. Have to make the image frame smaller
// comment the immediate line to remove resize
webCamFrameImage = opencv.Resize(webCamFrameImage, 700, 0, opencv.CV_INTER_LINEAR)
originalWindow.ShowImage(webCamFrameImage)
// convert BGR to HSV (Hue, Saturation, Value) colorspace
// It is not documented in Go-OpenCV godoc, but the integer is 40 for CV_BGR2HSV
// according to http://docs.opencv.org/3.1.0/df/d4e/group__imgproc__c.html
webCamFrameImageHSV := webCamFrameImage.Clone()
opencv.CvtColor(webCamFrameImage, webCamFrameImageHSV, 40)
segmentationWindow.ShowImage(webCamFrameImageHSV)
}
}
}
os.Exit(0)
}
References:
https://www.socketloop.com/tutorials/golang-changing-a-rgba-image-number-of-channels-with-opencv
See also : Golang : Changing a RGBA image number of channels with OpenCV
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.7k Golang : Fibonacci number generator examples
+16.2k Golang : Send email and SMTP configuration example
+25.8k Mac/Linux and Golang : Fix bind: address already in use error
+6.6k Get Facebook friends working in same company
+10.7k Golang : Sieve of Eratosthenes algorithm
+9.1k Golang : Timeout example
+9.2k Golang : Create unique title slugs example
+46k Golang : Marshal and unmarshal json.RawMessage struct example
+7.6k Golang : Ways to recover memory during run time.
+8.2k Golang : Generate Datamatrix barcode
+6.6k Golang : Get expvar(export variables) to work with multiplexer
+10.7k Golang : Create Temporary File