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.1k Golang : How to determine if user agent is a mobile device example
+4.9k Python : Create Whois client or function example
+7k Golang : Of hash table and hash map
+8.7k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+7.2k Golang : Handling Yes No Quit query input
+11.8k Golang : Save webcamera frames to video file
+5k Python : Convert(cast) string to bytes example
+31.9k Golang : Convert []string to []byte examples
+62.3k Golang : Convert HTTP Response body to string
+5.9k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+5.5k Get website traffic ranking with Similar Web or Alexa