Skip to content

Commit

Permalink
more renaming for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-m committed Dec 9, 2023
1 parent 0bbbe7a commit ada138d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 79 deletions.
24 changes: 12 additions & 12 deletions pkg/pmatch/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/pmatch/c.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ 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),
(*C.uint8_t)(&img.Pix[0]),
(*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
}
Expand Down
88 changes: 44 additions & 44 deletions pkg/pmatch/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -72,90 +72,90 @@ 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
px1i := v * ps

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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/pmatch/opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
34 changes: 16 additions & 18 deletions pkg/pmatch/slow.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,32 @@ 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++ {
pxI := img.GrayAt(img.Bounds().Min.X+x, img.Bounds().Min.Y+y)
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.
// Note that the alpha channel is ignored.
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++ {
Expand All @@ -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,
Expand Down

0 comments on commit ada138d

Please sign in to comment.