adding more filters to images, to get better comparisons in datasets. Current filters: Edge Detection, Grayscale)
arnaucode idoctnef@openmailbox.org
Sat, 01 Jul 2017 18:47:37 +0200
5 files changed,
48 insertions(+),
22 deletions(-)
M
imageOperations.go
→
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 +}
M
knn.go
→
knn.go
@@ -2,6 +2,7 @@ package main
import ( "fmt" + "image" "sort" )@@ -82,22 +83,34 @@ }
} 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 + " - ")
M
main.go
→
main.go
@@ -8,22 +8,21 @@ "strconv"
"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))
M
readDataset.go
→
readDataset.go
@@ -42,9 +42,11 @@ histogram := imageToHistogram(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 @@ fmt.Println(folder.Name())
var imgDataset ImgDataset var imgDatasetED ImgDataset + var imgDatasetG ImgDataset folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name()) for _, file := range folderFiles {@@ -65,12 +68,20 @@ imageED := EdgeDetection(image)
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 }
M
server.go
→
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)