From ada138dc1ea735239e2a2b810c474e9b5ba652ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Sun, 10 Dec 2023 00:27:13 +0100 Subject: [PATCH] more renaming for consistency --- pkg/pmatch/c.c | 24 ++++++------ pkg/pmatch/c.go | 6 +-- pkg/pmatch/opt.go | 88 +++++++++++++++++++++--------------------- pkg/pmatch/opt_test.go | 4 +- pkg/pmatch/slow.go | 34 ++++++++-------- 5 files changed, 77 insertions(+), 79 deletions(-) diff --git a/pkg/pmatch/c.c b/pkg/pmatch/c.c index 6495d3ed..94a0decd 100644 --- a/pkg/pmatch/c.c +++ b/pkg/pmatch/c.c @@ -13,7 +13,7 @@ void SearchGrayC(const int m, const int n, const int du, const int dv, for (int x = 0; x < m; x++) { const int imgPatStartIx = y * is + x; - uint64_t dot = 0, sqSumI = 0, sqSumP = 0; + uint64_t dot = 0, absI2 = 0, absP2 = 0; for (int v = 0; v < dv; v++) { int pxIi = v * is; @@ -24,17 +24,17 @@ void SearchGrayC(const int m, const int n, const int du, const int dv, const int pxP = patPix[pxPi + u]; dot += (uint64_t)(pxI) * (uint64_t)(pxP); - sqSumI += (uint64_t)(pxI) * (uint64_t)(pxI); - sqSumP += (uint64_t)(pxP) * (uint64_t)(pxP); + absI2 += (uint64_t)(pxI) * (uint64_t)(pxI); + absP2 += (uint64_t)(pxP) * (uint64_t)(pxP); } } - const float64 abs = (float64)(sqSumI) * (float64)(sqSumP); + const float64 abs2 = (float64)(absI2) * (float64)(absP2); float64 cos2; - if (abs == 0) { + if (abs2 == 0) { cos2 = 1; } else { - cos2 = (float64)(dot * dot) / abs; + cos2 = (float64)(dot) * (float64)(dot) / abs2; } #ifdef _OPENMP @@ -64,7 +64,7 @@ void SearchGrayRGBAC(const int m, const int n, const int du, const int dv, for (int x = 0; x < m; x++) { const int imgPatStartIx = y * is + x * four; - uint64_t dot = 0, sqSumI = 0, sqSumP = 0; + uint64_t dot = 0, absI2 = 0, absP2 = 0; for (int v = 0; v < dv; v++) { int pxIi = v * is; @@ -76,18 +76,18 @@ void SearchGrayRGBAC(const int m, const int n, const int du, const int dv, const int pxP = patPix[pxPi + u * four + rgb]; dot += (uint64_t)(pxI) * (uint64_t)(pxP); - sqSumI += (uint64_t)(pxI) * (uint64_t)(pxI); - sqSumP += (uint64_t)(pxP) * (uint64_t)(pxP); + absI2 += (uint64_t)(pxI) * (uint64_t)(pxI); + absP2 += (uint64_t)(pxP) * (uint64_t)(pxP); } } } - const float64 abs = (float64)(sqSumI) * (float64)(sqSumP); + const float64 abs2 = (float64)(absI2) * (float64)(absP2); float64 cos2; - if (abs == 0) { + if (abs2 == 0) { cos2 = 1; } else { - cos2 = (float64)(dot * dot) / abs; + cos2 = (float64)dot * (float64)dot / abs2; } #ifdef _OPENMP diff --git a/pkg/pmatch/c.go b/pkg/pmatch/c.go index 81ac8901..e0f88457 100644 --- a/pkg/pmatch/c.go +++ b/pkg/pmatch/c.go @@ -53,7 +53,7 @@ func SearchGrayC(img, pat *image.Gray) (int, int, float64) { is, ps := img.Stride, pat.Stride var maxX, maxY C.int - var maxCos C.float64 + var maxCos2 C.float64 C.SearchGrayC( C.int(m), C.int(n), C.int(du), C.int(dv), C.int(is), C.int(ps), @@ -61,11 +61,11 @@ func SearchGrayC(img, pat *image.Gray) (int, int, float64) { (*C.uint8_t)(&pat.Pix[0]), (*C.int)(&maxX), (*C.int)(&maxY), - (*C.float64)(&maxCos), + (*C.float64)(&maxCos2), ) // This was left out above. - cos := math.Sqrt(float64(maxCos)) + cos := math.Sqrt(float64(maxCos2)) return int(maxX), int(maxY), cos } diff --git a/pkg/pmatch/opt.go b/pkg/pmatch/opt.go index 81619dee..b5d36551 100644 --- a/pkg/pmatch/opt.go +++ b/pkg/pmatch/opt.go @@ -32,7 +32,7 @@ func SearchGray(img, pat *image.Gray) (maxX, maxY int, maxCos float64) { imgPatStartIx := y*is + x - var dot, sqSumI, sqSumP uint64 + var dot, absI2, absP2 uint64 for v := 0; v < dv; v++ { pxIi := v * is @@ -43,17 +43,17 @@ func SearchGray(img, pat *image.Gray) (maxX, maxY int, maxCos float64) { pxP := pat.Pix[pxPi+u] dot += uint64(pxI) * uint64(pxP) - sqSumI += uint64(pxI) * uint64(pxI) - sqSumP += uint64(pxP) * uint64(pxP) + absI2 += uint64(pxI) * uint64(pxI) + absP2 += uint64(pxP) * uint64(pxP) } } - abs := float64(sqSumI) * float64(sqSumP) + abs2 := float64(absI2) * float64(absP2) var cos2 float64 - if abs == 0 { + if abs2 == 0 { cos2 = 1 } else { - cos2 = float64(dot*dot) / abs + cos2 = float64(dot) * float64(dot) / abs2 } if cos2 > maxCos2 { @@ -72,52 +72,52 @@ func SearchGray(img, pat *image.Gray) (maxX, maxY int, maxCos float64) { // CosSimGray returns the cosine similarity score for two (grayscale) images of the same size. // Slightly optimized implementation. // Panics (due to out of bounds errors) if the sizes don't match. -func CosSimGray(im0, im1 *image.Gray) (cos float64) { - if im0.Bounds().Size() != im1.Bounds().Size() { +func CosSimGray(imA, imB *image.Gray) (cos float64) { + if imA.Bounds().Size() != imB.Bounds().Size() { panic("image sizes do not match") } - du, dv := im1.Bounds().Dx(), im1.Bounds().Dy() - is, ps := im0.Stride, im1.Stride + du, dv := imB.Bounds().Dx(), imB.Bounds().Dy() + is, ps := imA.Stride, imB.Stride - var dot, sqSum0, sqSum1 uint64 + var dot, absA2, absB2 uint64 for v := 0; v < dv; v++ { px0i := v * is px1i := v * ps for u := 0; u < du; u++ { - px0 := im0.Pix[px0i] - px1 := im1.Pix[px1i] + px0 := imA.Pix[px0i] + px1 := imB.Pix[px1i] dot += uint64(px0) * uint64(px1) - sqSum0 += uint64(px0) * uint64(px0) - sqSum1 += uint64(px1) * uint64(px1) + absA2 += uint64(px0) * uint64(px0) + absB2 += uint64(px1) * uint64(px1) px0i++ px1i++ } } - abs := float64(sqSum0) * float64(sqSum1) - if abs == 0 { + abs2 := float64(absA2) * float64(absB2) + if abs2 == 0 { return 1 } - return math.Sqrt(float64(dot*dot) / abs) + return float64(dot) / math.Sqrt(abs2) } // CosSimRGBA is like CosSimGray() but for RGBA images. // Note that the alpha channel is ignored. -func CosSimRGBA(im0, im1 *image.RGBA) (cos float64) { - if im0.Bounds().Size() != im1.Bounds().Size() { +func CosSimRGBA(imA, imB *image.RGBA) (cos float64) { + if imA.Bounds().Size() != imB.Bounds().Size() { panic("image sizes do not match") } - du, dv := im1.Bounds().Dx(), im1.Bounds().Dy() - is, ps := im0.Stride, im1.Stride + du, dv := imB.Bounds().Dx(), imB.Bounds().Dy() + is, ps := imA.Stride, imB.Stride - var dot, sqSum0, sqSum1 uint64 + var dot, absA2, absB2 uint64 for v := 0; v < dv; v++ { px0i := v * is @@ -125,37 +125,37 @@ func CosSimRGBA(im0, im1 *image.RGBA) (cos float64) { for u := 0; u < du; u++ { // R - px0R := im0.Pix[px0i+0] - px1R := im1.Pix[px1i+0] + px0R := imA.Pix[px0i+0] + px1R := imB.Pix[px1i+0] dot += uint64(px0R) * uint64(px1R) - sqSum0 += uint64(px0R) * uint64(px0R) - sqSum1 += uint64(px1R) * uint64(px1R) + absA2 += uint64(px0R) * uint64(px0R) + absB2 += uint64(px1R) * uint64(px1R) // B - px0B := im0.Pix[px0i+1] - px1B := im1.Pix[px1i+1] + px0B := imA.Pix[px0i+1] + px1B := imB.Pix[px1i+1] dot += uint64(px0B) * uint64(px1B) - sqSum0 += uint64(px0B) * uint64(px0B) - sqSum1 += uint64(px1B) * uint64(px1B) + absA2 += uint64(px0B) * uint64(px0B) + absB2 += uint64(px1B) * uint64(px1B) // G - px0G := im0.Pix[px0i+2] - px1G := im1.Pix[px1i+2] + px0G := imA.Pix[px0i+2] + px1G := imB.Pix[px1i+2] dot += uint64(px0G) * uint64(px1G) - sqSum0 += uint64(px0G) * uint64(px0G) - sqSum1 += uint64(px1G) * uint64(px1G) + absA2 += uint64(px0G) * uint64(px0G) + absB2 += uint64(px1G) * uint64(px1G) px0i++ px1i++ } } - abs := float64(sqSum0) * float64(sqSum1) - if abs == 0 { + abs2 := float64(absA2) * float64(absB2) + if abs2 == 0 { return 1 } - return math.Sqrt(float64(dot*dot) / abs) + return float64(dot) / math.Sqrt(abs2) } const four = 4 @@ -187,7 +187,7 @@ func SearchRGBA(img, pat *image.RGBA) (maxX, maxY int, maxCos float64) { imgPatStartIx := y*is + x*four - var dot, sqSumI, sqSumP uint64 + var dot, absI2, absP2 uint64 for v := 0; v < dv; v++ { pxIi := v * is @@ -199,19 +199,19 @@ func SearchRGBA(img, pat *image.RGBA) (maxX, maxY int, maxCos float64) { pxP := pat.Pix[pxPi+u*four+rgb] dot += uint64(pxI) * uint64(pxP) - sqSumI += uint64(pxI) * uint64(pxI) - sqSumP += uint64(pxP) * uint64(pxP) + absI2 += uint64(pxI) * uint64(pxI) + absP2 += uint64(pxP) * uint64(pxP) } } } - abs := float64(sqSumI) * float64(sqSumP) + abs2 := float64(absI2) * float64(absP2) var cos2 float64 - if abs == 0 { + if abs2 == 0 { cos2 = 1 } else { - cos2 = float64(dot*dot) / abs + cos2 = float64(dot) * float64(dot) / abs2 } if cos2 > maxCos2 { diff --git a/pkg/pmatch/opt_test.go b/pkg/pmatch/opt_test.go index bc3cef4b..858cea40 100644 --- a/pkg/pmatch/opt_test.go +++ b/pkg/pmatch/opt_test.go @@ -69,7 +69,7 @@ func Test_CosSimGray(t *testing.T) { assert.Equal(t, 0.9794014202411235, score) score = CosSimGray(im00.(*image.Gray), im01.(*image.Gray)) - assert.Equal(t, 0.9843413588376975, score) + assert.Equal(t, 0.9843413588376974, score) score = CosSimGray(im00.(*image.Gray), im11.(*image.Gray)) assert.Equal(t, 0.9807568924870074, score) @@ -78,7 +78,7 @@ func Test_CosSimGray(t *testing.T) { assert.Equal(t, 0.9551280220660936, score) score = CosSimGray(im00.(*image.Gray), im44.(*image.Gray)) - assert.Equal(t, 0.899141744592327, score) + assert.Equal(t, 0.8991417445923269, score) score = CosSimGray(im00.(*image.Gray), im88.(*image.Gray)) assert.Equal(t, 0.7897102979887818, score) diff --git a/pkg/pmatch/slow.go b/pkg/pmatch/slow.go index 5a532bb3..f26a7632 100644 --- a/pkg/pmatch/slow.go +++ b/pkg/pmatch/slow.go @@ -31,7 +31,7 @@ func imgPatchWindow(img, pat image.Image, offset image.Point) image.Image { func ScoreGrayCosSlow(img, pat *image.Gray, offset image.Point) (cos float64) { img = imgPatchWindow(img, pat, offset).(*image.Gray) - var dot, sqSumI, sqSumP uint64 + var dot, absI2, absP2 uint64 for y := 0; y < pat.Bounds().Dy(); y++ { for x := 0; x < pat.Bounds().Dx(); x++ { @@ -39,17 +39,16 @@ func ScoreGrayCosSlow(img, pat *image.Gray, offset image.Point) (cos float64) { pxP := pat.GrayAt(pat.Bounds().Min.X+x, pat.Bounds().Min.Y+y) dot += uint64(pxI.Y) * uint64(pxP.Y) - sqSumI += uint64(pxI.Y) * uint64(pxI.Y) - sqSumP += uint64(pxP.Y) * uint64(pxP.Y) + absI2 += uint64(pxI.Y) * uint64(pxI.Y) + absP2 += uint64(pxP.Y) * uint64(pxP.Y) } } - absI := math.Sqrt(float64(sqSumI)) - absP := math.Sqrt(float64(sqSumP)) - if absI*absP == 0 { + abs2 := float64(absI2) * float64(absP2) + if abs2 == 0 { return 1 } - return float64(dot) / (absI * absP) + return float64(dot) / math.Sqrt(abs2) } // ScoreRGBACosSlow is like ScoreGrayCosSlow() but for RGBA images. @@ -57,7 +56,7 @@ func ScoreGrayCosSlow(img, pat *image.Gray, offset image.Point) (cos float64) { func ScoreRGBACosSlow(img, pat *image.RGBA, offset image.Point) (cos float64) { img = imgPatchWindow(img, pat, offset).(*image.RGBA) - var dot, sqSumI, sqSumP uint64 + var dot, absI2, absP2 uint64 for y := 0; y < pat.Rect.Dy(); y++ { for x := 0; x < pat.Rect.Dx(); x++ { @@ -68,22 +67,21 @@ func ScoreRGBACosSlow(img, pat *image.RGBA, offset image.Point) (cos float64) { dot += uint64(pxI.G) * uint64(pxP.G) dot += uint64(pxI.B) * uint64(pxP.B) - sqSumI += uint64(pxI.R) * uint64(pxI.R) - sqSumI += uint64(pxI.G) * uint64(pxI.G) - sqSumI += uint64(pxI.B) * uint64(pxI.B) + absI2 += uint64(pxI.R) * uint64(pxI.R) + absI2 += uint64(pxI.G) * uint64(pxI.G) + absI2 += uint64(pxI.B) * uint64(pxI.B) - sqSumP += uint64(pxP.R) * uint64(pxP.R) - sqSumP += uint64(pxP.G) * uint64(pxP.G) - sqSumP += uint64(pxP.B) * uint64(pxP.B) + absP2 += uint64(pxP.R) * uint64(pxP.R) + absP2 += uint64(pxP.G) * uint64(pxP.G) + absP2 += uint64(pxP.B) * uint64(pxP.B) } } - absI := math.Sqrt(float64(sqSumI)) - absP := math.Sqrt(float64(sqSumP)) - if absI*absP == 0 { + abs2 := float64(absI2) * float64(absP2) + if abs2 == 0 { return 1 } - return float64(dot) / (absI * absP) + return float64(dot) / math.Sqrt(abs2) } // SearchGraySlow searches for the position of a (grayscale) patch in a (grayscale) image,