Documentation ¶
Overview ¶
Example ¶
package main import ( "bytes" "fmt" "log" "github.com/johandry/finder2d" ) func main() { var on, off byte on = []byte(`+`)[0] off = []byte(` `)[0] percentage := 80.0 delta := 1 // Create the Finder2D finder := finder2d.New(on, off, percentage, delta) // Load the frame or source matrix frameReader := bytes.NewBufferString(frame) if err := finder.LoadSource(frameReader); err != nil { log.Fatalf("[ERROR] fail to load the source matrix. %s", err) } // Load the image or target matrix imageReader := bytes.NewBufferString(target) if err := finder.LoadTarget(imageReader); err != nil { log.Fatalf("[ERROR] fail to load the target matrix. %s", err) } // Search the target in the source if err := finder.SearchSimple(); err != nil { log.Fatalf("[ERROR] failed to search the target matrix. %s", err) } // Print the matches in JSON fmt.Println(finder) } const frame = `++++++++++++++ +++++ +++++ +++++++++++ +++ ++++++++++++++++++++++ +++++++ ++ ++ ++++++ +++++++++++++++++++++++++++++++++++++++++ +++++ ++++++++++++ +++ + ++++ ++++++++ ++ +++++++ ++++++++ ++++++++++++++++++++++++++++ +++++++ +++ ++++++++ ++ +++++++++++++ ++ ++ ++++++++++++++ + + +++++ +++++ ++++++ ++ ++++ +++++++++++++ ++++ +++ ++++ ++ +++ ++ + ++ ++ + +++++++++ ++++++++++++++++++ ++++ ++ + +++++ ++ +++ +++++++ ++++ ++++ + + +++++++ ++++ +++++++++ ++++++++++++++ +++ +++++++++++++ + + +++++++++++++++++++++++++ +++ +++ +++++++ +++++++++++++ +++++++++++++++++++++ ++++++ ++ +++ +++ +++++++++++ + ++++++++++ +++ +++ ++ +++++++++++++ +++++++++++ ++++++ +++++++++++ ++ +++++++++++++ ++++ ++ +++ ++ ++++ +++++ + ++++ ++++ ++++++++++++++ ++++++++++ ++ +++ ++ +++++++++ ++++++ + + ++ ++ +++ + ++++++++++++++++ ++++++++ ++++++++++++++++++ ++ ++ ++++++ ++++++++++++ ++ + + ++ +++++ ++++++ ++++ +++++++ +++++++++++++++ ++++++ + ++ + + ++ + ++++++++ ++++++++ ++ +++ ++ ++ + ++++++++++++++++++ + +++ ++++++++ +++ +++++ ++ +++ ++ +++++++++ ++++++++++ ++ ++ ++++ +++++ ++++++++++ + +++++++++++ ++++++++ ++++ ++ ++ +++++++++++++++ ++ +++++ ++ ++ ++ + +++++ ++ ++ +++++++++ + ++++++++++++++++ + +++++ ++++++++++ +++++++++ + ++++ ++++++++++ +++++ +++++++++++++++++ +++ ++++ + + ++++ +++++++++++ +++++ ++++++++++++ ++++++++++++++ ++++++++ ++++ ++ +++++++ ++++++++++++ ++ ++ ++++++ + +++ +++++++++++ +++++++++++++ ++++ ++++++++++++++++++++++++++++ +++++++++++++++++++++++ ++++++++ + ++ + ++++++++++ ++++ +++++ +++++++ + +++++++++++++++ +++++++++++++++++++++++ +++++++ +++ +++++++++++++++ +++++ ++++ ++ + +++++++ +++++++++++++++ ++ ++++++++++++++++++++++ +++++ +++++ ++++++++++++++ +++++++ ++++ ++ +++++++++++++++++++++++ +++++ ++++++++++++++++++ ++++++++ +++ ++++++++++++ +++++++++++++ ++++++++ ++ ++++++++++ ++++ ++ +++ ++++++++++++++++++++++++ +++ +++ +++ ++++++ ++ ++ ++ +++++++++ ++++++++ +++++++++++ + +++ +++++++++++++++++++++++ ++++ + +++ +++++++++++++++++++++++++ +++++++++ + +++++++ +++++++++++++++++ + +++ ++++++++ ++++++++++ ++++ + +++++++++++++++ ++++++++ ++++++++++ + +++++++++++++++ ++++++++ ++++++++++++++++++++ ++++ + +++++++ +++ ++ ++++++++++++++++++++++++ ++++ + ++ +++++++++ +++++++++++ ++++++++++ +++++++++++++ +++++ +++++++++++++++++++++++++++++++ ++++++++++ +++++++++ +++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++ +++++++++ ++++++++ +++++ ++ +++++++++++++ +++++++++++++++++++++++++++ ++++++ +++++++++++ ++++++ ++ ++++++++++++++++ ++ ++++++++ +++++++++++++++++++ ++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++ ++ +++ +++++++++++++++ +++++ ++++ +++++ +++++++++ ++++++++++ +++++++++ ++++++++++ + +++++++++++++++++++ +++ ++ +++ ++++++++++++ +++++++++++++ +++++++++++++++++++++++ +++++++++++++ +++ ++ ++++ + +++++ +++ ++++ +++++++ ++ + + +++++++++++++++ + ++++ ++++ +++++++++ +++++++++++++++ +++++++++++++++++ +++++ ++ ++ +++++++ +++++++++++++++++++++ +++ + + + +++++++ +++++++++++++++ +++++++ + +++++++++++++++++++++++++ ++++ ++++++++ +++++++++ +++++++ +++++++++++++++++++++++ ++ +++ ++ ++ + +++++ +++++++++ ++++++++++ ++++++ ++++++ ++ ++++++++++ +++++++++++++++ +++++++++++++ + + + +++++++ + +++++++++++++++++++++++++ +++++++ ++++++ +++++ +++++++ +++++ + ++++ + ++++++++ +++++++++++++++++ +++ +++++ +++++ ++++ +++++ ++++++++ +++++++++++++++++++ ++++ +++ ++++++++++ + ++++++++++ +++++ +++++++ + ++ ++++ ++++++++++++ + ++++++++++++++++ ++++++++++++++ +++++++++++ +++++++++++++++ +++++++++++++ ++++++++++++++++++++++++++ +++ ++++++++ + ++++++++ +++++++ +++++++++++++++++++ +++++++ + ++++ ++++ +++++++++ ++++++++++++ +++ ++++++++++++ + +++++++++++++++++++++++++++++++ ++ ++ ++++ +++++++++++++ ++++++ ++++++++++ ++++++++++ ++ +++ +++++ ++++++++++++++++++++ +++++++ +++++++++ +++++++ +++ ++++++++++++++++++ + ++++++++ ++++ +++++ +++++++++++ +++++++++++++++++++++++++ + ++++++++++++++++ + +++ +++++++ +++++++++++ +++++++ ++++++++++++ ++ ++++++++++++++++ ++++ +++++++++++++++++++++++++++ ++++++ ++++++ + + +++ ++ ++ +++ ++++++++++++++++++++++++++++ +++++++++++++ ++++++++++ ++++++++ +++++++++ +++ +++ +++ +++++++ +++++++++++ ++++++++++++++ ++ +++ ++ ++ +++++ +++++++++++ ++++ ++++ ++++++++++++ ++++++++ +++ ++++++ +++ +++++ ++++++ ++++++ + + ++++ + ++++++++ ++ +++++++++++++++++++++ ++++ +++++ + + ++++++++++++++ +++++++++++++++ ++ +++ +++ + +++++ ++ + +++ ++ ++++++ +++++ ++++ +++++ + ++++ ++++ +++ +++++ ++++++++ ++++++++++++++ ++++++++++++++++++++++++++++++++++++ + ++++ + ++ ++ ++++ + ++++ ++ +++++++++++++++++++++ ++ +++ ++ + +++++++++ + +++++++ ++++++++++++ +++++++ +++++++++++++++ +++++++++++++++++ + ++++++ ++ + + ++ ++++++++++++++++++++++++++++++++++++++ ++++ +++++++++++ ++ ++++++++++++++++++++ ++++++ ++ + ++ +++++ ++++++++ ++++++ ++ ++ + ++++++++++++++++ ++++++ +++++++++++++ ++ ++ ++ ++ +++ +++++++++++ ++++++++++++++ + ++++ ++++++++++++ ++++ ++ ++++++++ +++++++ ++++ ++ +++ ++ +++++++++++++++++++++ + ++++++++++ ++ + +++++ ++++++++ ++++++++++++++++++++++++++ +++++ ++++++++ ++++++++++++++++++ ++ + ++ + ++ +++ ++++++++++++++++++++++ ++++ ++++++ + + ++++++++ ++++++ ++++ ++++ + + +++ ++ ++++++++++++ ++++++++++++++++++ ++ ++ +++ +++++++++++++++ + +++++++ +++ +++ +++ +++++++++++++ ++++++++++++++++++++++ +++++++++++ +++++ +++++++++++++++++++ ++++++ ++ + +++ + +++ +++ +++++++++ +++++ ++++++ +++++++++++ ++ ++++ +++ +++ +++++++++++ ++++++++++ ++ + + ++ + ++++++++++++ ++ ++ +++ + + + + + +++++++ ++++++++ +++ ++++++ +++++++++ ++++ +++++ ++ ++ ++++++++ + +++++ +++ ++++ +++++ ++++++++++++ + +++++++++++ ++ +++++++++++++++ +++ ++ + + ++ + +++ ++ +++++++++++ +++++++++++ +++++++++++++++++++++ +++++ +++++++++++++++++++++++ ++ +++ +++ + ++++ +++ +++++++++++++++++++++++++++ +++++++++++ ++++++++++++ ++++++++++ + + ++++ + ++ ++ + + ++++++++++++++++++++++ +++++ +++++++ +++++++++++++ +++++++++++++++++++++++++++ + +++++ + +++++++ +++++++++++++++++++++ ++++++++++++++ +++ ++++++++++++++++++++++ + ++++++++++ + +++ + ++++ + ++ + +++++ +++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++ + ++++++ ++++++++++++++++++++++++++ ++ + + ++++++++++++++++++++++++++++ +++++++++++ + ++++++++++++++ ++++++++++ +++++++++++++++++++++++++++++++ +++ +++ + +++++++ +++++++++++ +++ + +++++++++++ ++++++ ++++++++++++++ +++++++ ++ + +++++ +++++++++++++ ++++ +++ +++++++ ++ ++++++++ +++ +++++++ +++++++++ ++ ++++++++ +++++++++ +++++++++ +++++ +++++++++ ++++ + + +++++++++ ++ +++++++++++ +++ + ++++++++ + +++++++++++++ +++ ++++++++++++++++ ++++++++++ +++++++++++++++ +++++++++++ +++ +++++ +++++++ ++++++++ ++++++++++ +++++++++++++++++++++++++ ++++++ ++++++++++++++++ +++ ++ ++ +++++++++++++++++++++++++++++ + +++ + ++++++++++++ ++++++ ++ ++++++++ +++++++++++++++ +++++++++++++++++ +++++ +++++++++++++++++++ ++++ ++ ++ ++++++ +++ ++++++++++++++ + +++++++++++++++++++++++++++ + +++++ ++++++++++++++++++++ +++ + ++++ ++++ ++++++++++++++++ +++++++++++ + + ++ +++++ + ++ +++ + +++++++++ +++++++++ ++++++++ ++++ ++++++ ++++++++++ ++ ++ ++++++++ ++++++++ +++++ + ++++++ ++++++ ++ + + +++++++ ++++ +++ +++ ++++ ++++++ +++++++++++++++++++++ +++ ++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++ ++ +++ ++++++++++++++ ++++++ + ++++++++++++ +++++ +++++++ + + +++++++ ++++++++++++++ ++ ++++++++++++++++++ +++++++++++++++ ++++++ + ++ + ++++ + +++ +++++ ++++++ +++ ++++++++++++++++++ +++++++++++++++ +++++ ++ ++++++++++ +++ +++++++++ +++++++++++++++++++++++++++++++ +++++++++ +++++++++++++++ +++++++++++++ ++++ ++++++++ ++ +++++++++ +++++++++++++ +++++ +++++++++ ++ ++++++++++++++++++ +++ ++++++++ + ++++ +++ ++ ++++++++++++++++ +++++++++++++++++++ +++++++++++++++++++++ +++ +++ ++++++++ ++++++++++ ++++++ + + ++++++++++++++++++++++++++++ ++++ ++++++++++ ++++ ++ +++++ +++++++++++++++++++++ +++ +++ +++++++++++++ ++ ++++++++++++++++++ + +++ +++ +++++ ++++++++++++ +++++++ + ++++++ ++ ++++++++ ++++ ++++++++++ ++++++++++++++++++++++ +++++++++++ + ++++++++++++++ +++++++ ++++++ +++++++ ++++++++++++++ +++++ ++++++++++++++++++ +++++++ ++ ++ ++++++++++++++++ +++ +++++++++++++++ ++++ ++++++ ++++ ++++++++++++++++++++++++++++++++ + + ++++ +++++ ++ +++++++ ++ + ++++++++++++++++++++++++++++++++++++++++++++ +++ +++ +++ ++++++++++++ ++++++++++ +++ ++++ + ++++++++ +++++++++++ ++++++++++ + +++++ ++ ++ ++++ +++++++++++++++++++ +++++++++++++ + ++++++++ ++++++++++++++++++++ +++++++++ ++++++ ++ +++ ++ +++++ + +++++++++++++ ++ ++ + ++++++++++++++++++++++ +++++++++++++ ++++++ +++ ++ ++ + ++++++++ +++++++++++ + + + +++ + +++++++++++++ ++++++++++++++++++++++++++++++ ++ + ++ +++++++ +++++++ + ++++ +++ +++ +++ ++++ ++++++++ ++ +++++++++++++++ +++++++++++++ ++ +++ ++ ++++++++++++++++++++++++ + +++ ++++++ +++++++++++++++++++ ++ ++ +++ + ++ + ++ ++ ++++++++++ ++++++ +++ ++ +++ ++ + +++++++++++++++++++ ++++++++++ ++++++++++++++++ +++++ + ++++++++++++ +++++++ ++ ++ + ++++++++++++++++++ + ++ +++++++++++++ ++++++++ + ++++++++++++++ + +++ ++ + + ++ + ++++++ ++++++++++++++++++++++++++ + ++ ++++++ + + ++ +++++++++++++ + +++ ++ +++ ++ + ++++++ +++++++++++++++ +++++++ ++++++++ + ++++++++++++++++++++++++++++++++++++++++++ ++ ++ + ++++++++++++++ +++++ ++ +++++++++ ++++ ++++++++++++++++++++++++++ +++++++++++++++++ + +++ + +++++++++++++++++ ++++ + ++++++++++ +++++++++++++++++++++++ +++++++++++++++ +++++ + ++ +++++++++++++ ++++ +++ ++++ +++++++++ +++++++++++++++++++++++ ++++++++ ++++++++ + ++++++++++++++++++++++++++ +++++++ ++++++++++++++++++++++ ++++++++ +++++++++++++++++++++++++++++++++ ` const target = `+ + +++ +++ +++++++++++++ ++ ++ ++ + + ++ ++ +++ +++ ++ ++ ++ ++ +++ ++ ++ ++ ++ + + ++ ++ +++ ++ ++ ++ +++++ `
Output: [{"X":80,"Y":0,"Percentage":99.11111111111111},{"X":45,"Y":1,"Percentage":97.77777777777777},{"X":42,"Y":40,"Percentage":95.55555555555556},{"X":84,"Y":49,"Percentage":91.55555555555556},{"X":47,"Y":80,"Percentage":92.44444444444444},{"X":84,"Y":84,"Percentage":91.55555555555556}]
Index ¶
- Constants
- Variables
- type Finder2D
- func (f *Finder2D) IsAMatchPoint(x, y int) bool
- func (f *Finder2D) IsInMatchArea(x, y int) bool
- func (f *Finder2D) LoadSource(r io.Reader) error
- func (f *Finder2D) LoadTarget(r io.Reader) error
- func (f *Finder2D) Matrix() string
- func (f *Finder2D) SearchSimple() error
- func (f *Finder2D) String() string
- func (f *Finder2D) Stringf(format string) string
- func (f *Finder2D) Values() (byte, byte)
- type Match
- type Matrix
Examples ¶
Constants ¶
const DefaultMinMatchPercentage float64 = 50.0
DefaultMinMatchPercentage default minimun match percentage. Any match percentage lower than this number is not considered
const MinDelta = 1
MinDelta minimum difference between coordinates of group of matches to be considered the same image
Variables ¶
var ( DefaultOne = []byte(`+`)[0] DefaultZero = []byte(` `)[0] )
Default values for a one and a zero in a matrix
Functions ¶
This section is empty.
Types ¶
type Finder2D ¶
type Finder2D struct { Target *Matrix Source *Matrix Matches []Match Percentage float64 Delta int // contains filtered or unexported fields }
Finder2D is the struct used to find a 2D pattern into a 2D source matrix
func (*Finder2D) IsAMatchPoint ¶
IsAMatchPoint returns true if the coordinate is a match coordinate
func (*Finder2D) IsInMatchArea ¶
IsInMatchArea return true if the coordinate is in the area of the match
func (*Finder2D) LoadSource ¶
LoadSource loads the source from a reader replacing the cell value given in `one` for `1` and `zero` for `0`
func (*Finder2D) LoadTarget ¶
LoadTarget loads the target from a reader replacing the cell value given in `one` for `1` and `zero` for `0`
func (*Finder2D) SearchSimple ¶
SearchSimple find the occurences of the target in the source and the percentage match in the simplest way which is to iterate thru the entire matrix searching for the pattern, storing the match when the match percentage is higher than the required
type Match ¶
Match represents the coordinate the target matrix was found in the source matrix and the percentage match
type Matrix ¶
type Matrix struct { Content [][]int // contains filtered or unexported fields }
Matrix represents a 2D array
func LoadMatrix ¶
LoadMatrix create a matrix from a reader
func (*Matrix) Load ¶
Load loads a matrix from a reader replacing the cell value given in `one` for `1` and `zero` for `0`
func (*Matrix) Sample ¶
Sample gets a sample of the Matrix from the coordinates (x,y) and the given width and height