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