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
+17.4k Golang : Upload/Receive file progress indicator
+7.2k Golang : Process json data with Jason package
+5k Golang : Customize scanner.Scanner to treat dash as part of identifier
+35.2k Golang : Integer is between a range
+9.6k Golang : Get current, epoch time and display by year, month and day
+13.6k Golang : Human readable time elapsed format such as 5 days ago
+22.2k Golang : How to read JPG(JPEG), GIF and PNG files ?
+7.2k Golang : Word limiter example
+7.6k Golang : Scan files for certain pattern and rename part of the files
+20.3k Golang : Secure(TLS) connection between server and client
+40.8k Golang : How to count duplicate items in slice/array?
+11.1k Golang : Byte format example