Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DATA-535] Create an orbslam integration test that runs mono YCbCr im… #1502

Merged
merged 2 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 32 additions & 94 deletions .artifact/tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -50802,129 +50802,67 @@
}
}
},
"temp_mock_camera": {
"color": {
"0.png": {
"hash": "d6a4ad2b5d0a07cb42ce0a968bdd2d1e",
"size": 1074038
},
"1.png": {
"hash": "bf162ae919b499fb2a31c1c0651bc7e7",
"size": 1110424
},
"10.png": {
"hash": "7ecd6687e8af2f741b74fd6d61e5b94f",
"size": 1262970
},
"11.png": {
"hash": "e49a10305cd6444d65a2e259e092dcf3",
"size": 1320711
},
"12.png": {
"hash": "ccc96509aecebfcad460f66a8980ea9b",
"size": 1383680
},
"13.png": {
"hash": "a54e3cad45855001accb0f3e90cc4901",
"size": 1232388
},
"14.png": {
"hash": "2d0fcc43e04cfa64533f066f08286c8c",
"size": 1319859
},
"2.png": {
"hash": "561f98f2fdb955118c396071cb768711",
"size": 1113264
},
"3.png": {
"hash": "0b1245610799dfc255a39c7b28592839",
"size": 1126246
},
"4.png": {
"hash": "f87169c5c85b32e255f59aa50856ff27",
"size": 1126679
},
"5.png": {
"hash": "769088a8ebdbb1ce2979b64808ac707d",
"size": 1108197
},
"6.png": {
"hash": "fcc66c01f0f1cb5c200d0b3a67d0991e",
"size": 1118615
},
"7.png": {
"hash": "f81538daa0836653b935b97c99a60142",
"size": 1094447
},
"8.png": {
"hash": "9668c21a11b5abedb1254770dafb959d",
"size": 1246588
},
"9.png": {
"hash": "5e6afad5bf9912b1d69ed06d357f0b36",
"size": 1269381
}
},
"depth": {
"mock_mono_camera": {
"rgb": {
"0.png": {
"hash": "e49b03a3e9fe6a9e41d67e3308a70120",
"size": 245153
"hash": "c4dc7ae86a546684f3c9cbe0e47acf55",
"size": 834386
},
"1.png": {
"hash": "34e0a2ae549be6f507b8f4e27f31f2e2",
"size": 345685
"hash": "c4dc7ae86a546684f3c9cbe0e47acf55",
"size": 834386
},
"10.png": {
"hash": "65517158a4175157ab3636e1ef9ac90d",
"size": 500936
"hash": "97cb4afc22754e0433303ae8b44d380f",
"size": 813831
},
"11.png": {
"hash": "593e152cea83f14e1c4b4cec78e0b33b",
"size": 528680
"hash": "f87b5643a7b58bc4cdf231ab096038b7",
"size": 833993
},
"12.png": {
"hash": "0abc4c2361127eb343572390c8d9f35c",
"size": 528095
"hash": "f87b5643a7b58bc4cdf231ab096038b7",
"size": 833993
},
"13.png": {
"hash": "3f28b21a7206a6e6cc109fd2b03d432b",
"size": 526535
"hash": "c40ec042eb926ed5e012c0a505b0453d",
"size": 787645
},
"14.png": {
"hash": "f5142f9dbe12b339f8b4a0b92abe22f9",
"size": 485705
"hash": "87f3db7908d0455ffe3b2a51093abe16",
"size": 824040
},
"2.png": {
"hash": "34e0a2ae549be6f507b8f4e27f31f2e2",
"size": 345685
"hash": "c4dc7ae86a546684f3c9cbe0e47acf55",
"size": 834386
},
"3.png": {
"hash": "2a3b9d6fa3177270c8be3fc48f12b6e9",
"size": 480005
"hash": "c4dc7ae86a546684f3c9cbe0e47acf55",
"size": 834386
},
"4.png": {
"hash": "59765705cd3ff8331b908f5a2f2b7d20",
"size": 479413
"hash": "f87b5643a7b58bc4cdf231ab096038b7",
"size": 833993
},
"5.png": {
"hash": "bbb525470e857878207ce030a0d78b26",
"size": 479728
"hash": "f87b5643a7b58bc4cdf231ab096038b7",
"size": 833993
},
"6.png": {
"hash": "ed4df21dd6036b0cda823de71dd81c53",
"size": 513825
"hash": "d17c07b754a6c274915a94a2dd978c02",
"size": 819850
},
"7.png": {
"hash": "b37632c463e4a4a78b3bcd172a215ea5",
"size": 511135
"hash": "87f3db7908d0455ffe3b2a51093abe16",
"size": 824040
},
"8.png": {
"hash": "95683c3b3c6a93282c3d06fabcd149ed",
"size": 503015
"hash": "c40ec042eb926ed5e012c0a505b0453d",
"size": 787645
},
"9.png": {
"hash": "fcac230bb47d1617838e48fe3ca10f56",
"size": 505238
"hash": "97cb4afc22754e0433303ae8b44d380f",
"size": 813831
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions rimage/image_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,51 @@ func EdgeHysteresisFiltering(mag *mat.Dense, low, high float64) (*image.Gray, er
}
return edges, nil
}

// ImageToYCbCrForTesting converts an image to YCbCr. It is only to be used for testing.
func ImageToYCbCrForTesting(dst *image.YCbCr, src image.Image) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find another was to convert to YCbCr, so I exported this function. Let me know if you have another idea!

Copy link
Member

@bhaney bhaney Oct 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can probably use the draw.Draw function to do this

import "image/draw"

func MakeYCbCr(pic image.Image) *image.YCbCr {
    result := image.NewYCbCr(pic.Bounds())
    draw.Draw(result, result.Bounds(), pic, pic.Bounds().Min, draw.Src)
    return result
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like I can do this, since image.YCbCr doesn't implement draw.Image (missing method Set).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wow, yea it looks like YCbCr isn't pixel addressable! So, yea, this function seems like the best bet for now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just found an alternative - but if you would like to stick to using one set of images (the png sources) then what you have now seems like the way to go

if dst == nil {
panic("dst can't be nil")
}

yuvImg, ok := src.(*image.YCbCr)
if ok {
*dst = *yuvImg
return
}

bounds := src.Bounds()
dy := bounds.Dy()
dx := bounds.Dx()
flat := dy * dx

if len(dst.Y)+len(dst.Cb)+len(dst.Cr) < 3*flat {
i0 := 1 * flat
i1 := 2 * flat
i2 := 3 * flat
if cap(dst.Y) < i2 {
dst.Y = make([]uint8, i2)
}
dst.Y = dst.Y[:i0]
dst.Cb = dst.Y[i0:i1]
dst.Cr = dst.Y[i1:i2]
}
dst.SubsampleRatio = image.YCbCrSubsampleRatio444
dst.YStride = dx
dst.CStride = dx
dst.Rect = bounds

i := 0
for yi := 0; yi < dy; yi++ {
for xi := 0; xi < dx; xi++ {
// TODO(erh): probably try to get the alpha value with something like
// https://en.wikipedia.org/wiki/Alpha_compositing
r, g, b, _ := src.At(xi, yi).RGBA()
yy, cb, cr := color.RGBToYCbCr(uint8(r/256), uint8(g/256), uint8(b/256))
dst.Y[i] = yy
dst.Cb[i] = cb
dst.Cr[i] = cr
i++
}
}
}
52 changes: 2 additions & 50 deletions rimage/image_processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rimage
import (
"fmt"
"image"
"image/color"
"math"
"testing"

Expand Down Expand Up @@ -116,59 +115,12 @@ func BenchmarkConvertImage(b *testing.B) {
}
}

func imageToYCbCr(dst *image.YCbCr, src image.Image) {
if dst == nil {
panic("dst can't be nil")
}

yuvImg, ok := src.(*image.YCbCr)
if ok {
*dst = *yuvImg
return
}

bounds := src.Bounds()
dy := bounds.Dy()
dx := bounds.Dx()
flat := dy * dx

if len(dst.Y)+len(dst.Cb)+len(dst.Cr) < 3*flat {
i0 := 1 * flat
i1 := 2 * flat
i2 := 3 * flat
if cap(dst.Y) < i2 {
dst.Y = make([]uint8, i2)
}
dst.Y = dst.Y[:i0]
dst.Cb = dst.Y[i0:i1]
dst.Cr = dst.Y[i1:i2]
}
dst.SubsampleRatio = image.YCbCrSubsampleRatio444
dst.YStride = dx
dst.CStride = dx
dst.Rect = bounds

i := 0
for yi := 0; yi < dy; yi++ {
for xi := 0; xi < dx; xi++ {
// TODO(erh): probably try to get the alpha value with something like
// https://en.wikipedia.org/wiki/Alpha_compositing
r, g, b, _ := src.At(xi, yi).RGBA()
yy, cb, cr := color.RGBToYCbCr(uint8(r/256), uint8(g/256), uint8(b/256))
dst.Y[i] = yy
dst.Cb[i] = cb
dst.Cr[i] = cr
i++
}
}
}

func TestConvertYCbCr(t *testing.T) {
orig, err := readImageFromFile(artifact.MustPath("rimage/canny1.png"))
test.That(t, err, test.ShouldBeNil)

var yuvImg image.YCbCr
imageToYCbCr(&yuvImg, orig)
ImageToYCbCrForTesting(&yuvImg, orig)

err = WriteImageToFile(outDir+"/canny1-ycbcr.png", &yuvImg)
test.That(t, err, test.ShouldBeNil)
Expand All @@ -185,7 +137,7 @@ func BenchmarkConvertImageYCbCr(b *testing.B) {
test.That(b, err, test.ShouldBeNil)

var yuvImg image.YCbCr
imageToYCbCr(&yuvImg, orig)
ImageToYCbCrForTesting(&yuvImg, orig)

b.ResetTimer()

Expand Down
6 changes: 3 additions & 3 deletions services/slam/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ func (slamSvc *builtIn) StopSLAMProcess() error {
return nil
}

func (slamSvc *builtIn) getLazyPNGImage(ctx context.Context, cam camera.Camera) ([]byte, func(), error) {
func (slamSvc *builtIn) getPNGImage(ctx context.Context, cam camera.Camera) ([]byte, func(), error) {
// We will hint that we want a PNG.
// The Camera service server implementation in RDK respects this; others may not.
img, release, err := camera.ReadImage(
Expand Down Expand Up @@ -798,7 +798,7 @@ func (slamSvc *builtIn) getAndSaveDataSparse(
return nil, errors.Errorf("expected 1 camera for mono slam, found %v", len(camStreams))
}

image, release, err := slamSvc.getLazyPNGImage(ctx, cams[0])
image, release, err := slamSvc.getPNGImage(ctx, cams[0])
if err != nil {
if err.Error() == opTimeoutErrorMessage {
slamSvc.logger.Warnw("Skipping this scan due to error", "error", err)
Expand Down Expand Up @@ -893,7 +893,7 @@ func (slamSvc *builtIn) getSimultaneousColorAndDepth(
goutils.PanicCapturingGo(func() {
defer slamSvc.activeBackgroundWorkers.Done()
defer wg.Done()
images[iLoop], releaseFuncs[iLoop], errs[iLoop] = slamSvc.getLazyPNGImage(ctx, cams[iLoop])
images[iLoop], releaseFuncs[iLoop], errs[iLoop] = slamSvc.getPNGImage(ctx, cams[iLoop])
})
}
wg.Wait()
Expand Down
Loading