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) r := effect.EdgeDetection(img, 1.0)
return r 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 ( import (
"fmt" "fmt"
"image"
"sort" "sort"
) )
@ -82,22 +83,34 @@ func distNeighboursFromDataset(dataset Dataset, neighbours []Neighbour, input []
} }
return neighbours return neighbours
} }
func knn(dataset Dataset, input [][]float64) string {
func knn(datasets []Dataset, imgInput image.Image) string {
k := 6 k := 6
var neighbours []Neighbour var neighbours []Neighbour
var neighboursED []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 //get a key from map dataset, the key is a label
label := getMapKey(dataset)
label := getMapKey(datasets[0])
//fill the first k neighbours //fill the first k neighbours
for i := 0; i < k; i++ { 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++ { for i := 0; i < len(neighbours); i++ {
fmt.Print(neighbours[i].Label + " - ") fmt.Print(neighbours[i].Label + " - ")

+ 5
- 6
main.go

@ -8,22 +8,21 @@ import (
"time" "time"
) )
//dataset := make(Dataset)
var dataset Dataset
var datasetED Dataset
//array of datasets
var datasets []Dataset
func main() { func main() {
readConfig("./config.json") readConfig("./config.json")
c.Cyan("reading images datasets") c.Cyan("reading images datasets")
tStart := time.Now() tStart := time.Now()
dataset, datasetED = readDataset("./dataset")
datasets = readDataset("./dataset")
fmt.Print("time spend reading images: ") fmt.Print("time spend reading images: ")
fmt.Println(time.Since(tStart)) 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 numImages := 0
for _, v := range dataset {
for _, v := range datasets[0] {
numImages = numImages + len(v) numImages = numImages + len(v)
} }
c.Cyan("total images in dataset: " + strconv.Itoa(numImages)) 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 return histogram
*/ */
} }
func readDataset(path string) (Dataset, Dataset) {
func readDataset(path string) []Dataset {
var resultDatasets []Dataset
dataset := make(Dataset) dataset := make(Dataset)
datasetED := make(Dataset) datasetED := make(Dataset)
datasetG := make(Dataset)
folders, _ := ioutil.ReadDir(path) folders, _ := ioutil.ReadDir(path)
for _, folder := range folders { for _, folder := range folders {
@ -52,6 +54,7 @@ func readDataset(path string) (Dataset, Dataset) {
var imgDataset ImgDataset var imgDataset ImgDataset
var imgDatasetED ImgDataset var imgDatasetED ImgDataset
var imgDatasetG ImgDataset
folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name()) folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name())
for _, file := range folderFiles { for _, file := range folderFiles {
@ -65,12 +68,20 @@ func readDataset(path string) (Dataset, Dataset) {
histogramED := imageToHistogram(imageED) histogramED := imageToHistogram(imageED)
imgDatasetED = append(imgDatasetED, histogramED) 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 //add the foldername to the Dataset map
dataset[folder.Name()] = imgDataset dataset[folder.Name()] = imgDataset
datasetED[folder.Name()] = imgDatasetED 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") _, handler, err := r.FormFile("file")
check(err) check(err)
//imageName := strings.Split(handler.Filename, ".")[0]
//fileName := imageName + ".png"
//data, err := ioutil.ReadAll(file)
//check(err)
img := readImage(handler.Filename) 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) c.Purple("seems to be a " + result)
fmt.Fprintln(w, "seems to be a "+result) fmt.Fprintln(w, "seems to be a "+result)

Loading…
Cancel
Save