Golang : Changing a RGBA image number of channels with OpenCV
Problem:
Golang image packages deal with RGBA ( 4 channels ) and OpenCV deals with BGR ( 3 channels ).
You found out that the image frame returned by OpenCV's video capture from a camera only has 3 channels(BGR) - without the Alpha channel. You need to convert the image frame to BGRA ( 4 channels ).
Also, you would like to convert BGRA ( 4 channels ) to BGR (3 channels) as well. How to do that?
Solution:
First, create a new opencv.IplImage and with the number of channels that you want. If you want to convert the image to grayscale, set the number of channel to 1.
Next, use opencv.cvtColor()
to convert the image.
Just a note, you don't have to stick to the limited constants found in Go-OpenCV package.
const (
CV_BGR2GRAY = C.CV_BGR2GRAY
CV_RGB2GRAY = C.CV_RGB2GRAY
CV_GRAY2BGR = C.CV_GRAY2BGR
CV_GRAY2RGB = C.CV_GRAY2RGB
CV_BGR2BGRA = C.CV_BGR2BGRA
CV_RGBA2BGRA = C.CV_RGBA2BGRA
CV_BLUR_NO_SCALE = C.CV_BLUR_NO_SCALE
CV_BLUR = C.CV_BLUR
CV_GAUSSIAN = C.CV_GAUSSIAN
CV_MEDIAN = C.CV_MEDIAN
CV_BILATERAL = C.CV_BILATERAL
CV_8U = C.CV_8U
CV_8S = C.CV_8S
CV_16U = C.CV_16U
CV_16S = C.CV_16S
CV_32S = C.CV_32S
CV_32F = C.CV_32F
CV_64F = C.CV_64F
)
You can use the integer enumerations at [http://docs.opencv.org/3.1.0/df/d4e/group__imgproc__c.html][1]
in opencv.cvtColor()
function.
Here you go!
func opencvImageBGRToBGRA(img *opencv.IplImage) opencv.IplImage {
// The image frames from camera is in RGB (3 channels )
// We need to convert the frames to RGBA (4 channels )
// so that we can perform copy and paste the UTF8 strings
// into the region of interest.
// Using the ToImage() function will work, but will cause delay in refresh rate.
// Use CvtColor() function for the best result
w := img.Width()
h := img.Height()
// create an IplImage with 4 channels
tmp := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 4)
// upgrade BGR to BGRA ( 3 to 4 channels)
opencv.CvtColor(img, tmp, opencv.CV_BGR2BGRA)
return *tmp
}
func BGRAToBGR(img *opencv.IplImage) opencv.IplImage {
w := img.Width()
h := img.Height()
// create a IplImage with 3 channels
tmp := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 3)
// downgrade BGRA to BGR ( 4 to 3 channels)
opencv.CvtColor(img, tmp, 1)
// why use integer value of 1?
// see http://docs.opencv.org/3.1.0/df/d4e/group__imgproc__c.html
return *tmp
}
NOTES:
See request for RGB and RGB48 support in Golang discussion : https://groups.google.com/forum/#!topic/golang-nuts/z7W0nhBfCho
See also : Golang : Get RGBA values of each image pixel
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 : Sort words with first uppercase letter
+8.8k Golang : Get curl -I or head data from URL example
+11.7k Golang : How to parse plain email text and process email header?
+24.9k Golang : Storing cookies in http.CookieJar example
+5.6k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+8k Golang : HttpRouter multiplexer routing example
+8.2k Golang : How to check if input string is a word?
+10.9k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+7.1k Golang : How to detect if a sentence ends with a punctuation?
+9.9k Golang : How to get quoted string into another string?
+5.2k Golang : Get S3 or CloudFront object or file information