[go] Go 언어를 이용한 영상 밝기 및 대비 조절 기능

본 문서에서는 Go 언어를 사용하여 영상의 밝기와 대비를 조절하는 방법에 대해 알아보겠습니다.

필요한 라이브러리 가져오기

먼저, 영상 처리에 필요한 기능을 제공하는 image 라이브러리를 가져와야 합니다.

import (
    "image"
    "image/color"
    "image/jpeg"
    "os"
)

영상 불러오기

변경할 영상을 불러옵니다.

// 이미지 파일 열기
file, err := os.Open("input.jpg")
if err != nil {
    log.Fatal(err)
}
defer file.Close()

// 이미지 디코드
img, _, err := image.Decode(file)
if err != nil {
    log.Fatal(err)
}

밝기와 대비 조절

영상의 밝기와 대비를 조절하는 간단한 함수를 작성합니다.

func adjustBrightnessAndContrast(img image.Image, brightness float64, contrast float64) image.Image {
    bounds := img.Bounds()
    newImg := image.NewRGBA(bounds)

    // 각 픽셀별로 밝기와 대비 조절
    for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
        for x := bounds.Min.X; x < bounds.Max.X; x++ {
            oldPixel := img.At(x, y)
            r, g, b, a := oldPixel.RGBA()

            // 밝기 조절
            r = uint32(math.Max(math.Min(float64(r)*contrast+brightness, 65535), 0))
            g = uint32(math.Max(math.Min(float64(g)*contrast+brightness, 65535), 0))
            b = uint32(math.Max(math.Min(float64(b)*contrast+brightness, 65535), 0))

            // 새로운 픽셀 값 설정
            newPixel := color.RGBA64{uint16(r), uint16(g), uint16(b), uint16(a)}
            newImg.Set(x, y, newPixel)
        }
    }

    return newImg
}

결과 이미지 저장

조절된 영상을 저장합니다.

// 결과 이미지 파일 생성
outFile, err := os.Create("output.jpg")
if err != nil {
    log.Fatal(err)
}
defer outFile.Close()

// 이미지 인코딩
jpeg.Encode(outFile, adjustedImg, nil)

결론

위의 코드를 활용하여 Go 언어를 사용하여 영상의 밝기와 대비를 조절할 수 있습니다. 이러한 기능은 영상 처리 및 컴퓨터 비전 애플리케이션에서 유용하게 활용될 수 있습니다.

관련 자료: Go 언어 이미지 패키지, 고효율 이미지 처리용 Go 언어 라이브러리