integral

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2022 License: GPL-3.0 Imports: 3 Imported by: 3

README

# rescribe.xyz/integral package

This package contains methods and structures for dealing with
Integral Images, aka Summed Area Tables. These are structures
which precompute the sum of pixels to the left and above each
pixel, which can make several common image processing
operations much faster.

This is a Go package, and can be installed in the standard go way,
by running `go get rescribe.xyz/integral` and documentation
can be read with the `go doc` command or online at
<https://pkg.go.dev/rescribe.xyz/integral>.

## Contributions

Any and all comments, bug reports, patches or pull requests would
be very welcomely received. Please email them to <nick@rescribe.xyz>.

## License

This package is licensed under the GPLv3. See the LICENSE file for
more details.

Documentation

Overview

integral is a package for processing integral images, aka summed area tables. These are structures which precompute the sum of pixels to the left and above each pixel, which can make several common image processing operations much faster.

A lot of image processing operations rely on many calculations of the sum or mean of a set of pixels. As these have been precalculated for an integral image, these calculations are much faster. Image.Sum() and Image.Mean() functions are provided by this package to take advantage of this.

Another common requirement is standard deviation over an area of an image. This can be calculated by creating an integral image and squared integral image (SqImage) for a base image, and passing them to the MeanStdDev() function provided.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MeanStdDev

func MeanStdDev(i Image, sq SqImage, r image.Rectangle) (float64, float64)

MeanStdDev calculates the mean and standard deviation of a section of an image, using the corresponding regular and square integral images.

Example
package main

import (
	"fmt"
	"image"
	"image/draw"
	"log"
	"os"

	_ "image/png"
	"rescribe.xyz/integral"
)

func main() {
	f, err := os.Open("testdata/in.png")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	img, _, err := image.Decode(f)
	if err != nil {
		log.Fatal(err)
	}
	b := img.Bounds()
	in := integral.NewImage(b)
	sq := integral.NewSqImage(b)
	draw.Draw(in, b, img, b.Min, draw.Src)
	draw.Draw(sq, b, img, b.Min, draw.Src)
	mean, stddev := integral.MeanStdDev(*in, *sq, b)
	fmt.Printf("Mean: %f, Standard Deviation: %f\n", mean, stddev)
}
Output:

Mean: 54677.229042, Standard Deviation: 21643.721672

Types

type Image

type Image [][]uint64

Image is an integral image

func NewImage

func NewImage(r image.Rectangle) *Image

NewImage returns a new integral image with the given bounds.

func (Image) At

func (i Image) At(x, y int) color.Color

func (Image) Bounds

func (i Image) Bounds() image.Rectangle

func (Image) ColorModel

func (i Image) ColorModel() color.Model

func (Image) Mean

func (i Image) Mean(r image.Rectangle) float64

Mean returns the average value of pixels in a section of an image

Example
package main

import (
	"fmt"
	"image"
	"image/draw"
	"log"
	"os"

	_ "image/png"
	"rescribe.xyz/integral"
)

func main() {
	f, err := os.Open("testdata/in.png")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	img, _, err := image.Decode(f)
	if err != nil {
		log.Fatal(err)
	}
	b := img.Bounds()
	in := integral.NewImage(b)
	draw.Draw(in, b, img, b.Min, draw.Src)
	fmt.Printf("Mean: %f\n", in.Mean(b))
}
Output:

Mean: 54677.229042

func (Image) Set

func (i Image) Set(x, y int, c color.Color)

func (Image) Sum

func (i Image) Sum(r image.Rectangle) uint64

Sum returns the sum of all pixels in a section of an image

Example
package main

import (
	"fmt"
	"image"
	"image/draw"
	"log"
	"os"

	_ "image/png"
	"rescribe.xyz/integral"
)

func main() {
	f, err := os.Open("testdata/in.png")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	img, _, err := image.Decode(f)
	if err != nil {
		log.Fatal(err)
	}
	b := img.Bounds()
	in := integral.NewImage(b)
	draw.Draw(in, b, img, b.Min, draw.Src)
	fmt.Printf("Sum: %d\n", in.Sum(b))
}
Output:

Sum: 601340165

type SqImage

type SqImage [][]uint64

SqImage is a Square integral image. A squared integral image is an integral image for which the square of each pixel is saved; this is useful for efficiently calculating Standard Deviation.

func NewSqImage

func NewSqImage(r image.Rectangle) *SqImage

NewSqImage returns a new squared integral image with the given bounds.

func (SqImage) At

func (i SqImage) At(x, y int) color.Color

func (SqImage) Bounds

func (i SqImage) Bounds() image.Rectangle

func (SqImage) ColorModel

func (i SqImage) ColorModel() color.Model

func (SqImage) Mean

func (i SqImage) Mean(r image.Rectangle) float64

Mean returns the average value of pixels in a section of an image

func (SqImage) Set

func (i SqImage) Set(x, y int, c color.Color)

func (SqImage) Sum

func (i SqImage) Sum(r image.Rectangle) uint64

Sum returns the sum of all pixels in a section of an image

Jump to

Keyboard shortcuts

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