JeVois  1.22
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
OutputFrame.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
19
22#include <jevois/Util/Utils.H>
23#include <opencv2/imgproc/imgproc.hpp>
24
25// ####################################################################################################
26jevois::OutputFrame::OutputFrame(std::shared_ptr<jevois::VideoOutput> const & gad, jevois::RawImage * excimg) :
27 itsGadget(gad), itsDidGet(false), itsDidSend(false), itsImagePtrForException(excimg)
28{ }
29
30// ####################################################################################################
32{
33 // If itsGadget is invalidated, we have been moved to another object, so do not do anything here:
34 if (itsGadget.get() == nullptr) return;
35
36 // If we did not get(), just end now:
37 if (itsDidGet == false) return;
38
39 // If Engine gave us a non-zero image pointer for exceptions, and we did get(), pass down the buffer we did get, so
40 // that Engine can write exception text into it, unless it is too late (exception occurred after send()) or too early
41 // (before get()), in which case Engine will get its own buffer:
42 if (itsImagePtrForException)
43 {
44 if (itsDidSend == false) { *itsImagePtrForException = itsImage; }
45 // Engine will be responsible for the final send()
46 }
47 else
48 {
49 // If we did get() but not send(), send now (the image will likely contain garbage):
50 if (itsDidSend == false) try { itsGadget->send(itsImage); } catch (...) { }
51 }
52}
53
54// ####################################################################################################
56{
57 itsGadget->get(itsImage);
58 itsDidGet = true;
59 return itsImage;
60}
61
62// ####################################################################################################
64{
65 itsGadget->send(itsImage);
66 itsDidSend = true;
67 if (itsImagePtrForException) itsImagePtrForException->invalidate();
68}
69
70// ####################################################################################################
71void jevois::OutputFrame::sendCv(cv::Mat const & img, int quality) const
72{
73 switch(img.type())
74 {
75 case CV_8UC3: sendScaledCvBGR(img, quality); break;
76 case CV_8UC1: sendScaledCvGRAY(img, quality); break;
77 case CV_8UC4: sendScaledCvRGBA(img, quality); break;
78 default: LFATAL("cv::Mat of type " << cvtypestr(img.type()) << " not supported.");
79 }
80}
81
82// ####################################################################################################
83void jevois::OutputFrame::sendCvGRAY(cv::Mat const & img, int quality) const
84{
85 jevois::RawImage rawimg = get();
87 send();
88}
89
90// ####################################################################################################
91void jevois::OutputFrame::sendCvBGR(cv::Mat const & img, int quality) const
92{
93 jevois::RawImage rawimg = get();
94 jevois::rawimage::convertCvBGRtoRawImage(img, rawimg, quality);
95 send();
96}
97// ####################################################################################################
98void jevois::OutputFrame::sendCvRGB(cv::Mat const & img, int quality) const
99{
100 jevois::RawImage rawimg = get();
101 jevois::rawimage::convertCvRGBtoRawImage(img, rawimg, quality);
102 send();
103}
104
105// ####################################################################################################
106void jevois::OutputFrame::sendCvRGBA(cv::Mat const & img, int quality) const
107{
108 jevois::RawImage rawimg = get();
109 jevois::rawimage::convertCvRGBAtoRawImage(img, rawimg, quality);
110 send();
111}
112
113// ####################################################################################################
114void jevois::OutputFrame::sendScaledCvGRAY(cv::Mat const & img, int quality) const
115{
116 jevois::RawImage rawimg = get();
118 rawimg, quality);
119 send();
120}
121
122// ####################################################################################################
123void jevois::OutputFrame::sendScaledCvBGR(cv::Mat const & img, int quality) const
124{
125 jevois::RawImage rawimg = get();
127 rawimg, quality);
128 send();
129}
130// ####################################################################################################
131void jevois::OutputFrame::sendScaledCvRGB(cv::Mat const & img, int quality) const
132{
133 jevois::RawImage rawimg = get();
135 rawimg, quality);
136 send();
137}
138
139// ####################################################################################################
140void jevois::OutputFrame::sendScaledCvRGBA(cv::Mat const & img, int quality) const
141{
142 jevois::RawImage rawimg = get();
144 rawimg, quality);
145 send();
146}
147
RawImage const & get() const
Get a pre-allocated image so that we can fill the pixel data and later send out over USB using send()
Definition OutputFrame.C:55
OutputFrame(OutputFrame &&other)=default
Move constructor.
void sendScaledCvGRAY(cv::Mat const &img, int quality=75) const
Shorthand to send a GRAY cv::Mat after converting it to the current output format.
void sendCvBGR(cv::Mat const &img, int quality=75) const
Shorthand to send a BGR cv::Mat after converting it to the current output format.
Definition OutputFrame.C:91
void sendScaledCvRGBA(cv::Mat const &img, int quality=75) const
Shorthand to send a RGBA cv::Mat after converting it to the current output format.
void sendScaledCvRGB(cv::Mat const &img, int quality=75) const
Shorthand to send a RGB cv::Mat after converting it to the current output format.
void sendCv(cv::Mat const &img, int quality=75) const
Shorthand to send a cv::Mat after converting / scaling it to the current output format.
Definition OutputFrame.C:71
void sendCvGRAY(cv::Mat const &img, int quality=75) const
Shorthand to send a GRAY cv::Mat after converting it to the current output format.
Definition OutputFrame.C:83
~OutputFrame()
Destructor, returns the buffers to the driver as needed.
Definition OutputFrame.C:31
void sendScaledCvBGR(cv::Mat const &img, int quality=75) const
Shorthand to send a BGR cv::Mat after converting it to the current output format.
void sendCvRGB(cv::Mat const &img, int quality=75) const
Shorthand to send a RGB cv::Mat after converting it to the current output format.
Definition OutputFrame.C:98
void sendCvRGBA(cv::Mat const &img, int quality=75) const
Shorthand to send a RGBA cv::Mat after converting it to the current output format.
void send() const
Send an image out over USB to the host computer.
Definition OutputFrame.C:63
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition RawImage.H:111
void invalidate()
Invalidate the image by zero'ing out the pointer to pixel buffer and the dims and format.
Definition RawImage.C:42
unsigned int width
Image width in pixels.
Definition RawImage.H:145
unsigned int height
Image height in pixels.
Definition RawImage.H:146
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Definition Log.H:230
void convertCvRGBAtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert an RGBA cv::Mat to RawImage with already-allocated pixels and pixel type.
cv::Mat rescaleCv(cv::Mat const &img, cv::Size const &newdims)
Rescale an OpenCV image, choosing the right kind of interpolation.
void convertCvBGRtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert a BGR cv::Mat to RawImage with already-allocated pixels and pixel type.
void convertCvRGBtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert a RGB cv::Mat to RawImage with already-allocated pixels and pixel type.
void convertCvGRAYtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert a Gray cv::Mat to RawImage with already-allocated pixels and pixel type.
std::string cvtypestr(unsigned int cvtype)
Convert cv::Mat::type() code to to a string (e.g., CV_8UC1, CV_32SC3, etc)
Definition Utils.C:58