arnaucube
arnaucube/numberImgRecognitionPublic Self-hosted repository
numberImgRecognition/goVersion/readExamples.go
goVersion/readExamples.goRaw
 1package main
 2
 3import (
 4	"image"
 5	_ "image/png"
 6	"io/ioutil"
 7	"os"
 8	"strings"
 9)
10
11type imgRGBA [][]int
12type CollectionImgs []imgRGBA
13type ExamplesImg map[string]CollectionImgs
14
15func readImage(path string) imgRGBA {
16	//open image, and generate the histogram, made from r,g,b,a components
17	reader, err := os.Open(path)
18	check(err)
19	defer reader.Close()
20
21	m, _, err := image.Decode(reader)
22	check(err)
23	bounds := m.Bounds()
24
25	//generate the histogram
26	var histogram imgRGBA
27	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
28		for x := bounds.Min.X; x < bounds.Max.X; x++ {
29			r, g, b, a := m.At(x, y).RGBA()
30			var pixel []int
31			pixel = append(pixel, int(r), int(g), int(b), int(a))
32			histogram = append(histogram, pixel)
33		}
34	}
35	return histogram
36}
37func readExamples() map[string][]imgRGBA {
38	//var examplesImg ExamplesImg
39	examplesImg := make(map[string][]imgRGBA)
40	//for each image in the directory of example images, read the image and generate the histogram
41	files, _ := ioutil.ReadDir("./images/numbers/")
42	for _, f := range files {
43		//fmt.Println("images/numbers/" + f.Name())
44		file := readImage("images/numbers/" + f.Name())
45		numberName := strings.Split(f.Name(), ".")[0]
46		//fmt.Println(numberName)
47		examplesImg[numberName] = append(examplesImg[numberName], file)
48	}
49	return examplesImg
50
51}