blob: 2ae56ac770936cf1c98c72579e80ffecec84e020 [file] [log] [blame]
Parker Schuh6691f192017-01-14 17:01:02 -08001#ifndef _AOS_VISION_BLOB_STREAM_VIEW_H_
2#define _AOS_VISION_BLOB_STREAM_VIEW_H_
3
4#include "aos/vision/blob/range_image.h"
5#include "aos/vision/debug/debug_viewer.h"
6#include "aos/vision/image/image_types.h"
7
8#include <memory>
9
10namespace aos {
11namespace vision {
12
Parker Schuhd7db83d2017-02-08 20:49:15 -080013class BlobStreamViewer : public DebugViewer {
Parker Schuh6691f192017-01-14 17:01:02 -080014 public:
Parker Schuhd7db83d2017-02-08 20:49:15 -080015 BlobStreamViewer() : DebugViewer(false) {}
16 explicit BlobStreamViewer(bool flip) : DebugViewer(flip) {}
17
Parker Schuh6691f192017-01-14 17:01:02 -080018 void Submit(ImageFormat fmt, const BlobList &blob_list) {
19 SetFormatAndClear(fmt);
20 DrawBlobList(blob_list, {255, 255, 255});
21 }
22
23 inline void SetFormatAndClear(ImageFormat fmt) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080024 if (!image_.fmt().Equals(fmt)) {
Parker Schuh6691f192017-01-14 17:01:02 -080025 printf("resizing data: %d, %d\n", fmt.w, fmt.h);
Parker Schuhd7db83d2017-02-08 20:49:15 -080026 image_ = ImageValue(fmt);
27 UpdateImage(image_.get());
Parker Schuh6691f192017-01-14 17:01:02 -080028 }
Parker Schuhd7db83d2017-02-08 20:49:15 -080029 memset(image_.data(), 0, fmt.ImgSize() * sizeof(PixelRef));
Parker Schuh6691f192017-01-14 17:01:02 -080030 }
31
32 inline void DrawBlobList(const BlobList &blob_list, PixelRef color) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080033 ImagePtr ptr = img();
34 for (const RangeImage &blob : blob_list) {
35 for (int i = 0; i < (int)blob.ranges().size(); ++i) {
36 for (const auto &range : blob.ranges()[i]) {
Parker Schuh6691f192017-01-14 17:01:02 -080037 for (int j = range.st; j < range.ed; ++j) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080038 ptr.get_px(j, i + blob.min_y()) = color;
Parker Schuh6691f192017-01-14 17:01:02 -080039 }
40 }
41 }
42 }
43 }
44
45 inline void DrawSecondBlobList(const BlobList &blob_list, PixelRef color1,
46 PixelRef color2) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080047 ImagePtr ptr = img();
Parker Schuh6691f192017-01-14 17:01:02 -080048 for (const auto &blob : blob_list) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080049 for (int i = 0; i < (int)blob.ranges().size(); ++i) {
50 for (const auto &range : blob.ranges()[i]) {
Parker Schuh6691f192017-01-14 17:01:02 -080051 for (int j = range.st; j < range.ed; ++j) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080052 auto px = ptr.get_px(j, i + blob.min_y());
Parker Schuh6691f192017-01-14 17:01:02 -080053 if (px.r == 0 && px.g == 0 && px.b == 0) {
Parker Schuhd7db83d2017-02-08 20:49:15 -080054 ptr.get_px(j, i + blob.min_y()) = color1;
Parker Schuh6691f192017-01-14 17:01:02 -080055 } else {
Parker Schuhd7db83d2017-02-08 20:49:15 -080056 ptr.get_px(j, i + blob.min_y()) = color2;
Parker Schuh6691f192017-01-14 17:01:02 -080057 }
58 }
59 }
60 }
61 }
62 }
63
Parker Schuh0ff777c2017-02-19 15:01:13 -080064 inline void DrawSecondBlobList(const BlobList &blob_list, PixelRef color1,
65 PixelRef color2, PixelRef prev_color) {
66 ImagePtr ptr = img();
67 for (const auto &blob : blob_list) {
68 for (int i = 0; i < (int)blob.ranges().size(); ++i) {
69 for (const auto &range : blob.ranges()[i]) {
70 for (int j = range.st; j < range.ed; ++j) {
71 auto px = ptr.get_px(j, i + blob.min_y());
72 if (px.r == prev_color.r && px.g == prev_color.g &&
73 px.b == prev_color.b) {
74 ptr.get_px(j, i + blob.min_y()) = color2;
75 } else {
76 ptr.get_px(j, i + blob.min_y()) = color1;
77 }
78 }
79 }
80 }
81 }
82 }
83
Parker Schuhd7db83d2017-02-08 20:49:15 -080084 // Backwards compatible.
85 DebugViewer *view() { return this; }
86
87 ImagePtr img() { return image_.get(); }
Parker Schuh6691f192017-01-14 17:01:02 -080088
89 private:
Parker Schuhd7db83d2017-02-08 20:49:15 -080090 ImageValue image_;
Parker Schuh6691f192017-01-14 17:01:02 -080091};
92
93} // namespace vision
94} // namespace aos
95
96#endif // _AOS_VISION_BLOB_STREAM_VIEW_H_