Browse Source

added filtered images analysis to neighbours

master
arnaucode 7 years ago
parent
commit
d0c35f8de8
4 changed files with 39 additions and 31 deletions
  1. +2
    -6
      imageOperations.go
  2. +28
    -19
      knn.go
  3. +2
    -1
      main.go
  4. +7
    -5
      readDataset.go

+ 2
- 6
imageOperations.go

@ -19,9 +19,7 @@ func dataToImage(data []byte, imageExtension string) (image.Image, error) {
switch imageExtension { switch imageExtension {
case "png": case "png":
img, err = png.Decode(reader) img, err = png.Decode(reader)
case "jpg":
img, err = jpeg.Decode(reader)
case "jpeg":
case "jpg", "jpeg":
img, err = jpeg.Decode(reader) img, err = jpeg.Decode(reader)
default: default:
img = nil img = nil
@ -38,9 +36,7 @@ func imageToData(img image.Image, imageExtension string) ([]byte, error) {
switch imageExtension { switch imageExtension {
case "png": case "png":
err = png.Encode(buf, img) err = png.Encode(buf, img)
case "jpg":
err = jpeg.Encode(buf, img, nil)
case "jpeg":
case "jpg", "jpeg":
err = jpeg.Encode(buf, img, nil) err = jpeg.Encode(buf, img, nil)
default: default:
img = nil img = nil

+ 28
- 19
knn.go

@ -5,6 +5,11 @@ import (
"sort" "sort"
) )
type Neighbour struct {
Dist float64
Label string
}
func euclideanDist(img1, img2 [][]float64) float64 { func euclideanDist(img1, img2 [][]float64) float64 {
var dist float64 var dist float64
for i := 0; i < len(img1); i++ { for i := 0; i < len(img1); i++ {
@ -16,11 +21,6 @@ func euclideanDist(img1, img2 [][]float64) float64 {
return dist return dist
} }
type Neighbour struct {
Dist float64
Label string
}
func isNeighbour(neighbours []Neighbour, dist float64, label string) []Neighbour { func isNeighbour(neighbours []Neighbour, dist float64, label string) []Neighbour {
var temp []Neighbour var temp []Neighbour
@ -71,30 +71,39 @@ func averageLabel(neighbours []Neighbour) string {
//send the most appeared neighbour in k //send the most appeared neighbour in k
return a[0].Label return a[0].Label
} }
func knn(dataset Dataset, input [][]float64) string {
k := 10
var neighbours []Neighbour
label := getMapKey(dataset)
for i := 0; i < k; i++ {
/*neighbours[i].Dist = euclideanDist(dataset["leopard"][0], input)
neighbours[i].Label = "leopard"*/
neighbours = append(neighbours, Neighbour{euclideanDist(dataset[label][0], input), label})
}
func distNeighboursFromDataset(dataset Dataset, neighbours []Neighbour, input [][]float64) []Neighbour {
//check the complete dataset, checking if each entry is a k nearest neighbour
for l, v := range dataset { for l, v := range dataset {
for i := 0; i < len(v); i++ { for i := 0; i < len(v); i++ {
dNew := euclideanDist(v[i], input) dNew := euclideanDist(v[i], input)
/*if dNew < d {
d = dNew
label = l
}*/
neighbours = isNeighbour(neighbours, dNew, l) neighbours = isNeighbour(neighbours, dNew, l)
} }
} }
return neighbours
}
func knn(dataset Dataset, input [][]float64) string {
k := 6
var neighbours []Neighbour
var neighboursED []Neighbour
//get a key from map dataset, the key is a label
label := getMapKey(dataset)
//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 = distNeighboursFromDataset(dataset, neighbours, input)
neighboursED = distNeighboursFromDataset(datasetED, neighbours, input)
neighbours = append(neighbours, neighboursED...)
for i := 0; i < len(neighbours); i++ { for i := 0; i < len(neighbours); i++ {
fmt.Print(neighbours[i].Label + " - ") fmt.Print(neighbours[i].Label + " - ")
fmt.Println(neighbours[i].Dist) fmt.Println(neighbours[i].Dist)
} }
//from the k nearest neighbours, get the more frequent neighbour
r := averageLabel(neighbours) r := averageLabel(neighbours)
return r return r
} }

+ 2
- 1
main.go

@ -10,13 +10,14 @@ import (
//dataset := make(Dataset) //dataset := make(Dataset)
var dataset Dataset var dataset Dataset
var datasetED 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 = readDataset("./dataset")
dataset, datasetED = 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(dataset)))

+ 7
- 5
readDataset.go

@ -42,33 +42,35 @@ func readImage(path string) image.Image {
return histogram return histogram
*/ */
} }
func readDataset(path string) map[string]ImgDataset {
func readDataset(path string) (Dataset, Dataset) {
dataset := make(Dataset) dataset := make(Dataset)
datasetED := make(Dataset)
folders, _ := ioutil.ReadDir(path) folders, _ := ioutil.ReadDir(path)
for _, folder := range folders { for _, folder := range folders {
fmt.Println(folder.Name()) fmt.Println(folder.Name())
var imgDataset ImgDataset var imgDataset ImgDataset
var imgDatasetED ImgDataset
folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name()) folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name())
for _, file := range folderFiles { for _, file := range folderFiles {
//get the image as original //get the image as original
image := readImage(path + "/" + folder.Name() + "/" + file.Name()) image := readImage(path + "/" + folder.Name() + "/" + file.Name())
histogram := imageToHistogram(image) histogram := imageToHistogram(image)
imgDataset = append(imgDataset, histogram)
//get the image with EdgeDetection filter //get the image with EdgeDetection filter
imageED := EdgeDetection(image) imageED := EdgeDetection(image)
histogramED := imageToHistogram(imageED) histogramED := imageToHistogram(imageED)
imgDataset = append(imgDataset, histogram)
imgDataset = append(imgDataset, histogramED)
imgDatasetED = append(imgDatasetED, histogramED)
} }
//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
} }
return dataset
return dataset, datasetED
} }

Loading…
Cancel
Save