2017-07-28 10:18:24 -07:00
|
|
|
// Copyright 2017 The goimagehash Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package etcs
|
|
|
|
|
2017-07-31 07:24:04 -07:00
|
|
|
// MeanOfPixels function returns a mean of pixels.
|
2017-07-28 10:18:24 -07:00
|
|
|
func MeanOfPixels(pixels []float64) float64 {
|
|
|
|
m := 0.0
|
|
|
|
lens := len(pixels)
|
|
|
|
if lens == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range pixels {
|
|
|
|
m += p
|
|
|
|
}
|
|
|
|
|
|
|
|
return m / float64(lens)
|
|
|
|
}
|
|
|
|
|
2017-07-31 07:24:04 -07:00
|
|
|
// MedianOfPixels function returns a median value of pixels.
|
|
|
|
// It uses quick selection algorithm.
|
2017-07-28 10:18:24 -07:00
|
|
|
func MedianOfPixels(pixels []float64) float64 {
|
|
|
|
tmp := make([]float64, len(pixels))
|
|
|
|
copy(tmp, pixels)
|
2018-05-07 20:54:48 -07:00
|
|
|
l := len(tmp)
|
2017-07-28 10:18:24 -07:00
|
|
|
pos := l / 2
|
2018-05-07 20:54:48 -07:00
|
|
|
v := quickSelectMedian(tmp, 0, l-1, pos)
|
2017-07-28 10:18:24 -07:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2018-05-07 20:54:48 -07:00
|
|
|
func quickSelectMedian(sequence []float64, low int, hi int, k int) float64 {
|
|
|
|
if low == hi {
|
2017-07-28 10:18:24 -07:00
|
|
|
return sequence[k]
|
|
|
|
}
|
|
|
|
|
2018-05-07 20:54:48 -07:00
|
|
|
for low < hi {
|
|
|
|
pivot := low/2 + hi/2
|
|
|
|
pivotValue := sequence[pivot]
|
|
|
|
storeIdx := low
|
|
|
|
sequence[pivot], sequence[hi] = sequence[hi], sequence[pivot]
|
|
|
|
for i := low; i < hi; i++ {
|
|
|
|
if sequence[i] < pivotValue {
|
|
|
|
sequence[storeIdx], sequence[i] = sequence[i], sequence[storeIdx]
|
|
|
|
storeIdx++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sequence[hi], sequence[storeIdx] = sequence[storeIdx], sequence[hi]
|
|
|
|
if k <= storeIdx {
|
|
|
|
hi = storeIdx
|
|
|
|
} else {
|
|
|
|
low = storeIdx + 1
|
|
|
|
}
|
2017-07-28 10:18:24 -07:00
|
|
|
}
|
|
|
|
|
2018-05-07 20:54:48 -07:00
|
|
|
if len(sequence)%2 == 0 {
|
|
|
|
return sequence[k-1]/2 + sequence[k]/2
|
2017-07-28 10:18:24 -07:00
|
|
|
}
|
2018-05-07 20:54:48 -07:00
|
|
|
return sequence[k]
|
2017-07-28 10:18:24 -07:00
|
|
|
}
|