You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.6 KiB

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