https://github.com/carbocation/matriximage
Golang utility to convert between (grayscale) images and their Fourier transforms, and to manipulate them in frequency space
https://github.com/carbocation/matriximage
Last synced: 5 months ago
JSON representation
Golang utility to convert between (grayscale) images and their Fourier transforms, and to manipulate them in frequency space
- Host: GitHub
- URL: https://github.com/carbocation/matriximage
- Owner: carbocation
- License: mit
- Created: 2015-12-17T18:53:26.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-12-21T14:52:53.000Z (over 10 years ago)
- Last Synced: 2024-06-20T16:33:13.457Z (about 2 years ago)
- Language: Go
- Size: 28.3 KB
- Stars: 10
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# matriximage
Golang utility to convert between (grayscale) images and their Fourier transforms, and to manipulate them in frequency space
```sh
go get github.com/carbocation/matriximage
```
##Example:
```go
package main
import (
"fmt"
"math"
"os"
"testing"
"github.com/mjibson/go-dsp/dsputils"
)
// Work with the sine wave image
const Case = "sin"
const MaxUint = math.MaxUint16
func main() {
Basic()
Masked()
}
// Reads an image, performs a Fourier transform, then outputs various components
// as visible images
func Basic() {
// Read an image from file. This helper function also converts it to Gray16
m, err := FromFile(fmt.Sprintf("./images/%s.gif", Case))
if err != nil {
t.Fatal(err)
}
// Take the 2D Fourier transform of the image
fourier := m.DFT()
// Write the amplitude, (brightened) amplitude, phase, and then recombined gray image to disk
save(*fourier.AmplitudeImage(), "amp")
save(*fourier.BrighterAmplitudeImage(), "logamp")
save(*fourier.PhaseImage(), "phase")
save(*fourier.IDFTImage(), "recovered")
}
// Reads an image, applies a mask in the frequency space, performs the inverse
// Fourier transform, and writes the resulting recovered image to disk
func Masked() {
// Read an image from file. This helper function also converts it to Gray16
m, err := FromFile(fmt.Sprintf("./images/%s.gif", Case))
if err != nil {
t.Fatal(err)
}
// Take the 2D Fourier transform of the image
fourier := m.DFT()
// Create a square mask, where only the middle part of the amplitude is recovered.
maskMatrix := dsputils.MakeEmptyMatrix(fourier.Dimensions()) //fourier.AmplitudeImage().toGrayMatrix()
dims := maskMatrix.Dimensions()
for y := 0; y < dims[0]; y++ {
for x := 0; x < dims[1]; x++ {
rad := 1
if (y < dims[0]/2-rad || y > dims[0]/2+rad) || (x < dims[1]/2-rad || x > 1+dims[1]/2+rad) {
maskMatrix.SetValue(complex(0, 0), []int{y, x})
} else {
maskMatrix.SetValue(complex(float64(MaxUint), 0), []int{y, x})
}
}
}
maskedFourier, err := fourier.ApplyMatrixMask(maskMatrix)
if err != nil {
t.Fatal(err)
}
save(*maskedFourier.AmplitudeImage(), "masked-amplitude")
save(*maskedFourier.BrighterAmplitudeImage(), "masked-brighter-amplitude")
save(*maskedFourier.PhaseImage(), "masked-phase")
save(*maskedFourier.IDFTImage(), "masked-recovered")
}
func save(m Image, name string) {
os.MkdirAll("./images.out", 0755)
filename := fmt.Sprintf("./images.out/%s-%s.png", Case, name)
m.ToFile(filename)
}
```