arnaucube
arnaucube/galdricPublic Self-hosted repository
galdric/readDataset.go
readDataset.goRaw
 1package main
 2
 3import (
 4	"fmt"
 5	"image"
 6	"io/ioutil"
 7	"strconv"
 8	"strings"
 9)
10
11//each image is [][]float64, is a array of pixels
12type ImgDataset [][][]float64
13
14type Dataset map[string]ImgDataset
15
16func byteArrayToFloat64Array(b []byte) []float64 {
17	var f []float64
18	for i := 0; i < len(b); i++ {
19		val, _ := strconv.ParseFloat(string(b[i]), 64)
20		f = append(f, val)
21	}
22	return f
23}
24
25func readImage(path string) image.Image {
26	//open image file
27	dat, err := ioutil.ReadFile(path)
28	check(err)
29
30	pathSplited := strings.Split(path, ".")
31	imageExtension := pathSplited[len(pathSplited)-1]
32	imageRaw, err := dataToImage(dat, imageExtension)
33	check(err)
34
35	//resize the image to standard size
36	image := Resize(imageRaw)
37
38	return image
39	/*
40		//convert the image to histogram(RGBA)
41		histogram := imageToHistogram(image)
42		return histogram
43	*/
44}
45func readDataset(path string) map[string]ImgDataset {
46	dataset := make(Dataset)
47
48	folders, _ := ioutil.ReadDir(path)
49	for _, folder := range folders {
50		fmt.Println(folder.Name())
51
52		var imgDataset ImgDataset
53
54		folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name())
55		for _, file := range folderFiles {
56			//get the image as original
57			image := readImage(path + "/" + folder.Name() + "/" + file.Name())
58			histogram := imageToHistogram(image)
59
60			//get the image with EdgeDetection filter
61			imageED := EdgeDetection(image)
62			histogramED := imageToHistogram(imageED)
63
64			imgDataset = append(imgDataset, histogram)
65			imgDataset = append(imgDataset, histogramED)
66
67		}
68
69		//add the foldername to the Dataset map
70		dataset[folder.Name()] = imgDataset
71	}
72
73	return dataset
74}