本文整理汇总了Golang中github.com/jvlmdr/go-cv/rimg64.Multi.Size方法的典型用法代码示例。如果您正苦于以下问题:Golang Multi.Size方法的具体用法?Golang Multi.Size怎么用?Golang Multi.Size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jvlmdr/go-cv/rimg64.Multi
的用法示例。
在下文中一共展示了Multi.Size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CorrMultiBankStrideBLAS
// CorrMultiBankStrideBLAS computes the strided correlation of
// a multi-channel image with a bank of multi-channel filters.
// h_p[u, v] = sum_q (f_q corr g_pq)[stride*u, stride*v]
func CorrMultiBankStrideBLAS(f *rimg64.Multi, g *MultiBank, stride int) (*rimg64.Multi, error) {
out := ValidSizeStride(f.Size(), g.Size(), stride)
if out.X <= 0 || out.Y <= 0 {
return nil, nil
}
h := rimg64.NewMulti(out.X, out.Y, len(g.Filters))
// Size of filters.
m, n, k := g.Width, g.Height, g.Channels
// Express as dense matrix multiplication.
// h_p[u, v] = sum_q (f_q corr g_pq)[u, v]
// h = A(f) X(g)
// where A is whk by mnk
// with w = ceil[(M-m+1)/stride],
// h = ceil[(N-n+1)/stride].
a := blas.NewMat(h.Width*h.Height, m*n*k)
{
var r int
for u := 0; u < h.Width; u++ {
for v := 0; v < h.Height; v++ {
var s int
for i := 0; i < g.Width; i++ {
for j := 0; j < g.Height; j++ {
for q := 0; q < g.Channels; q++ {
a.Set(r, s, f.At(stride*u+i, stride*v+j, q))
s++
}
}
}
r++
}
}
}
x := blas.NewMat(m*n*k, h.Channels)
{
var r int
for i := 0; i < g.Width; i++ {
for j := 0; j < g.Height; j++ {
for q := 0; q < g.Channels; q++ {
for p := 0; p < h.Channels; p++ {
x.Set(r, p, g.Filters[p].At(i, j, q))
}
r++
}
}
}
}
y := blas.MatMul(1, a, x)
{
var r int
for u := 0; u < h.Width; u++ {
for v := 0; v < h.Height; v++ {
for p := 0; p < h.Channels; p++ {
h.Set(u, v, p, y.At(r, p))
}
r++
}
}
}
return h, nil
}
示例2: CorrMultiStrideBLAS
// CorrMultiStrideBLAS computes the strided correlation of
// a multi-channel image with a multi-channel filter.
// h[u, v] = sum_q (f_q corr g_q)[stride*u, stride*v]
func CorrMultiStrideBLAS(f, g *rimg64.Multi, stride int) (*rimg64.Image, error) {
out := ValidSizeStride(f.Size(), g.Size(), stride)
if out.X <= 0 || out.Y <= 0 {
return nil, nil
}
h := rimg64.New(out.X, out.Y)
// Size of filters.
m, n, k := g.Width, g.Height, g.Channels
// Express as dense matrix multiplication.
// h[u, v] = sum_q (f_q corr g_q)[stride*u, stride*v]
// y(h) = A(f) x(g)
// where A is wh by mnk
// with w = ceil[(M-m+1)/stride],
// h = ceil[(N-n+1)/stride].
a := blas.NewMat(h.Width*h.Height, m*n*k)
{
var r int
for u := 0; u < h.Width; u++ {
for v := 0; v < h.Height; v++ {
var s int
for i := 0; i < g.Width; i++ {
for j := 0; j < g.Height; j++ {
for q := 0; q < g.Channels; q++ {
a.Set(r, s, f.At(stride*u+i, stride*v+j, q))
s++
}
}
}
r++
}
}
}
x := blas.NewMat(m*n*k, 1)
{
var r int
for i := 0; i < g.Width; i++ {
for j := 0; j < g.Height; j++ {
for q := 0; q < g.Channels; q++ {
x.Set(r, 0, g.At(i, j, q))
r++
}
}
}
}
y := blas.MatMul(1, a, x)
{
var r int
for u := 0; u < h.Width; u++ {
for v := 0; v < h.Height; v++ {
h.Set(u, v, y.At(r, 0))
r++
}
}
}
return h, nil
}
示例3: CorrMultiStrideFFT
// CorrMultiStrideFFT computes the correlation of
// a multi-channel image with a multi-channel filter.
// h[u, v] = sum_q (f_q corr g_q)[u, v]
func CorrMultiStrideFFT(f, g *rimg64.Multi, stride int) (*rimg64.Image, error) {
if err := errIfChannelsNotEq(f, g); err != nil {
panic(err)
}
out := ValidSizeStride(f.Size(), g.Size(), stride)
if out.X <= 0 || out.Y <= 0 {
return nil, nil
}
// Compute strided convolution as the sum over
// a stride x stride grid of small convolutions.
grid := image.Pt(stride, stride)
// But do not divide into a larger grid than the size of the filter.
// If the filter is smaller than the stride,
// then some pixels in the image will not affect the output.
grid.X = min(grid.X, g.Width)
grid.Y = min(grid.Y, g.Height)
// Determine the size of the sub-sampled filter.
gsub := image.Pt(ceilDiv(g.Width, grid.X), ceilDiv(g.Height, grid.Y))
// The sub-sampled size of the image should be such that
// the output size is attained.
fsub := image.Pt(out.X+gsub.X-1, out.Y+gsub.Y-1)
// Determine optimal size for FFT.
work, _ := FFT2Size(fsub)
// Cache FFT of each channel of image for convolving with multiple filters.
// Re-use plan for multiple convolutions too.
fhat := fftw.NewArray2(work.X, work.Y)
ffwd := fftw.NewPlan2(fhat, fhat, fftw.Forward, fftw.Estimate)
defer ffwd.Destroy()
ghat := fftw.NewArray2(work.X, work.Y)
gfwd := fftw.NewPlan2(ghat, ghat, fftw.Forward, fftw.Estimate)
defer gfwd.Destroy()
// Normalization factor.
alpha := complex(1/float64(work.X*work.Y), 0)
// Add the convolutions over channels and strides.
hhat := fftw.NewArray2(work.X, work.Y)
for k := 0; k < f.Channels; k++ {
for i := 0; i < grid.X; i++ {
for j := 0; j < grid.Y; j++ {
// Copy each downsampled channel and take its transform.
copyChannelStrideTo(fhat, f, k, stride, image.Pt(i, j))
ffwd.Execute()
copyChannelStrideTo(ghat, g, k, stride, image.Pt(i, j))
gfwd.Execute()
addMul(hhat, ghat, fhat)
}
}
}
// Take the inverse transform.
h := rimg64.New(out.X, out.Y)
scale(alpha, hhat)
fftw.IFFT2To(hhat, hhat)
copyRealTo(h, hhat)
return h, nil
}
示例4: DecimateMulti
// DecimateMulti takes every r-th sample starting at (0, 0).
func DecimateMulti(f *rimg64.Multi, r int) *rimg64.Multi {
out := ceilDivPt(f.Size(), r)
g := rimg64.NewMulti(out.X, out.Y, f.Channels)
for i := 0; i < g.Width; i++ {
for j := 0; j < g.Height; j++ {
for k := 0; k < g.Channels; k++ {
g.Set(i, j, k, f.At(r*i, r*j, k))
}
}
}
return g
}
示例5: Apply
func (phi *ConvEach) Apply(x *rimg64.Multi) (*rimg64.Multi, error) {
channels := x.Channels * len(phi.Filters.Filters)
field := image.Pt(phi.Filters.Width, phi.Filters.Height)
size := slide.ValidSize(x.Size(), field)
y := rimg64.NewMulti(size.X, size.Y, channels)
var n int
for i := 0; i < x.Channels; i++ {
// Convolve each channel of the input with the bank.
yi, err := slide.CorrBankBLAS(x.Channel(i), phi.Filters)
if err != nil {
return nil, err
}
for j := 0; j < yi.Channels; j++ {
// Copy the channels into the output.
y.SetChannel(n, yi.Channel(j))
n++
}
}
return y, nil
}
示例6: CorrMultiBankFFT
// CorrMultiBankFFT computes the correlation of
// a multi-channel image with a bank of multi-channel filters.
// h_p[u, v] = sum_q (f_q corr g_pq)[u, v]
func CorrMultiBankFFT(f *rimg64.Multi, g *MultiBank) (*rimg64.Multi, error) {
out := ValidSize(f.Size(), g.Size())
if out.X <= 0 || out.Y <= 0 {
return nil, nil
}
// Determine optimal size for FFT.
work, _ := FFT2Size(f.Size())
// Cache FFT of each channel of image.
fhat := make([]*fftw.Array2, f.Channels)
for i := range fhat {
fhat[i] = fftw.NewArray2(work.X, work.Y)
copyChannelTo(fhat[i], f, i)
fftw.FFT2To(fhat[i], fhat[i])
}
curr := fftw.NewArray2(work.X, work.Y)
fwd := fftw.NewPlan2(curr, curr, fftw.Forward, fftw.Estimate)
defer fwd.Destroy()
sum := fftw.NewArray2(work.X, work.Y)
bwd := fftw.NewPlan2(sum, sum, fftw.Backward, fftw.Estimate)
defer bwd.Destroy()
h := rimg64.NewMulti(out.X, out.Y, len(g.Filters))
alpha := complex(1/float64(work.X*work.Y), 0)
// For each output channel.
for p, gp := range g.Filters {
zero(sum)
// For each input channel.
for q := 0; q < f.Channels; q++ {
// Take FFT of this input channel.
copyChannelTo(curr, gp, q)
fwd.Execute()
// h_p[x] = (G_qp corr F_p)[x]
// H_p[x] = conj(G_qp[x]) F_p[x]
addScaleMul(sum, alpha, curr, fhat[q])
}
bwd.Execute()
copyRealToChannel(h, p, sum)
}
return h, nil
}
示例7: CorrMultiFFT
// CorrMultiBankFFT computes the correlation of
// a multi-channel image with a multi-channel filter.
// h[u, v] = sum_p (f_p corr g_p)[u, v]
func CorrMultiFFT(f, g *rimg64.Multi) (*rimg64.Image, error) {
if err := errIfChannelsNotEq(f, g); err != nil {
panic(err)
}
out := ValidSize(f.Size(), g.Size())
if out.Eq(image.ZP) {
return nil, nil
}
work, _ := FFT2Size(f.Size())
fhat := fftw.NewArray2(work.X, work.Y)
ghat := fftw.NewArray2(work.X, work.Y)
ffwd := fftw.NewPlan2(fhat, fhat, fftw.Forward, fftw.Estimate)
defer ffwd.Destroy()
gfwd := fftw.NewPlan2(ghat, ghat, fftw.Forward, fftw.Estimate)
defer gfwd.Destroy()
hhat := fftw.NewArray2(work.X, work.Y)
for p := 0; p < f.Channels; p++ {
// Take transform of each channel.
copyChannelTo(fhat, f, p)
ffwd.Execute()
copyChannelTo(ghat, g, p)
gfwd.Execute()
addMul(hhat, ghat, fhat)
}
n := float64(work.X * work.Y)
scale(complex(1/n, 0), hhat)
fftw.IFFT2To(hhat, hhat)
h := rimg64.New(out.X, out.Y)
copyRealTo(h, hhat)
return h, nil
}
示例8: CorrMultiStrideNaive
// CorrMultiStrideNaive computes the correlation of
// a multi-channel image with a multi-channel filter.
// h[u, v] = sum_q (f_q corr g_q)[u, v]
func CorrMultiStrideNaive(f, g *rimg64.Multi, stride int) (*rimg64.Image, error) {
if err := errIfChannelsNotEq(f, g); err != nil {
panic(err)
}
out := ValidSizeStride(f.Size(), g.Size(), stride)
h := rimg64.New(out.X, out.Y)
for i := 0; i < h.Width; i++ {
for j := 0; j < h.Height; j++ {
var total float64
for u := 0; u < g.Width; u++ {
for v := 0; v < g.Height; v++ {
p := image.Pt(i, j).Mul(stride).Add(image.Pt(u, v))
for k := 0; k < f.Channels; k++ {
total += f.At(p.X, p.Y, k) * g.At(u, v, k)
}
}
}
h.Set(i, j, total)
}
}
return h, nil
}
示例9: CorrMultiBankStrideNaive
// CorrMultiBankStrideNaive computes the strided correlation of
// a multi-channel image with a bank of multi-channel filters.
// h_p[u, v] = sum_q (f_q corr g_pq)[stride*u, stride*v]
func CorrMultiBankStrideNaive(f *rimg64.Multi, g *MultiBank, stride int) (*rimg64.Multi, error) {
out := ValidSizeStride(f.Size(), g.Size(), stride)
if out.X <= 0 || out.Y <= 0 {
return nil, nil
}
h := rimg64.NewMulti(out.X, out.Y, len(g.Filters))
for u := 0; u < h.Width; u++ {
for v := 0; v < h.Height; v++ {
for p := 0; p < h.Channels; p++ {
for i := 0; i < g.Width; i++ {
for j := 0; j < g.Height; j++ {
for q := 0; q < g.Channels; q++ {
val := f.At(stride*u+i, stride*v+j, q) * g.Filters[p].At(i, j, q)
h.Set(u, v, p, h.At(u, v, p)+val)
}
}
}
}
}
}
return h, nil
}
示例10: Score
func (f *AffineScorer) Score(x *rimg64.Multi) (float64, error) {
if f.Op != Cos {
panic("cosine unimplemented")
}
if !x.Size().Eq(f.Tmpl.Size()) {
return 0, fmt.Errorf("different size: input %v, template %v", x.Size(), f.Tmpl.Size())
}
if x.Channels != f.Tmpl.Channels {
return 0, fmt.Errorf("different channels: input %v, template %v", x.Channels, f.Tmpl.Channels)
}
size := f.Tmpl.Size()
var y float64
for i := 0; i < size.X; i++ {
for j := 0; j < size.Y; j++ {
for k := 0; k < f.Tmpl.Channels; k++ {
y += x.At(i, j, k) * f.Tmpl.At(i, j, k)
}
}
}
y += f.Bias
return y, nil
}
示例11: CorrMultiBankNaive
// CorrMultiBankNaive computes the correlation of
// a multi-channel image with a bank of multi-channel filters.
// h_p[u, v] = sum_q (f_q corr g_pq)[u, v]
func CorrMultiBankNaive(f *rimg64.Multi, g *MultiBank) (*rimg64.Multi, error) {
out := ValidSize(f.Size(), g.Size())
if out.X <= 0 || out.Y <= 0 {
return nil, nil
}
h := rimg64.NewMulti(out.X, out.Y, len(g.Filters))
for u := 0; u < h.Width; u++ {
for v := 0; v < h.Height; v++ {
for p := 0; p < h.Channels; p++ {
var sum float64
for i := 0; i < g.Width; i++ {
for j := 0; j < g.Height; j++ {
for q := 0; q < g.Channels; q++ {
sum += f.At(i+u, j+v, q) * g.Filters[p].At(i, j, q)
}
}
}
h.Set(u, v, p, sum)
}
}
}
return h, nil
}
示例12: CorrMultiNaive
// CorrMultiNaive computes the correlation of
// a multi-channel image with a multi-channel filter.
// h[u, v] = sum_p (f_p corr g_p)[u, v]
func CorrMultiNaive(f, g *rimg64.Multi) (*rimg64.Image, error) {
if err := errIfChannelsNotEq(f, g); err != nil {
panic(err)
}
out := ValidSize(f.Size(), g.Size())
if out.Eq(image.ZP) {
return nil, nil
}
h := rimg64.New(out.X, out.Y)
for i := 0; i < out.X; i++ {
for j := 0; j < out.Y; j++ {
var total float64
for u := 0; u < g.Width; u++ {
for v := 0; v < g.Height; v++ {
for p := 0; p < f.Channels; p++ {
total += f.At(i+u, j+v, p) * g.At(u, v, p)
}
}
}
h.Set(i, j, total)
}
}
return h, nil
}
示例13: errIfNotEqMulti
func errIfNotEqMulti(f, g *rimg64.Multi, eps float64) error {
if !f.Size().Eq(g.Size()) {
return fmt.Errorf("different size: %v, %v", f.Size(), g.Size())
}
if f.Channels != g.Channels {
return fmt.Errorf("different channels: %d, %d", f.Channels, g.Channels)
}
for i := 0; i < f.Width; i++ {
for j := 0; j < f.Height; j++ {
for k := 0; k < f.Channels; k++ {
a, b := f.At(i, j, k), g.At(i, j, k)
if math.Abs(a-b) > eps*math.Max(math.Abs(a), math.Abs(b)) {
return fmt.Errorf("different at x %d, y %d, c %d: %g, %g", i, j, k, a, b)
}
}
}
}
return nil
}
示例14: CorrMultiAuto
// CorrMultiAuto computes the correlation of
// a multi-channel image with a multi-channel filter.
// h[u, v] = sum_p (f_p corr g_p)[u, v]
// Automatically selects between naive and Fourier-domain convolution.
func CorrMultiAuto(f, g *rimg64.Multi) (*rimg64.Image, error) {
if err := errIfChannelsNotEq(f, g); err != nil {
panic(err)
}
// Size of output.
size := ValidSize(f.Size(), g.Size())
// Return empty image if that's the result.
if size.Eq(image.ZP) {
return nil, nil
}
// Need to compute one inner product per output element.
naiveMuls := size.X * size.Y * g.Width * g.Height
// Optimal FFT size and number of multiplications.
_, fftMuls := FFT2Size(f.Size())
// Need to perform two forward and one inverse transform.
fftMuls *= 3
// Switch implementation based on image size.
if fftMuls < naiveMuls {
return CorrMultiFFT(f, g)
}
return CorrMultiNaive(f, g)
}
示例15: CorrMultiBankBLAS
// CorrMultiBankBLAS computes the correlation of
// a multi-channel image with a bank of multi-channel filters.
// h_p[u, v] = sum_q (f_q corr g_pq)[u, v]
func CorrMultiBankBLAS(f *rimg64.Multi, g *MultiBank) (*rimg64.Multi, error) {
out := ValidSize(f.Size(), g.Size())
if out.X <= 0 || out.Y <= 0 {
return nil, nil
}
// Express as dense matrix multiplication.
// h_p[u, v] = sum_q (f_q corr g_pq)[u, v]
// Y(h) = A(f) X(g)
// If the number of input and output channels are Q and P, then
// A is (M-m+1)(N-n+1) x mnQ and
// X is mnQ x P, so that
// Y is (M-m+1)(N-n+1) x P.
// Note that the time to build the system is therefore
// affected more by the number of input channels Q than outputs P.
h := rimg64.NewMulti(out.X, out.Y, len(g.Filters))
M, N, K := h.Width, h.Height, h.Channels
m, n, k := g.Width, g.Height, g.Channels
a := blas.NewMat(M*N, m*n*k)
{
var r int
for u := 0; u < h.Width; u++ {
for v := 0; v < h.Height; v++ {
var s int
for i := 0; i < g.Width; i++ {
for j := 0; j < g.Height; j++ {
for q := 0; q < g.Channels; q++ {
a.Set(r, s, f.At(i+u, j+v, q))
s++
}
}
}
r++
}
}
}
x := blas.NewMat(m*n*k, K)
{
var r int
for i := 0; i < g.Width; i++ {
for j := 0; j < g.Height; j++ {
for q := 0; q < g.Channels; q++ {
for p := 0; p < h.Channels; p++ {
x.Set(r, p, g.Filters[p].At(i, j, q))
}
r++
}
}
}
}
y := blas.MatMul(1, a, x)
{
var r int
for u := 0; u < h.Width; u++ {
for v := 0; v < h.Height; v++ {
for p := 0; p < h.Channels; p++ {
h.Set(u, v, p, y.At(r, p))
}
r++
}
}
}
return h, nil
}