26#include <opencv2/dnn.hpp>
39 segtype::freeze(doit);
46 for (
size_t i = 0; i < 256; ++i)
48 uint32_t & c = itsColor[i]; c = 0;
51 for (
int shift = 7; shift >= 0; --shift)
53 for (
int channel = 0; channel < 3; ++channel) c |= ((ind >> channel) & 1) << (shift + 8 * (3-channel));
63 int const bgclass = bgid::get();
64 uint32_t
const alph = alpha::get() << 24;
65 cv::MatSize
const rs = results.size;
66 T
const * r =
reinterpret_cast<T
const *
>(results.data);
67 T
const thresh(cthresh::get() * 0.01F);
69 switch (segtype::get())
72 case jevois::dnn::postprocessor::SegType::ClassesHWC:
76 if (rs.dims() != 4 || rs[0] != 1)
LTHROW(
"Need 1xHxWxC for C classes");
77 int const numclass = rs[3];
int const siz = rs[1] * rs[2] * numclass;
80 itsOverlay = cv::Mat(rs[1], rs[2], CV_8UC4);
81 uint32_t * im =
reinterpret_cast<uint32_t *
>(itsOverlay.data);
83 for (
int i = 0; i < siz; i += numclass)
85 int maxc = -1; T maxval = thresh;
86 for (
int c = 0; c < numclass; ++c)
89 if (v > maxval) { maxval = v; maxc = c; }
93 if (maxc < 0 || maxc > 255 || maxc == bgclass) *im++ = 0;
else *im++ = itsColor[maxc] | alph;
99 case jevois::dnn::postprocessor::SegType::ClassesCHW:
103 if (rs.dims() != 4 || rs[0] != 1)
LTHROW(
"Need 1xCxHxW for C classes");
104 int const numclass = rs[1];
int const hw = rs[2] * rs[3];
107 itsOverlay = cv::Mat(rs[2], rs[3], CV_8UC4);
108 uint32_t * im =
reinterpret_cast<uint32_t *
>(itsOverlay.data);
110 for (
int i = 0; i < hw; ++i)
112 int maxc = -1; T maxval = thresh;
113 for (
int c = 0; c < numclass; ++c)
115 T v = results.at<T>(i + c * hw);
116 if (v > maxval) { maxval = v; maxc = c; }
120 if (maxc < 0 || maxc > 255 || maxc == bgclass) *im++ = 0;
else *im++ = itsColor[maxc] | alph;
126 case jevois::dnn::postprocessor::SegType::ArgMax:
129 if (rs.dims() != 2 && (rs.dims() != 3 || rs[0] != 1) && (rs.dims() != 4 || rs[0] != 1 || rs[3] != 1))
130 LTHROW(
"Need shape HxW, 1xHxW, or 1xHxWx1 with class ID in each pixel");
131 int const siz = rs[1] * rs[2];
134 itsOverlay = cv::Mat(rs[1], rs[2], CV_8UC4);
135 uint32_t * im =
reinterpret_cast<uint32_t *
>(itsOverlay.data);
137 for (
int i = 0; i < siz; ++i)
140 int32_t
const id = *r++;
141 if (id < 0 || id > 255 ||
id == bgclass) *im++ = 0;
else *im++ = itsColor[id] | alph;
153 if (outs.size() != 1)
LTHROW(
"Need exactly one output blob");
156 if (bgid::get() != 0) itsColor[0] = 0xff0000;
else itsColor[0] = 0;
159 cv::Mat
const & results = outs[0];
161 switch (results.type())
163 case CV_8UC1: process<uint8_t>(results);
break;
164 case CV_16UC1: process<uint16_t>(results);
break;
165 case CV_32FC1: process<float>(results);
break;
166 case CV_32SC1: process<int32_t>(results);
break;
172 catch (std::exception
const & e)
174 std::string err =
"Selected segtype is " + segtype::strget() +
" and network produced:\n\n";
176 err +=
"\nFATAL ERROR(s):\n\n";
193 if (itsOverlay.empty())
return;
206 ImVec2 tl = helper->
i2d(itsTLx, itsTLy), br = helper->
i2d(itsBRx, itsBRy);
207 int dtlx = tl.x, dtly = tl.y;
208 unsigned short dw = br.x - tl.x, dh = br.y - tl.y;
211 helper->
drawImage(
"ppsr", itsOverlay,
true, dtlx, dtly, dw, dh,
false ,
true );
Helper class to assist modules in creating graphical and GUI elements.
void releaseImage(char const *name)
Release an image.
ImVec2 i2d(ImVec2 p, char const *name=nullptr)
Convert coordinates of a point from within a rendered image to on-screen.
void drawImage(char const *name, RawImage const &img, int &x, int &y, unsigned short &w, unsigned short &h, bool noalias=false, bool isoverlay=false)
Draw a RawImage, copying pixel data to an OpenGL texture.
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Base class for a module that supports standardized serial messages.
virtual ~PostProcessorSegment()
Destructor.
void process(std::vector< cv::Mat > const &outs, PreProcessor *preproc) override
Process outputs and draw/send some results.
void report(jevois::StdModule *mod, jevois::RawImage *outimg=nullptr, jevois::OptGUIhelper *helper=nullptr, bool overlay=true, bool idle=false) override
Report what happened in last process() to console/output video/GUI.
void freeze(bool doit) override
Freeze/unfreeze parameters that users should not change while running.
void postInit() override
Create colormap in postInit()
Pre-Processor for neural network pipeline.
cv::Rect getUnscaledCropRect(size_t blobnum=0)
Get unscaled crop rectangle in image coordinates.
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
std::string shapestr(cv::Mat const &m)
Get a string of the form: "nD AxBxC... TYPE" from an n-dimensional cv::Mat with data type TYPE.