[OpenCV] Chapter02-06 Gamma correction and per-element math

Gamma correction and per-element math

Source Code

package main

import (
 "gocv.io/x/gocv"
 "log"
 "math"
)

const GAMMA float64 = 0.5

func main() {
 imageFilePath := "../../data/Lena.png"
 originalMat := gocv.IMRead(imageFilePath, gocv.IMReadGrayScale)
 if originalMat.Empty() {
  log.Panic("Can not read image: " + imageFilePath)
  return
 }

 windowForOriginal := gocv.NewWindow("Original Image")
 defer windowForOriginal.Close()
 windowForOriginal.IMShow(originalMat)

 // corrected Image
 correctedImage := originalMat.ToBytes()

 for i := 0; i < len(correctedImage); i++ {
  correctedValue := uint8(math.Pow(float64(correctedImage[i])/255, GAMMA) * 255)
  correctedImage[i] = correctedValue
 }

 correctedMat, err := gocv.NewMatFromBytes(originalMat.Rows(), originalMat.Cols(), gocv.IMReadGrayScale, correctedImage)
 if err != nil {
  log.Panic("Can not convert bytes to Mat")
  return
 }

 windowForCorrect := gocv.NewWindow("Corrected Image")
 defer windowForCorrect.Close()
 windowForCorrect.IMShow(correctedMat)

 // wait
 for {
  key := gocv.WaitKey(3)
  if key == 27 {
   log.Println("Pressed ESC")
   break
  }
 }
}

Execute Result

Comments

Popular posts from this blog

[OpenCV] What is gocv?

[OpenCV] Chapter01-05 Working with UI elements such as scrollbars in OpenCV window

[OpenCV] How to set up gocv