Home > Article > Backend Development > How to use Golang to equalize brightness and decolorize images
How to use Golang to equalize brightness and decolorize images
1. Background
With the development of image processing technology, images are processed Brightness equalization and decolorization have become common requirements. Brightness equalization refers to adjusting the brightness of an image to a suitable range to improve the visual effect of the image. The decolorization process removes the color information of the image and retains only the grayscale information, which is used for some special application scenarios.
This article will introduce how to use the Golang programming language to achieve brightness equalization and decolorization of images.
2. Brightness Balance
The goal of brightness balance is to adjust the brightness range of the image from the darker or brighter extremes to a suitable range. A common brightness equalization algorithm is histogram equalization.
The following is a sample code that demonstrates how to use Golang to implement the histogram equalization algorithm:
package main import ( "image" "image/color" "image/jpeg" "log" "os" ) func main() { // 打开图片文件 file, err := os.Open("input.jpg") if err != nil { log.Fatal(err) } defer file.Close() // 解码图片 img, err := jpeg.Decode(file) if err != nil { log.Fatal(err) } // 创建一个新的灰度图像 bounds := img.Bounds() gray := image.NewGray(bounds) // 计算每个灰度级的像素个数 count := make([]int, 256) for y := bounds.Min.Y; y < bounds.Max.Y; y++ { for x := bounds.Min.X; x < bounds.Max.X; x++ { grayColor := color.GrayModel.Convert(img.At(x, y)).(color.Gray) count[grayColor.Y]++ } } // 计算累积分布函数 cdf := make([]int, 256) cdf[0] = count[0] for i := 1; i < 256; i++ { cdf[i] = cdf[i-1] + count[i] } // 将累积分布函数映射到[0, 255]的范围 for i := 0; i < 256; i++ { cdf[i] = cdf[i] * 255 / (bounds.Dx() * bounds.Dy()) } // 对每个像素进行亮度均衡化处理 for y := bounds.Min.Y; y < bounds.Max.Y; y++ { for x := bounds.Min.X; x < bounds.Max.X; x++ { grayColor := color.GrayModel.Convert(img.At(x, y)).(color.Gray) grayColor.Y = uint8(cdf[grayColor.Y]) gray.Set(x, y, grayColor) } } // 保存处理后的图像 outputFile, err := os.Create("output.jpg") if err != nil { log.Fatal(err) } defer outputFile.Close() err = jpeg.Encode(outputFile, gray, nil) if err != nil { log.Fatal(err) } }
In the above code, we first open an image file and then decode it into an image object. Next, we create a new grayscale image object and count the number of pixels for each grayscale level in the original image. Next, we calculated the cumulative distribution function and mapped it to the range [0, 255]. Finally, we perform brightness equalization on each pixel and save the processed image.
3. Decolorization
Decolorization removes the color information of the image and only retains the grayscale information. The following is a sample code that demonstrates how to use Golang to implement decolorization:
package main import ( "image" "image/color" "image/jpeg" "log" "os" ) func main() { // 打开图片文件 file, err := os.Open("input.jpg") if err != nil { log.Fatal(err) } defer file.Close() // 解码图片 img, err := jpeg.Decode(file) if err != nil { log.Fatal(err) } // 创建一个新的灰度图像 bounds := img.Bounds() gray := image.NewGray(bounds) // 对每个像素进行去色处理 for y := bounds.Min.Y; y < bounds.Max.Y; y++ { for x := bounds.Min.X; x < bounds.Max.X; x++ { grayColor := color.GrayModel.Convert(img.At(x, y)).(color.Gray) grayColor.Y = uint8(grayColor.Y) gray.Set(x, y, grayColor) } } // 保存处理后的图像 outputFile, err := os.Create("output.jpg") if err != nil { log.Fatal(err) } defer outputFile.Close() err = jpeg.Encode(outputFile, gray, nil) if err != nil { log.Fatal(err) } }
In the above code, we use the same method to open and decode an image file and create a new grayscale image object. Then, we decolorize each pixel, that is, set the color information of the pixel to the grayscale information of the pixel. Finally, we save the processed image.
4. Summary
This article introduces how to use Golang to achieve brightness equalization and decolorization of images. Through the histogram equalization algorithm and decolorization processing algorithm, we can effectively adjust the brightness of the image and remove the color information of the image. Using Golang to implement image processing functions can quickly and efficiently process large amounts of image data to meet the needs of various practical application scenarios. I hope this article can help readers understand and apply image processing algorithms.
The above is the detailed content of How to use Golang to equalize brightness and decolorize images. For more information, please follow other related articles on the PHP Chinese website!