


Golang image manipulation: How to perform color balance and color conversion on images
Golang image operation: How to perform color balance and color conversion of pictures
Introduction: In the field of image processing, color balance and color conversion are one of the commonly used operations. This article will introduce how to use Go language to perform color balance and color conversion of pictures, and provide corresponding code examples.
1. Color balance
Color balance refers to adjusting the intensity of each color channel in the image to make the overall color of the image more uniform and natural. Commonly used color balance algorithms include brightness balance, white balance and histogram equalization.
- Brightness balance
Brightness balance is achieved by adjusting the distribution of image brightness. The following is a simple sample code to achieve brightness balance:
package main import ( "image" "image/color" "image/png" "os" ) func brightnessBalance(img image.Image) image.Image { width := img.Bounds().Dx() height := img.Bounds().Dy() balanceImg := image.NewRGBA(img.Bounds()) for y := 0; y < height; y++ { for x := 0; x < width; x++ { r, g, b, a := img.At(x, y).RGBA() r = r * 2 g = g * 2 b = b * 2 balanceImg.Set(x, y, color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}) } } return balanceImg } func main() { file, err := os.Open("input.png") if err != nil { panic(err) } defer file.Close() img, _, err := image.Decode(file) if err != nil { panic(err) } balanceImg := brightnessBalance(img) outputFile, err := os.Create("output.png") if err != nil { panic(err) } defer outputFile.Close() err = png.Encode(outputFile, balanceImg) if err != nil { panic(err) } }
In the above code, we loop through each pixel of the image and combine the red, green and blue channels of each pixel. The value is multiplied by 2 to achieve an increase in overall brightness. By loading the original image and saving the processed image, we can get a color-balanced image.
- White balance
White balance is to eliminate the color cast caused by uneven lighting in the picture. The following is a simple sample code to achieve white balance:
package main import ( "image" "image/color" "image/png" "math" "os" ) func whiteBalance(img image.Image) image.Image { width := img.Bounds().Dx() height := img.Bounds().Dy() whiteBalanceImg := image.NewRGBA(img.Bounds()) var sumR, sumG, sumB float64 for y := 0; y < height; y++ { for x := 0; x < width; x++ { r, g, b, a := img.At(x, y).RGBA() sumR += math.Log(float64(r)) sumG += math.Log(float64(g)) sumB += math.Log(float64(b)) } } avgR := math.Exp(sumR / (float64(width * height))) avgG := math.Exp(sumG / (float64(width * height))) avgB := math.Exp(sumB / (float64(width * height))) for y := 0; y < height; y++ { for x := 0; x < width; x++ { r, g, b, a := img.At(x, y).RGBA() r = uint32(math.Log(float64(r)) * avgR / float64(r)) g = uint32(math.Log(float64(g)) * avgG / float64(g)) b = uint32(math.Log(float64(b)) * avgB / float64(b)) whiteBalanceImg.Set(x, y, color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}) } } return whiteBalanceImg } func main() { file, err := os.Open("input.png") if err != nil { panic(err) } defer file.Close() img, _, err := image.Decode(file) if err != nil { panic(err) } whiteBalanceImg := whiteBalance(img) outputFile, err := os.Create("output.png") if err != nil { panic(err) } defer outputFile.Close() err = png.Encode(outputFile, whiteBalanceImg) if err != nil { panic(err) } }
In the above code, we calculate the average of the logarithmic values of all pixels in the image and divide the logarithmic value of each pixel White balance is achieved by multiplying the average and then performing an exponential operation. Similarly, by loading the original image and saving the processed image, we can get the white balanced image.
2. Color conversion
Color conversion refers to converting colors in one color space into colors in another color space. Commonly used color conversions include RGB to HSV and RGB to YUV.
- RGB to HSV conversion
RGB and HSV are two common color spaces. RGB is used to represent the three color components of red, green, and blue, and HSV is used to represent the hue of the color. , saturation and brightness three attributes.
The following is a simple sample code to convert RGB colors to HSV colors:
package main import ( "fmt" "image/color" ) func rgbToHsv(r, g, b uint8) (uint16, uint8, uint8) { var h, s, v uint16 max := uint16(r) if uint16(g) > max { max = uint16(g) } if uint16(b) > max { max = uint16(b) } min := uint16(r) if uint16(g) < min { min = uint16(g) } if uint16(b) < min { min = uint16(b) } v = max delta := max - min if max != 0 { s = uint8(delta) * 255 / uint8(max) } else { s = 0 } if delta != 0 { if max == uint16(r) { h = (uint16(g) - uint16(b)) * 60 / delta if uint16(g) < uint16(b) { h += 360 } } else if max == uint16(g) { h = (2 + (uint16(b)-uint16(r))/delta) * 60 } else { h = (4 + (uint16(r)-uint16(g))/delta) * 60 } } else { h = 0 } return h, s, uint8(v) } func main() { r := uint8(255) g := uint8(0) b := uint8(0) h, s, v := rgbToHsv(r, g, b) fmt.Printf("RGB(%d, %d, %d) -> HSV(%d, %d, %d) ", r, g, b, h, s, v) }
In the above code, we pass a series of Calculate and calculate the value of the corresponding HSV color component. We output a pure red RGB color by setting the value of the RGB component to the maximum value of red, and calculate the corresponding HSV color.
- RGB to YUV conversion
YUV is also a common color space, where Y represents brightness and U and V represent chroma. The following is a simple example code for converting RGB colors to YUV colors:
package main import ( "fmt" "image/color" ) func rgbToYuv(r, g, b uint8) (uint8, uint8, uint8) { y := uint8(float32(r)*0.299 + float32(g)*0.587 + float32(b)*0.114) u := uint8((-float32(r)*0.14713 - float32(g)*0.28886 + float32(b)*0.436 + 128) / 2) v := uint8((float32(r)*0.615 + float32(g)*0.51499 - float32(b)*0.10001 + 128) / 2) return y, u, v } func main() { r := uint8(255) g := uint8(0) b := uint8(0) y, u, v := rgbToYuv(r, g, b) fmt.Printf("RGB(%d, %d, %d) -> YUV(%d, %d, %d) ", r, g, b, y, u, v) }
In the above code, we calculate the corresponding values through a series of calculations based on the values of the RGB color components. The value of the YUV color component. Similarly, we output a pure red RGB color by setting the value of the RGB component to the maximum value of red, and calculate the corresponding YUV color.
Conclusion: This article introduces the method of color balance and color conversion of images using Go language, and provides corresponding code examples. I hope readers will have a deeper understanding of Golang image operations through this article and be able to apply it to actual projects.
The above is the detailed content of Golang image manipulation: How to perform color balance and color conversion on images. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use Python to adjust the color balance of pictures Introduction: In image processing, color balance is a common operation. By adjusting the color balance of an image, you can change the overall tone of the image to make it more suitable for your needs. This article will introduce how to use Python language to adjust the color balance of pictures, and provide code examples to help readers quickly implement it. 1. Principle of color balance Color balance is the operation of adjusting the intensity of different color channels in an image to achieve an overall color effect. Generally speaking, the color of the image is determined by red

Golang image manipulation: How to mirror, rotate and flip images 1. Introduction Image processing is one of the needs we often encounter in many development scenarios. In Golang, we can use the image package to operate and process images. This article will focus on how to use Golang to mirror, rotate and flip images, and provide corresponding code examples. 2. Mirroring operation Mirroring a picture is to change the left and right layout of the picture. In Golang, you can use Fli of the draw package

PHP Image Operation: How to Get the Size and File Size of Images In developing websites or applications, we often need to process images. Obtaining the size and file size of images is a common requirement, which can be easily achieved through some functions in PHP. This article will introduce how to use PHP to obtain the size and file size of images, and attach a code example. Get the image size To get the image size, you can use PHP's built-in function getimagesize(). This function will return a file containing the image size

Golang image operation: How to perform color balance and color conversion on images Introduction: In the field of image processing, color balance and color conversion are one of the commonly used operations. This article will introduce how to use Go language to perform color balance and color conversion of pictures, and provide corresponding code examples. 1. Color balance Color balance refers to adjusting the intensity of each color channel in the image to make the overall color of the image more uniform and natural. Commonly used color balance algorithms include brightness balance, white balance and histogram equalization. Brightness balanceBrightness balance is achieved by adjusting the graph

Golang image manipulation: How to perform gradient and texture mapping on images Overview: In image processing, gradient and texture mapping are two commonly used techniques. Gradients can create smooth transitions of color effects, while texture mapping can map a texture image to a target image. This article will introduce how to use the Golang programming language to perform gradient and texture mapping operations on images. Image gradient First, we need to import Golang's image processing packages image and image/color. The following is a sample code, created by

Golang connects to Baidu AI interface to realize image analysis function, easy to get started. Summary: This article will introduce how to use Golang to connect to Baidu AI interface to realize image analysis function. We will use the image recognition interface provided by Baidu AI open platform to implement label recognition and text recognition functions for images by writing code in Golang language. Before starting the preparation work, we need to prepare the following tools and materials: a Baidu developer account to obtain the APIKey and Se to access the Baidu AI interface

Golang Image Manipulation: How to Adjust Brightness and Contrast Introduction: In image processing, adjusting the brightness and contrast of an image is a very common task. By adjusting the brightness we can make the image brighter or darker. And by adjusting contrast, we can increase or decrease the color differences in an image. This article will introduce how to use Golang to adjust the brightness and contrast of images and provide code examples. Import the necessary packages First, we need to import the image and color packages to handle the reading and saving of images.

Golang Image Operation: How to Detect and Repair Brokenness and Missing Images Introduction: In daily development, image manipulation is a common task. In the process of processing pictures, we often encounter some problems, such as picture disconnection or missing pictures. How to detect and fix these problems quickly and accurately is a topic we need to discuss. Article content: This article will use Golang to demonstrate how to detect and repair broken lines and missing images. For better understanding, we will explain it in two parts. Part One: Detection Chart
