Golang image processing: learn how to stretch and geometrically transform images

PHPz
Release: 2023-08-17 13:01:03
Original
653 people have browsed it

Golang image processing: learn how to stretch and geometrically transform images

Golang Image Processing: Learn how to stretch and geometrically transform images

As a powerful programming language, Golang also has many rich functions in image processing . In this article, we will learn how to use Golang to stretch and geometrically transform images.

First, we need to import related packages such as image and gonum.org/v1/plot. Below is a simple example program for reading, resizing, and saving images.

package main

import (
    "image"
    "image/color"
    "image/jpeg"
    "image/png"
    "log"
    "math"
    "os"

    "github.com/gonum/matrix/mat64"
)

// 读取图像文件
func loadImage(filename string) (image.Image, error) {
    file, err := os.Open(filename)
    if err != nil {
        return nil, err
    }
    defer file.Close()
    img, _, err := image.Decode(file)
    return img, err
}

// 保存图像文件
func saveImage(img image.Image, format, filename string) error {
    file, err := os.Create(filename)
    if err != nil {
        return err
    }
    defer file.Close()
    switch format {
    case "jpeg":
        return jpeg.Encode(file, img, &jpeg.Options{Quality: 100})
    case "png":
        return png.Encode(file, img)
    default:
        return nil
    }
}

// 拉伸图像
func stretchImage(img image.Image, width, height int) image.Image {
    newImg := image.NewRGBA(image.Rect(0, 0, width, height))
    for y := 0; y < height; y++ {
        for x := 0; x < width; x++ {
            srcX := int(float64(x) / float64(width) * float64(img.Bounds().Dx()))
            srcY := int(float64(y) / float64(height) * float64(img.Bounds().Dy()))
            newImg.Set(x, y, img.At(srcX, srcY))
        }
    }
    return newImg
}

// 几何变换
func geometricTransformation(img image.Image, theta float64) image.Image {
    width, height := img.Bounds().Dx(), img.Bounds().Dy()
    cosTheta := math.Cos(theta)
    sinTheta := math.Sin(theta)
    M := mat64.NewDense(3, 3, nil)
    M.Set(0, 0, cosTheta)
    M.Set(0, 1, sinTheta)
    M.Set(0, 2, 0)
    M.Set(1, 0, -sinTheta)
    M.Set(1, 1, cosTheta)
    M.Set(1, 2, 0)
    M.Set(2, 0, 0)
    M.Set(2, 1, 0)
    M.Set(2, 2, 1)
    newImg := image.NewRGBA(image.Rect(0, 0, width, height))
    for y := 0; y < height; y++ {
        for x := 0; x < width; x++ {
            v := mat64.NewVector(3, []float64{float64(x), float64(y), 1})
            res := mat64.NewVector(3, nil)
            res.MulVec(M, v)
            srcX := int(res.At(0, 0) / res.At(2, 0))
            srcY := int(res.At(1, 0) / res.At(2, 0))
            if srcX >= 0 && srcX < width && srcY >= 0 && srcY < height {
                newImg.Set(x, y, img.At(srcX, srcY))
            } else {
                newImg.Set(x, y, color.White)
            }
        }
    }
    return newImg
}

func main() {
    // 读取图像
    img, err := loadImage("input.jpg")
    if err != nil {
        log.Fatal(err)
    }

    // 拉伸图像
    stretchedImg := stretchImage(img, 500, 500)
    err = saveImage(stretchedImg, "jpeg", "stretched.jpg")
    if err != nil {
        log.Fatal(err)
    }

    // 几何变换
    geometricImg := geometricTransformation(img, 0.5)
    err = saveImage(geometricImg, "jpeg", "geometric.jpg")
    if err != nil {
        log.Fatal(err)
    }
}
Copy after login

In the above code, we defined the loadImage function to read image files, and the saveImage function to save image files. At the same time, we also defined the stretchImage function and the geometricTransformation function for image stretching and geometric transformation respectively.

In the main function, we first read an image named input.jpg. Next, we call the stretchImage function to stretch the image to a size of 500x500 and save it as a stretched.jpg file. Then, we call the geometricTransformation function to perform geometric transformation and save the result as a geometric.jpg file.

You can adjust the parameters and functions in the code according to your own needs to achieve more detailed image processing. I hope this article can help you learn how to use Golang to stretch and geometrically transform images.

The above is the detailed content of Golang image processing: learn how to stretch and geometrically transform images. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template