matriximage

package module
v0.0.0-...-0221d3d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2015 License: MIT Imports: 11 Imported by: 0

README

matriximage

Golang utility to convert between (grayscale) images and their Fourier transforms, and to manipulate them in frequency space

go get github.com/carbocation/matriximage

##Example:

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)
}

Documentation

Index

Constants

View Source
const (
	Real = iota
	RealFromDFT
	Amplitude
	BrighterAmplitude
	Phase
)
View Source
const MaxUint = math.MaxUint16

Variables

This section is empty.

Functions

func ImageToGray

func ImageToGray(m image.Image) *image.Gray16

Types

type FourierImage

type FourierImage struct {
	*dsputils.Matrix // Is expected to be the result of a Fourier transform
}

func (FourierImage) AmplitudeImage

func (f FourierImage) AmplitudeImage() *Image

For making masks

func (FourierImage) ApplyImageMask

func (f FourierImage) ApplyImageMask(img *Image) (*FourierImage, error)

Generate a new FourierImage by applying a mask to the current one The mask is assumed to have its frequency origin at the center, which is shifted by N/2 and M/2 from the top left origin

func (FourierImage) ApplyMatrixMask

func (f FourierImage) ApplyMatrixMask(mask *dsputils.Matrix) (*FourierImage, error)

ApplyMatrixMask applies a mask to the fourier matrix. It expects the mask to be of the same dimensions as the fourier matrix, and to contain real values ranging from [0...1]. Any imaginary component is ignored.

func (FourierImage) BrighterAmplitudeImage

func (f FourierImage) BrighterAmplitudeImage() *Image

For making masks

func (FourierImage) IDFTImage

func (f FourierImage) IDFTImage() *Image

Inverse FFT image output

func (FourierImage) PhaseImage

func (f FourierImage) PhaseImage() *Image

For... visualization?

type Image

type Image struct {
	image.Image
}

func FromFile

func FromFile(filename string) (*Image, error)

func (Image) DFT

func (m Image) DFT() FourierImage

func (Image) ToFile

func (m Image) ToFile(named string) error

func (Image) ToGrayMatrix

func (m Image) ToGrayMatrix() *dsputils.Matrix

Work with gray for now Returns a matrix without rescaling values

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL