Browse Source

adding more filters to images, to get better comparisons in datasets. Current filters: Edge Detection, Grayscale)

master
arnaucode 7 years ago
parent
commit
c3e167726b
5 changed files with 48 additions and 22 deletions
  1. +8
    -0
      imageOperations.go
  2. +20
    -7
      knn.go
  3. +5
    -6
      main.go
  4. +13
    -2
      readDataset.go
  5. +2
    -7
      server.go

+ 8
- 0
imageOperations.go

@ -78,3 +78,11 @@ func EdgeDetection(img image.Image) image.Image {
r := effect.EdgeDetection(img, 1.0)
return r
}
func Grayscale(img image.Image) image.Image {
r := effect.Grayscale(img)
return r
}
func Emboss(img image.Image) image.Image {
r := effect.Emboss(img)
return r
}

+ 20
- 7
knn.go

@ -2,6 +2,7 @@ package main
import (
"fmt"
"image"
"sort"
)
@ -82,22 +83,34 @@ func distNeighboursFromDataset(dataset Dataset, neighbours []Neighbour, input []
}
return neighbours
}
func knn(dataset Dataset, input [][]float64) string {
func knn(datasets []Dataset, imgInput image.Image) string {
k := 6
var neighbours []Neighbour
var neighboursED []Neighbour
var neighboursG []Neighbour
imgED := EdgeDetection(imgInput)
imgG := Grayscale(imgInput)
histogram := imageToHistogram(imgInput)
histogramED := imageToHistogram(imgED)
histogramG := imageToHistogram(imgG)
//get a key from map dataset, the key is a label
label := getMapKey(dataset)
label := getMapKey(datasets[0])
//fill the first k neighbours
for i := 0; i < k; i++ {
neighbours = append(neighbours, Neighbour{euclideanDist(dataset[label][0], input), label})
neighboursED = append(neighbours, Neighbour{euclideanDist(dataset[label][0], input), label})
neighbours = append(neighbours, Neighbour{euclideanDist(datasets[0][label][0], histogram), label})
neighboursED = append(neighboursED, Neighbour{euclideanDist(datasets[1][label][0], histogramED), label})
neighboursG = append(neighboursG, Neighbour{euclideanDist(datasets[2][label][0], histogramG), label})
}
neighbours = distNeighboursFromDataset(dataset, neighbours, input)
neighboursED = distNeighboursFromDataset(datasetED, neighbours, input)
neighbours = append(neighbours, neighboursED...)
neighbours = distNeighboursFromDataset(datasets[0], neighbours, histogram)
neighboursED = distNeighboursFromDataset(datasets[1], neighboursED, histogramED)
neighboursG = distNeighboursFromDataset(datasets[2], neighboursG, histogramG)
//neighbours = append(neighbours, neighboursED...)
neighbours = append(neighbours, neighboursG...)
for i := 0; i < len(neighbours); i++ {
fmt.Print(neighbours[i].Label + " - ")

+ 5
- 6
main.go

@ -8,22 +8,21 @@ import (
"time"
)
//dataset := make(Dataset)
var dataset Dataset
var datasetED Dataset
//array of datasets
var datasets []Dataset
func main() {
readConfig("./config.json")
c.Cyan("reading images datasets")
tStart := time.Now()
dataset, datasetED = readDataset("./dataset")
datasets = readDataset("./dataset")
fmt.Print("time spend reading images: ")
fmt.Println(time.Since(tStart))
fmt.Println("total folders scanned: " + strconv.Itoa(len(dataset)))
fmt.Println("total folders scanned: " + strconv.Itoa(len(datasets[0])))
numImages := 0
for _, v := range dataset {
for _, v := range datasets[0] {
numImages = numImages + len(v)
}
c.Cyan("total images in dataset: " + strconv.Itoa(numImages))

+ 13
- 2
readDataset.go

@ -42,9 +42,11 @@ func readImage(path string) image.Image {
return histogram
*/
}
func readDataset(path string) (Dataset, Dataset) {
func readDataset(path string) []Dataset {
var resultDatasets []Dataset
dataset := make(Dataset)
datasetED := make(Dataset)
datasetG := make(Dataset)
folders, _ := ioutil.ReadDir(path)
for _, folder := range folders {
@ -52,6 +54,7 @@ func readDataset(path string) (Dataset, Dataset) {
var imgDataset ImgDataset
var imgDatasetED ImgDataset
var imgDatasetG ImgDataset
folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name())
for _, file := range folderFiles {
@ -65,12 +68,20 @@ func readDataset(path string) (Dataset, Dataset) {
histogramED := imageToHistogram(imageED)
imgDatasetED = append(imgDatasetED, histogramED)
//get the image with Grayscale filter
imageG := Grayscale(image)
histogramG := imageToHistogram(imageG)
imgDatasetG = append(imgDatasetG, histogramG)
}
//add the foldername to the Dataset map
dataset[folder.Name()] = imgDataset
datasetED[folder.Name()] = imgDatasetED
datasetG[folder.Name()] = imgDatasetG
}
return dataset, datasetED
resultDatasets = append(resultDatasets, dataset)
resultDatasets = append(resultDatasets, datasetED)
resultDatasets = append(resultDatasets, datasetG)
return resultDatasets
}

+ 2
- 7
server.go

@ -74,14 +74,9 @@ func NewImage(w http.ResponseWriter, r *http.Request) {
_, handler, err := r.FormFile("file")
check(err)
//imageName := strings.Split(handler.Filename, ".")[0]
//fileName := imageName + ".png"
//data, err := ioutil.ReadAll(file)
//check(err)
img := readImage(handler.Filename)
histogram := imageToHistogram(img)
result := knn(dataset, histogram)
//histogram := imageToHistogram(img)
result := knn(datasets, img)
c.Purple("seems to be a " + result)
fmt.Fprintln(w, "seems to be a "+result)

Loading…
Cancel
Save