goimagehash/etcs/utils.go

79 lines
1.7 KiB
Go
Raw Normal View History

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
2024-04-05 16:29:03 -07:00
import "golang.org/x/exp/constraints"
type Number interface {
constraints.Integer | constraints.Float
}
2017-07-31 07:24:04 -07:00
// MeanOfPixels function returns a mean of pixels.
2024-04-05 16:29:03 -07:00
func MeanOfPixels[N Number](pixels []N) N {
var m float64 = 0
lens := float64(len(pixels))
2017-07-28 10:18:24 -07:00
if lens == 0 {
return 0
}
for _, p := range pixels {
2024-04-05 16:29:03 -07:00
m += float64(p)
2017-07-28 10:18:24 -07:00
}
2024-04-05 16:29:03 -07:00
// print(m," ", lens, "\n")
2017-07-28 10:18:24 -07:00
2024-04-05 16:29:03 -07:00
return N(m / lens)
2017-07-28 10:18:24 -07:00
}
2017-07-31 07:24:04 -07:00
// MedianOfPixels function returns a median value of pixels.
// It uses quick selection algorithm.
2024-04-05 16:29:03 -07:00
func MedianOfPixels[N Number](pixels []N) N {
tmp := make([]N, len(pixels))
2017-07-28 10:18:24 -07:00
copy(tmp, pixels)
l := len(tmp)
2017-07-28 10:18:24 -07:00
pos := l / 2
v := quickSelectMedian(tmp, 0, l-1, pos)
2017-07-28 10:18:24 -07:00
return v
}
// MedianOfPixelsFast64 function returns a median value of pixels.
// It uses quick selection algorithm.
2024-04-05 16:29:03 -07:00
func MedianOfPixelsFast64[N Number](pixels []N) N {
tmp := [64]N{}
copy(tmp[:], pixels)
l := len(tmp)
pos := l / 2
return quickSelectMedian(tmp[:], 0, l-1, pos)
}
2024-04-05 16:29:03 -07:00
func quickSelectMedian[N Number](sequence []N, low, hi, k int) N {
if low == hi {
2017-07-28 10:18:24 -07:00
return sequence[k]
}
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
}
if len(sequence)%2 == 0 {
return sequence[k-1]/2 + sequence[k]/2
2017-07-28 10:18:24 -07:00
}
return sequence[k]
2017-07-28 10:18:24 -07:00
}