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
+7.8k Golang : Grayscale Image
+6.1k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+8.8k Golang : GMail API create and send draft with simple upload attachment example
+7.9k Golang : What fmt.Println() can do and println() cannot do
+8.7k Golang : Gorilla web tool kit schema example
+12.6k Swift : Convert (cast) Int or int32 value to CGFloat
+18.5k Golang : Iterating Elements Over A List
+14.2k Golang : How to pass map to html template and access the map's elements
+12.9k Swift : Convert (cast) Int to String ?
+39.9k Golang : UDP client server read write example
+18.4k Golang : Set, Get and List environment variables
+10k Golang : How to get quoted string into another string?