Golang : Apply Histogram Equalization to color images
For this tutorial, we will learn how to convert color images to greyscale and then apply Histogram Equalization as a way to standardize the brightness and contrast. This is a step to pre-process the image for facial recognition purpose with Eigenface method.
An example output. Left most is the original and right most image is the final product.
Here you go!
package main
import (
"fmt"
"github.com/lazywei/go-opencv/opencv"
"image"
gif "image/gif"
_ "image/jpeg"
_ "image/png"
"net/http"
"os"
)
func errCheck(err error) {
if err != nil {
panic(err)
}
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage : %s <imagefilename>\n", os.Args[0])
os.Exit(0)
}
imageFileName := os.Args[1]
// we will use Go's method of load images
// instead of openCV.LoadImage
// because we want to detect if the user supplies animated GIF or not
imageFile, err := os.Open(imageFileName)
errCheck(err)
defer imageFile.Close()
img, _, err := image.Decode(imageFile)
errCheck(err)
buffer := make([]byte, 512)
imageFile.Seek(0, 0) // reset reader
_, err = imageFile.Read(buffer)
errCheck(err)
filetype := http.DetectContentType(buffer)
// check if image is GIF and if yes, check to see if it is animated GIF by
// counting the LoopCount number
fmt.Println("Analyzing image type : ", filetype)
if filetype == "image/gif" {
imageFile.Seek(0, 0)
// warn if image is animated GIF
gif, err := gif.DecodeAll(imageFile)
errCheck(err)
if gif.LoopCount != 0 {
fmt.Println("Animated gif detected. Will only scan for faces in the 1st frame.")
}
}
// convert Go's image.Image type to OpenCV's IplImage(Intel Image Processing Library)
openCVImg := opencv.FromImage(img)
defer openCVImg.Release()
if openCVImg != nil {
fmt.Println("Converting [" + imageFileName + "] to greyscale image......")
w := openCVImg.Width()
h := openCVImg.Height()
// create an IplImage with 1 channel(grey)
greyImg := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 1)
defer greyImg.Release()
// convert to greyscale
opencv.CvtColor(openCVImg, greyImg, opencv.CV_BGR2GRAY)
// from http://www.shervinemami.info/faceRecognition.html
// make the image a fixed size
// CV_INTER_CUBIC or CV_INTER_LINEAR is good for enlarging, and
// CV_INTER_AREA is good for shrinking / decimation, but bad at enlarging.
histoEqImg := opencv.CreateImage(w, h, opencv.IPL_DEPTH_8U, 1)
defer histoEqImg.Release()
histoEqImg = opencv.Resize(greyImg, w, h, opencv.CV_INTER_LINEAR)
fmt.Println("Applying Histogram Equalization to [" + imageFileName + "]......")
// standard brightness and contrast
greyImg.EqualizeHist(histoEqImg)
// save to file
fmt.Println("Saving results to [grey.jpg] and [equalhisto.jpg]")
opencv.SaveImage("./grey.jpg", greyImg, opencv.CV_IMWRITE_JPEG_QUALITY)
opencv.SaveImage("./equalhisto.jpg", histoEqImg, opencv.CV_IMWRITE_JPEG_QUALITY)
} else {
panic("OpenCV FromImage error")
}
}
Happy coding and recognizing face with computer vision!
References:
http://www.shervinemami.info/faceRecognition.html
http://docs.opencv.org/2.4/modules/contrib/doc/facerec/facerec_tutorial.html
See also : Golang : GUI with Qt and OpenCV to capture image from camera
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
+21.4k Golang : Test file read write permission example
+44.3k Golang : Marshal and unmarshal json.RawMessage struct example
+4.9k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+19.3k Golang : Convert date string to variants of time.Time type examples
+9.1k Golang : Convert file content to Hex
+27.7k Golang : Get first few and last few characters from string
+19.8k Golang : How to get time zone and load different time zone?
+15.2k Golang : Convert slice to array
+9.1k Golang : Text file editor (accept input from screen and save to file)
+21.1k Golang : Set and Get HTTP request headers example
+5.8k CloudFlare : Another way to get visitor's real IP address
+35k Golang : Convert date or time stamp from string to time.Time type