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) (Dataset, Dataset) {
46	dataset := make(Dataset)
47	datasetED := make(Dataset)
48
49	folders, _ := ioutil.ReadDir(path)
50	for _, folder := range folders {
51		fmt.Println(folder.Name())
52
53		var imgDataset ImgDataset
54		var imgDatasetED ImgDataset
55
56		folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name())
57		for _, file := range folderFiles {
58			//get the image as original
59			image := readImage(path + "/" + folder.Name() + "/" + file.Name())
60			histogram := imageToHistogram(image)
61			imgDataset = append(imgDataset, histogram)
62
63			//get the image with EdgeDetection filter
64			imageED := EdgeDetection(image)
65			histogramED := imageToHistogram(imageED)
66			imgDatasetED = append(imgDatasetED, histogramED)
67
68		}
69
70		//add the foldername to the Dataset map
71		dataset[folder.Name()] = imgDataset
72		datasetED[folder.Name()] = imgDatasetED
73	}
74
75	return dataset, datasetED
76}