Documentation
¶
Index ¶
- type Kernel
- func (k *Kernel) ApplyAvg(img image.Image, parallelism int) *image.NRGBA
- func (k *Kernel) ApplyMax(img image.Image, parallelism int) *image.NRGBA
- func (k *Kernel) ApplyMin(img image.Image, parallelism int) *image.NRGBA
- func (k *Kernel) Avg(img *image.NRGBA, x, y int) color.NRGBA
- func (k *Kernel) Max(img *image.NRGBA, x, y int) color.NRGBA
- func (k *Kernel) Min(img *image.NRGBA, x, y int) color.NRGBA
- func (k *Kernel) SetWeightRGBA(x, y int, r, g, b, a float32)
- func (k *Kernel) SetWeightUniform(x, y int, weight float32)
- func (k *Kernel) SetWeightsRGBA(weights [][4]float32)
- func (k *Kernel) SetWeightsUniform(weights []float32)
- func (k *Kernel) SideLength() int
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Kernel ¶
type Kernel struct {
// contains filtered or unexported fields
}
Example (ChannelExtraction) ¶
package main import ( "github.com/mandykoh/convolver" "image/png" "log" "os" "path" "runtime" "time" ) func main() { imgFile, err := os.Open("test-images/avocado.png") if err != nil { log.Panicf("Error opening input image: %v", err) } defer imgFile.Close() img, err := png.Decode(imgFile) if err != nil { log.Panicf("Error decoding PNG: %v", err) } kernel := convolver.KernelWithRadius(0) kernel.SetWeightRGBA(0, 0, 0, 0, 1, 1) startTime := time.Now() result := kernel.ApplyAvg(img, runtime.NumCPU()) endTime := time.Now() log.Printf("Channel extraction applied in %v", endTime.Sub(startTime)) _ = os.Mkdir("example-output", os.ModePerm) outFilePath := path.Join("example-output", "example-channel-extraction.png") outFile, err := os.Create(outFilePath) if err != nil { log.Panicf("Error creating output file: %v", err) } defer outFile.Close() err = png.Encode(outFile, result) if err != nil { log.Panicf("Error encoding output image: %v", err) } err = outFile.Close() if err != nil { log.Panicf("Error closing output file: %v", err) } log.Printf("Output written to %s", outFilePath) }
Output:
Example (DilateErode) ¶
package main import ( "github.com/mandykoh/convolver" "image/png" "log" "os" "path" "runtime" "time" ) func main() { const numPasses = 5 imgFile, err := os.Open("test-images/convolver-alpha-1024.png") if err != nil { log.Panicf("Error opening input image: %v", err) } defer imgFile.Close() img, err := png.Decode(imgFile) if err != nil { log.Panicf("Error decoding PNG: %v", err) } weights := []float32{ 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, } kernel := convolver.KernelWithRadius(2) kernel.SetWeightsUniform(weights) startTime := time.Now() result := img for i := 0; i < numPasses; i++ { result = kernel.ApplyMax(result, runtime.NumCPU()) } for i := 0; i < numPasses; i++ { result = kernel.ApplyMin(result, runtime.NumCPU()) } endTime := time.Now() log.Printf("Dilate-erode applied in %v", endTime.Sub(startTime)) _ = os.Mkdir("example-output", os.ModePerm) outFilePath := path.Join("example-output", "example-dilate-erode.png") outFile, err := os.Create(outFilePath) if err != nil { log.Panicf("Error creating output file: %v", err) } defer outFile.Close() err = png.Encode(outFile, result) if err != nil { log.Panicf("Error encoding output image: %v", err) } err = outFile.Close() if err != nil { log.Panicf("Error closing output file: %v", err) } log.Printf("Output written to %s", outFilePath) }
Output:
Example (EdgeDetect) ¶
package main import ( "github.com/mandykoh/convolver" "image/png" "log" "os" "path" "runtime" "time" ) func main() { imgFile, err := os.Open("test-images/avocado.png") if err != nil { log.Panicf("Error opening input image: %v", err) } defer imgFile.Close() img, err := png.Decode(imgFile) if err != nil { log.Panicf("Error decoding PNG: %v", err) } weights := []float32{ -1, -1, -1, -1, 8, -1, -1, -1, -1, } kernel := convolver.KernelWithRadius(1) kernel.SetWeightsUniform(weights) startTime := time.Now() result := kernel.ApplyAvg(img, runtime.NumCPU()) endTime := time.Now() log.Printf("Edge detection applied in %v", endTime.Sub(startTime)) _ = os.Mkdir("example-output", os.ModePerm) outFilePath := path.Join("example-output", "example-edge-detect.png") outFile, err := os.Create(outFilePath) if err != nil { log.Panicf("Error creating output file: %v", err) } defer outFile.Close() err = png.Encode(outFile, result) if err != nil { log.Panicf("Error encoding output image: %v", err) } err = outFile.Close() if err != nil { log.Panicf("Error closing output file: %v", err) } log.Printf("Output written to %s", outFilePath) }
Output:
Example (GaussianBlur) ¶
package main import ( "github.com/mandykoh/convolver" "image/png" "log" "os" "path" "runtime" "time" ) func main() { const numPasses = 8 imgFile, err := os.Open("test-images/avocado.png") if err != nil { log.Panicf("Error opening input image: %v", err) } defer imgFile.Close() img, err := png.Decode(imgFile) if err != nil { log.Panicf("Error decoding PNG: %v", err) } weights := []float32{ 1, 4, 6, 4, 1, 4, 16, 24, 16, 4, 6, 24, 36, 24, 6, 4, 16, 24, 16, 4, 1, 4, 6, 4, 1, } kernel := convolver.KernelWithRadius(2) kernel.SetWeightsUniform(weights) startTime := time.Now() result := img for i := 0; i < numPasses; i++ { result = kernel.ApplyAvg(result, runtime.NumCPU()) } endTime := time.Now() log.Printf("Gaussian blur applied in %v", endTime.Sub(startTime)) _ = os.Mkdir("example-output", os.ModePerm) outFilePath := path.Join("example-output", "example-gaussian-blur.png") outFile, err := os.Create(outFilePath) if err != nil { log.Panicf("Error creating output file: %v", err) } defer outFile.Close() err = png.Encode(outFile, result) if err != nil { log.Panicf("Error encoding output image: %v", err) } err = outFile.Close() if err != nil { log.Panicf("Error closing output file: %v", err) } log.Printf("Output written to %s", outFilePath) }
Output:
Example (Sharpen) ¶
package main import ( "github.com/mandykoh/convolver" "image/png" "log" "os" "path" "runtime" "time" ) func main() { imgFile, err := os.Open("test-images/avocado.png") if err != nil { log.Panicf("Error opening input image: %v", err) } defer imgFile.Close() img, err := png.Decode(imgFile) if err != nil { log.Panicf("Error decoding PNG: %v", err) } weights := []float32{ 0, -1, 0, -1, 5, -1, 0, -1, 0, } kernel := convolver.KernelWithRadius(1) kernel.SetWeightsUniform(weights) startTime := time.Now() result := kernel.ApplyAvg(img, runtime.NumCPU()) endTime := time.Now() log.Printf("Sharpen applied in %v", endTime.Sub(startTime)) _ = os.Mkdir("example-output", os.ModePerm) outFilePath := path.Join("example-output", "example-sharpen.png") outFile, err := os.Create(outFilePath) if err != nil { log.Panicf("Error creating output file: %v", err) } defer outFile.Close() err = png.Encode(outFile, result) if err != nil { log.Panicf("Error encoding output image: %v", err) } err = outFile.Close() if err != nil { log.Panicf("Error closing output file: %v", err) } log.Printf("Output written to %s", outFilePath) }
Output:
func KernelWithRadius ¶
func (*Kernel) SetWeightRGBA ¶
func (*Kernel) SetWeightUniform ¶ added in v0.4.0
func (*Kernel) SetWeightsRGBA ¶ added in v0.4.0
func (*Kernel) SetWeightsUniform ¶ added in v0.4.0
func (*Kernel) SideLength ¶
Click to show internal directories.
Click to hide internal directories.