首頁 > 後端開發 > Golang > 主體

Golang圖片操作:學習如何進行圖片的直方圖均衡化和全域閾值化

王林
發布: 2023-08-18 14:49:52
原創
647 人瀏覽過

Golang圖片操作:學習如何進行圖片的直方圖均衡化和全域閾值化

Golang圖片操作:學習如何進行圖片的直方圖均衡化和全局閾值化

引言:
圖片處理是電腦視覺和圖像處理領域中的重要任務之一。在實際應用中,我們常常需要進行一些影像增強操作,以提高影像的品質或突顯影像中的某些特徵。本文將介紹如何使用Golang進行影像的直方圖均衡化和全域閾值化操作,以實現影像增強的目的。

一、直方圖均衡化
直方圖均衡化是一種常用的影像增強方法,它透過對影像像素的灰階分佈進行調整,使得影像的對比度得到增強。在這種方法中,我們首先計算影像的累積直方圖,然後根據累積直方圖對影像進行像素值的調整。

下面是一個簡單的Golang程式碼範例,用於實現圖像的直方圖均衡化:

package main

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

func main() {
    // 打开图片文件
    file, err := os.Open("input.jpg")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer file.Close()

    // 解码图片
    img, _, err := image.Decode(file)
    if err != nil {
        fmt.Println(err)
        return
    }

    // 计算直方图
    hist := histogram(img)

    // 计算累积直方图
    cumHist := cumulativeHistogram(hist)

    // 根据累积直方图对图像进行像素值调整
    newImg := adjustPixels(img, cumHist)

    // 保存处理后的图像
    outFile, err := os.Create("output.jpg")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer outFile.Close()

    // 编码图像
    err = jpeg.Encode(outFile, newImg, &jpeg.Options{Quality: 100})
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println("图像处理完成!")
}

// 计算直方图
func histogram(img image.Image) []int {
    bounds := img.Bounds()
    w, h := bounds.Max.X, bounds.Max.Y
    hist := make([]int, 256)

    for y := 0; y < h; y++ {
        for x := 0; x < w; x++ {
            r, _, _, _ := img.At(x, y).RGBA()
            gray := color.Gray{uint8(r / 256)}
            hist[gray.Y]++
        }
    }

    return hist
}

// 计算累积直方图
func cumulativeHistogram(hist []int) []int {
    cumHist := make([]int, len(hist))
    cumHist[0] = hist[0]

    for i := 1; i < len(hist); i++ {
        cumHist[i] = cumHist[i-1] + hist[i]
    }

    return cumHist
}

// 根据累积直方图调整像素值
func adjustPixels(img image.Image, cumHist []int) image.Image {
    bounds := img.Bounds()
    w, h := bounds.Max.X, bounds.Max.Y
    newImg := image.NewRGBA(bounds)

    for y := 0; y < h; y++ {
        for x := 0; x < w; x++ {
            r, g, b, a := img.At(x, y).RGBA()

            gray := color.Gray{uint8(r / 256)}
            val := uint8(float64(cumHist[gray.Y]) / float64(w*h) * 255)

            newImg.Set(x, y, color.RGBA{val, val, val, uint8(a / 256)})
        }
    }

    return newImg
}
登入後複製

在上述程式碼中,我們首先透過image套件的Decode函數將輸入影像檔案解碼為image.Image類型的物件。然後,我們分別呼叫histogram函數計算圖像的直方圖,cumulativeHistogram函數計算圖像的累積直方圖。最後,我們根據累積直方圖調整影像的像素值,並使用jpeg#包的Encode函數將處理後的影像儲存到檔案中。

二、全域閾值化
全域閾值化是一種簡單但有效的影像二值化方法,它將影像的像素值分為兩個互不重疊的光滑區域,分別代表目標物件和背景。這種方法通常應用於具有明顯的前景和背景差異的影像。

下面是一個簡單的Golang程式碼範例,用於實現圖像的全域閾值化:

package main

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

func main() {
    // 打开图片文件
    file, err := os.Open("input.jpg")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer file.Close()

    // 解码图片
    img, _, err := image.Decode(file)
    if err != nil {
        fmt.Println(err)
        return
    }

    // 根据全局阈值对图像进行二值化处理
    newImg := binarize(img)

    // 保存处理后的图像
    outFile, err := os.Create("output.jpg")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer outFile.Close()

    // 编码图像
    err = jpeg.Encode(outFile, newImg, &jpeg.Options{Quality: 100})
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println("图像处理完成!")
}

// 根据全局阈值对图像进行二值化处理
func binarize(img image.Image) image.Image {
    bounds := img.Bounds()
    w, h := bounds.Max.X, bounds.Max.Y
    newImg := image.NewRGBA(bounds)

    threshold := calculateThreshold(img)

    for y := 0; y < h; y++ {
        for x := 0; x < w; x++ {
            r, g, b, a := img.At(x, y).RGBA()

            gray := color.Gray{uint8(r / 256)}
            var val uint8
            if gray.Y > threshold {
                val = 255
            } else {
                val = 0
            }

            newImg.Set(x, y, color.RGBA{val, val, val, uint8(a / 256)})
        }
    }

    return newImg
}

// 根据图像的直方图计算全局阈值
func calculateThreshold(img image.Image) uint8 {
    hist := histogram(img)
    totalPixels := img.Bounds().Max.X * img.Bounds().Max.Y

    // 计算背景像素值的总和
    var bgSum, bgCount, fgSum, fgCount int
    for i := 0; i < len(hist); i++ {
        if i <= 128 {
            bgSum += i * hist[i]
            bgCount += hist[i]
        } else {
            fgSum += i * hist[i]
            fgCount += hist[i]
        }
    }

    // 计算背景和前景的平均灰度值
    bgMean := bgSum / bgCount
    fgMean := fgSum / fgCount

    // 根据背景和前景的平均灰度值计算阈值
    return uint8((bgMean + fgMean) / 2)
}

// 计算直方图
func histogram(img image.Image) []int {
    bounds := img.Bounds()
    w, h := bounds.Max.X, bounds.Max.Y
    hist := make([]int, 256)

    for y := 0; y < h; y++ {
        for x := 0; x < w; x++ {
            r, _, _, _ := img.At(x, y).RGBA()
            gray := color.Gray{uint8(r / 256)}
            hist[gray.Y]++
        }
    }

    return hist
}
登入後複製

在上述程式碼中,我們首先透過image套件的Decode函數將輸入影像檔案解碼為image.Image類型的物件。然後,我們呼叫calculateThreshold函數計算影像的全域閾值。最後,我們根據全域閾值將影像進行二值化處理,並使用jpeg包的Encode函數將處理後的影像儲存到檔案中。

總結:
本文我們介紹如何使用Golang進行影像的直方圖均衡化和全域閾值化運算。直方圖均衡化可用於提高影像的對比度,使影像更加清晰和鮮明;全域閾值化可用於將影像轉換為二值影像,突出影像中的目標物件。透過靈活運用這兩種方法,我們可以實現對影像的增強和特徵提取,滿足各種應用需求。在實際應用中,我們可以結合其他影像處理演算法,進一步提升影像處理的效果和品質。

以上是Golang圖片操作:學習如何進行圖片的直方圖均衡化和全域閾值化的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!