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