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
+14.3k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+7.5k Javascript : How to check a browser's Do Not Track status?
+47.4k Golang : How to convert JSON string to map and slice
+8.6k Golang : Build and compile multiple source files
+19.8k Golang : Count number of digits from given integer value
+4.5k HTTP common errors and their meaning explained
+5.3k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+14k Golang : How to shuffle elements in array or slice?
+17.5k Golang : Get all upper case or lower case characters from string example
+3.8k Javascript : Empty an array example
+7.6k Golang : Check from web if Go application is running or not
+32.4k Golang : How to check if a date is within certain range?