Home  >  Article  >  Backend Development  >  Golang image processing: how to color balance and histogram equalization of pictures

Golang image processing: how to color balance and histogram equalization of pictures

王林
王林Original
2023-08-20 16:21:371390browse

Golang image processing: how to color balance and histogram equalization of pictures

Golang image processing: How to perform color balance and histogram equalization of pictures

Introduction:
In the field of image processing, color balance and histogram equalization are two commonly used techniques. Color balancing is used to adjust the distribution of colors in an image for a more natural look, while histogram equalization is used to improve the contrast and brightness distribution of an image. This article will introduce how to use Golang to perform color balance and histogram equalization of images, and provide corresponding code examples.

  1. Color Balance
    Color balance can be achieved by adjusting the RGB channels of the image. Specifically, we can achieve color balance by modifying the color distribution of the image to make it more uniform.

The following is a code example for image color balancing using Golang:

// 导入所需的包
import (
    "image"
    "image/color"
    "math"
)

// 颜色平衡函数
func balanceColors(img image.Image) image.Image {
    bounds := img.Bounds()
    width, height := bounds.Max.X, bounds.Max.Y

    // 创建一个新的RGBA图像
    balancedImg := image.NewRGBA(image.Rect(0, 0, width, height))

    rTotal, gTotal, bTotal := 0, 0, 0
    numPixels := width * height

    // 遍历图像的每一个像素
    for y := 0; y < height; y++ {
        for x := 0; x < width; x++ {
            // 获取像素的RGB值
            r, g, b, _ := img.At(x, y).RGBA()

            // 累加RGB值
            rTotal += int(r >> 8)
            gTotal += int(g >> 8)
            bTotal += int(b >> 8)
        }
    }

    // 计算平均RGB值
    rAvg := float64(rTotal) / float64(numPixels)
    gAvg := float64(gTotal) / float64(numPixels)
    bAvg := float64(bTotal) / float64(numPixels)

    // 遍历图像的每一个像素
    for y := 0; y < height; y++ {
        for x := 0; x < width; x++ {
            // 获取像素的RGB值
            r, g, b, a := img.At(x, y).RGBA()

            // 计算调整后的RGB值
            rBalanced := uint8(math.Min(float64(r>>8)*(rAvg/255), 255))
            gBalanced := uint8(math.Min(float64(g>>8)*(gAvg/255), 255))
            bBalanced := uint8(math.Min(float64(b>>8)*(bAvg/255), 255))

            // 设置新图像的像素值
            balancedImg.Set(x, y, color.RGBA{rBalanced, gBalanced, bBalanced, uint8(a>>8)})
        }
    }

    return balancedImg
}
  1. Histogram equalization
    Histogram equalization is a method of converting the pixel values ​​of an image into A technique that redistributes images to improve image contrast and brightness. It can be achieved by statistics and conversion of the gray value of the image.

The following is a code example of using Golang for image histogram equalization:

// 导入所需的包
import (
    "image"
    "image/color"
    "math"
)

// 直方图均衡化函数
func equalizeHistogram(img image.Image) image.Image {
    bounds := img.Bounds()
    width, height := bounds.Max.X, bounds.Max.Y

    // 创建一个新的RGBA图像
    equalizedImg := image.NewRGBA(image.Rect(0, 0, width, height))

    // 计算像素值的累计分布
    var hist [256]int
    for y := 0; y < height; y++ {
        for x := 0; x < width; x++ {
            // 获取像素的灰度值
            r, g, b, _ := img.At(x, y).RGBA()
            gray := color.GrayModel.Convert(color.RGBA{uint8(r>>8), uint8(g>>8), uint8(b>>8), 0}).(color.Gray)

            // 累加灰度值分布
            hist[gray.Y]++
        }
    }

    // 计算像素值的累积直方图
    var cumHist [256]int
    cumHist[0] = hist[0]
    for i := 1; i < 256; i++ {
        cumHist[i] = cumHist[i-1] + hist[i]
    }

    // 计算像素值的映射关系
    var mapping [256]uint8
    for i := 0; i < 256; i++ {
        mapping[i] = uint8(math.Round(float64(cumHist[i]) * 255 / float64(width*height)))
    }

    // 遍历图像的每一个像素
    for y := 0; y < height; y++ {
        for x := 0; x < width; x++ {
            // 获取像素的灰度值
            r, g, b, a := img.At(x, y).RGBA()
            gray := color.GrayModel.Convert(color.RGBA{uint8(r>>8), uint8(g>>8), uint8(b>>8), uint8(a>>8)}).(color.Gray)

            // 获取映射后的灰度值
            newGray := mapping[gray.Y]

            // 设置新图像的像素值
            equalizedColor := color.Gray{newGray}
            equalizedImg.Set(x, y, equalizedColor)
        }
    }

    return equalizedImg
}

Conclusion:
This article introduces how to use Golang for image color balance and histogram equalization ization, and provides corresponding code examples. Color balancing and histogram equalization are two commonly used image processing techniques that can help improve the color distribution, contrast, and brightness of an image. Readers can flexibly use these technologies to process images according to their own needs and actual conditions to obtain better visual effects.

The above is the detailed content of Golang image processing: how to color balance and histogram equalization of pictures. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn