diff options
author | Nick White <git@njw.name> | 2020-07-23 15:01:41 +0100 |
---|---|---|
committer | Nick White <git@njw.name> | 2020-07-23 15:01:41 +0100 |
commit | d9c6a724e97141db9cbaa66226e0410a5535ae28 (patch) | |
tree | d5c6bba5548bcf269470e5bfe8941a319259f9f8 | |
parent | 1ae8e38ba4d9c141533a5a44e9fc1c470f4b29ff (diff) |
Add Set and NewImage functions to implement image/draw.Image interface
Now the integralimg can be created directly from an image
using image/draw.Draw.
-rw-r--r-- | integralimg.go | 40 | ||||
-rw-r--r-- | integralimg_test.go | 5 |
2 files changed, 36 insertions, 9 deletions
diff --git a/integralimg.go b/integralimg.go index 17d3332..a0cb565 100644 --- a/integralimg.go +++ b/integralimg.go @@ -28,22 +28,50 @@ func (i I) At(x, y int) color.Color { return color.Gray{} } - var oldx, oldy, oldxy uint64 - oldx, oldy, oldxy = 0, 0, 0 + var prevx, prevy, prevxy uint64 + prevx, prevy, prevxy = 0, 0, 0 if x > 0 { - oldx = i[y][x-1] + prevx = i[y][x-1] } if y > 0 { - oldy = i[y-1][x] + prevy = i[y-1][x] } if x > 0 && y > 0 { - oldxy = i[y-1][x-1] + prevxy = i[y-1][x-1] } - orig := i[y][x] + oldxy - oldx - oldy + orig := i[y][x] + prevxy - prevx - prevy return color.Gray{uint8(orig)} } +func (i I) Set(x, y int, c color.Color) { + var prevx, prevy, prevxy uint64 + prevx, prevy, prevxy = 0, 0, 0 + if x > 0 { + prevx = i[y][x-1] + } + if y > 0 { + prevy = i[y-1][x] + } + if x > 0 && y > 0 { + prevxy = i[y-1][x-1] + } + gray := color.GrayModel.Convert(c).(color.Gray).Y + final := uint64(gray) + prevx + prevy - prevxy + i[y][x] = final +} + +// NewImage returns a new Integral Image with the given bounds. +func NewImage(r image.Rectangle) *I { + w, h := r.Dx(), r.Dy() + var rows I + for i := 0; i < h; i++ { + col := make([]uint64, w) + rows = append(rows, col) + } + return &rows +} + // Sq contains an Integral Image and its Square type WithSq struct { Img I diff --git a/integralimg_test.go b/integralimg_test.go index 3bc8dd6..df97caa 100644 --- a/integralimg_test.go +++ b/integralimg_test.go @@ -23,10 +23,9 @@ func TestFromPNG(t *testing.T) { t.Fatalf("Could not decode image: %v\n", err) } b := img.Bounds() - gray := image.NewGray(image.Rect(0, 0, b.Dx(), b.Dy())) - draw.Draw(gray, b, img, b.Min, draw.Src) - integral := ToIntegralImg(gray) + integral := NewImage(image.Rect(0, 0, b.Dx(), b.Dy())) + draw.Draw(integral, b, img, b.Min, draw.Src) if !imgsequal(img, integral) { t.Errorf("Read png image differs to integral\n") |