JeVois  1.21
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
RawImageOps.H
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
18#pragma once
19
21
22#include <opencv2/core/core.hpp>
23
24namespace jevois
25{
26 //! Functions for RawImage conversion, processing, drawing
27 namespace rawimage
28 {
29 //! Create an OpenCV image from the existing RawImage data, sharing the pixel memory rather than copying it
30 /*! The pixel data is not copied, just pointed to, and no pixel format conversion is done. You should destroy the
31 cv::Mat before the RawImage to avoid that your cv::Mat ends up with de-allocated pixel memory. \ingroup image */
32 cv::Mat cvImage(RawImage const & src);
33
34 //! Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV gray byte
35 /*! Supported RawImage pixel formats:
36
37 - V4L2_PIX_FMT_YUYV
38 - V4L2_PIX_FMT_GREY
39 - V4L2_PIX_FMT_SRGGB8 (Bayer)
40 - V4L2_PIX_FMT_RGB565
41 - V4L2_PIX_FMT_BGR24
42
43 \ingroup image */
44 cv::Mat convertToCvGray(RawImage const & src);
45
46 //! Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV BGR byte
47 /*! For historical reasons, BGR is the "native" color format of OpenCV, check whether your algorithm needs RGB or
48 BGR and use the appropriate conversion for it.
49
50 Supported RawImage pixel formats:
51
52 - V4L2_PIX_FMT_YUYV
53 - V4L2_PIX_FMT_GREY
54 - V4L2_PIX_FMT_SRGGB8 (Bayer)
55 - V4L2_PIX_FMT_RGB565
56 - V4L2_PIX_FMT_BGR24
57
58 \ingroup image */
59 cv::Mat convertToCvBGR(RawImage const & src);
60
61 //! Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV RGB byte
62 /*! For historical reasons, BGR is the "native" color format of OpenCV, not RGB as created here, check whether your
63 algorithm needs RGB or BGR and use the appropriate conversion for it.
64
65 Supported RawImage pixel formats:
66 - V4L2_PIX_FMT_YUYV
67 - V4L2_PIX_FMT_GREY
68 - V4L2_PIX_FMT_SRGGB8 (Bayer)
69 - V4L2_PIX_FMT_RGB565
70 - V4L2_PIX_FMT_BGR24
71
72 \ingroup image */
73 cv::Mat convertToCvRGB(RawImage const & src);
74
75 //! Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV RGB-A byte
76 /*! RGBA is seldom used by OpenCV itself, but is useful for many NEON and OpenGL (GPU) algorithms. For these
77 algorithms, we here just use cv::Mat as a convenient container for raw pixel data.
78
79 Supported RawImage pixel formats:
80 - V4L2_PIX_FMT_YUYV
81 - V4L2_PIX_FMT_GREY
82 - V4L2_PIX_FMT_SRGGB8 (Bayer)
83 - V4L2_PIX_FMT_RGB565
84 - V4L2_PIX_FMT_BGR24
85
86 \ingroup image */
87 cv::Mat convertToCvRGBA(RawImage const & src);
88
89 //! Swap pairs of bytes in a RawImage
90 /*! This should never be needed, except maybe for RGB565 images, mainly for internal debugging, or to directly pass
91 an RGB565 camera image (which is big endian) to USB (which assumes little endian when using RGBP UVC
92 format). This function is NEON-accelerated when compiled for the platform hardware. \ingroup image */
93 void byteSwap(RawImage & img);
94
95 //! Paste an image within another of same pixel type
96 /*! To keep this function fast, throws if the source image does not fully fit within the destination image, or if
97 the two images do not have the same pixel format. \ingroup image */
98 void paste(RawImage const & src, RawImage & dest, int dx, int dy);
99
100 //! Paste a grey byte image into a YUYV image
101 /*! \ingroup image */
102 void pasteGreyToYUYV(cv::Mat const & src, RawImage & dest, int dx, int dy);
103
104 //! Paste a BGR byte image into a YUYV image
105 /*! \ingroup image */
106 void pasteBGRtoYUYV(cv::Mat const & src, RawImage & dst, int dx, int dy);
107
108 //! Paste a RGB byte image into a YUYV image
109 /*! \ingroup image */
110 void pasteRGBtoYUYV(cv::Mat const & src, RawImage & dst, int dx, int dy);
111
112 //! Paste an ROI from an image to within another of same pixel type
113 /*! To keep this function fast, throws if the source image does not fully fit within the destination image or if the
114 pixel formats differ. \ingroup image */
115 void roipaste(RawImage const & src, int x, int y, unsigned int w, unsigned int h, RawImage & dest, int dx, int dy);
116
117 //! Draw a disk in a YUYV image
118 /*! \ingroup image */
119 void drawDisk(RawImage & img, int x, int y, unsigned int rad, unsigned int col);
120
121 //! Draw a circle in a YUYV image
122 /*! \ingroup image */
123 void drawCircle(RawImage & img, int x, int y, unsigned int rad, unsigned int thick, unsigned int col);
124
125 //! Clip a line to fit inside a viewport [wxmin...wxmax[ x [wymin...wymax[
126 /*! This uses the Liang-Barsky line clipping algorithm. Returns false if the line is entirely outside the viewport
127 (nothing to draw). \ingroup image */
128 bool clipLine(int wxmin, int wymin, int wxmax, int wymax, int & x1, int & y1, int & x2, int & y2);
129
130 //! Draw a line in a YUYV image
131 /*! \ingroup image */
132 void drawLine(RawImage & img, int x1, int y1, int x2, int y2, unsigned int thick, unsigned int col);
133
134 //! Draw a rectangle in a YUYV image
135 /*! The pixels drawn go from (x,y) included to (x+w-1,y+h-1) included. If w or h is 0, it is replaced by 1.
136 \ingroup image */
137 void drawRect(RawImage & img, int x, int y, unsigned int w, unsigned int h, unsigned int thick, unsigned int col);
138
139 //! Draw a rectangle in a YUYV image
140 /*! This is really not flexible and is optimized for speed only. col will be used for both YU and YV pixels. For
141 more demanding drawings, use openCV functions. The pixels drawn go from (x,y) included to (x+w-1,y+h-1)
142 included. If w or h is 0, it is replaced by 1. \ingroup image */
143 void drawRect(RawImage & img, int x, int y, unsigned int w, unsigned int h, unsigned int col);
144
145 //! Draw a filled rectangle in a YUYV image
146 /*! This is really not flexible and is optimized for speed only. col will be used for both YU and YV pixels. For
147 more demanding drawings, use openCV functions. The pixels drawn go from (x,y) included to (x+w-1,y+h-1)
148 included. If w or h is 0, it is replaced by 1. \ingroup image */
149 void drawFilledRect(RawImage & img, int x, int y, unsigned int w, unsigned int h, unsigned int col);
150
151 //! Available fonts for writeText()
152 /*! \ingroup image */
168
169 //! Write some text in an image
170 /*! \ingroup image */
171 void writeText(RawImage & img, std::string const & txt, int x, int y, unsigned int col, Font font = Font6x10);
172
173 //! Write some text in an image
174 /*! \ingroup image */
175 void writeText(RawImage & img, char const * txt, int x, int y, unsigned int col, Font font = Font6x10);
176
177 //! Shorthand to write some text in an image, with x = 3 and return the next y position
178 /*! Used to show several lines of text messages on top of a video image. The given y position is initialized to 3 if
179 < 3; then the returned value is the y that should be used for the next line. \ingroup image */
180 int itext(RawImage & img, std::string const & txt, int y = 3, unsigned int col = jevois::yuyv::White,
181 Font font = Font6x10);
182
183 //! Shorthand to write some text in an image, with x = 3 and return the next y position
184 /*! Used to show several lines of text messages on top of a video image. The given y position is initialized to 3 if
185 < 3; then the returned value is the y that should be used for the next line. \ingroup image */
186 int itext(RawImage & img, char const * txt, int y = 3, unsigned int col = jevois::yuyv::White,
187 Font font = Font6x10);
188
189 //! Convert a BGR cv::Mat to RawImage with already-allocated pixels and pixel type
190 /*! This is used to output video frames over USB from internally computed results in opencv BGR format. dst should
191 already have an allocated pixel buffer (typically, it should be obtained from the USB gadget), and its size
192 must match that of the src image.
193
194 Supported dst pixel formats:
195 - V4L2_PIX_FMT_YUYV
196 - V4L2_PIX_FMT_GREY
197 - V4L2_PIX_FMT_SRGGB8 (Bayer)
198 - V4L2_PIX_FMT_RGB565
199 - V4L2_PIX_FMT_MJPG
200 - V4L2_PIX_FMT_BGR24
201
202 quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
203 void convertCvBGRtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
204
205 //! Convert a RGB cv::Mat to RawImage with already-allocated pixels and pixel type
206 /*! This is used to output video frames over USB from internally computed results in opencv BGR format. dst should
207 already have an allocated pixel buffer (typically, it should be obtained from the USB gadget), and its size
208 must match that of the src image.
209
210 Supported dst pixel formats:
211 - V4L2_PIX_FMT_YUYV
212 - V4L2_PIX_FMT_GREY
213 - V4L2_PIX_FMT_SRGGB8 (Bayer)
214 - V4L2_PIX_FMT_RGB565
215 - V4L2_PIX_FMT_MJPG
216 - V4L2_PIX_FMT_BGR24
217
218 quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
219 void convertCvRGBtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
220
221 //! Convert an RGBA cv::Mat to RawImage with already-allocated pixels and pixel type
222 /*! This is used to output video frames over USB from internally computed results in opencv RGB-A format (which is
223 often the format of choice for NEON or OpenGL processing). dst should already have an allocated pixel buffer
224 (typically, it should be obtained from the USB gadget), and its size must match that of the src image.
225
226 Supported dst pixel formats:
227 - V4L2_PIX_FMT_YUYV
228 - V4L2_PIX_FMT_GREY
229 - V4L2_PIX_FMT_SRGGB8 (Bayer)
230 - V4L2_PIX_FMT_RGB565
231 - V4L2_PIX_FMT_MJPG
232 - V4L2_PIX_FMT_BGR24
233
234 quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
235 void convertCvRGBAtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
236
237 //! Convert a Gray cv::Mat to RawImage with already-allocated pixels and pixel type
238 /*! This is used to output video frames over USB from internally computed results in opencv Gray format. dst should
239 already have an allocated pixel buffer (typically, it should be obtained from the USB gadget), and its size
240 must match that of the src image.
241
242 Supported dst pixel formats:
243 - V4L2_PIX_FMT_YUYV
244 - V4L2_PIX_FMT_GREY
245 - V4L2_PIX_FMT_SRGGB8 (Bayer)
246 - V4L2_PIX_FMT_RGB565
247 - V4L2_PIX_FMT_MJPG
248 - V4L2_PIX_FMT_BGR24
249
250 quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
251 void convertCvGRAYtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
252
253 //! Split an RGBA cv::Mat into a 4x taller grey RawImage with already-allocated pixels
254 /*! This is used to run GPU shaders that apply different filters to an image and store their results separately into
255 the R, G, B and A components of an RGBA output image. Here, we just unpack the 4 components and stuff them into
256 the output image one above the other: R on top, then G below it, then B below it, then A below it. The
257 destination RawImage must be at least as wide as src and at least 4 times as tall as src, and must have
258 V4L2_PIX_FMT_GREY pixels. \ingroup image */
259 void unpackCvRGBAtoGrayRawImage(cv::Mat const & src, RawImage & dst);
260
261 //! Flip a YUYV RawImage horizontally while preserving color information
262 /*! This function is to allow one to use JeVois with the PhotoBooth app on a Mac, whih flips the image horizontally
263 (about the vertical axis). Only YUYV pixels are supported. You might be able to use cv::flip() from OpenCV
264 instead with other pixel types. \ingroup image */
265 void hFlipYUYV(RawImage & img);
266
267 //! OpenCV does not provide conversion from RGB to YUYV in cvtColor(), so this function provides it
268 /*! \ingroup image */
269 void convertCvRGBtoCvYUYV(cv::Mat const & src, cv::Mat & dst);
270
271 //! OpenCV does not provide conversion from BGR to YUYV in cvtColor(), so this function provides it
272 /*! \ingroup image */
273 void convertCvBGRtoCvYUYV(cv::Mat const & src, cv::Mat & dst);
274
275 //! OpenCV does not provide conversion from GRAY to YUYV in cvtColor(), so this function provides it
276 /*! \ingroup image */
277 void convertCvGRAYtoCvYUYV(cv::Mat const & src, cv::Mat & dst);
278
279 //! OpenCV does not provide conversion from RGBA to YUYV in cvtColor(), so this function provides it
280 /*! \ingroup image */
281 void convertCvRGBAtoCvYUYV(cv::Mat const & src, cv::Mat & dst);
282
283 //! Convert from Bayer to YUYV, only used internally by Camera class
284 /*! Both src and dst should already have allocated buffers of correct sizes. */
285 void convertBayerToYUYV(RawImage const & src, RawImage & dst);
286
287 //! Convert from Grey (monochrome) to YUYV, only used internally by Camera class
288 /*! Both src and dst should already have allocated buffers of correct sizes. */
289 void convertGreyToYUYV(RawImage const & src, RawImage & dst);
290
291 } // namespace rawimage
292
293 //! Rescale an OpenCV image, choosing the right kind of interpolation
294 /*! \ingroup image */
295 cv::Mat rescaleCv(cv::Mat const & img, cv::Size const & newdims);
296
297} // namespace jevois
298
int h
Definition GUIhelper.C:2491
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition RawImage.H:111
cv::Mat convertToCvRGBA(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV RGB-A byte...
void paste(RawImage const &src, RawImage &dest, int dx, int dy)
Paste an image within another of same pixel type.
bool clipLine(int wxmin, int wymin, int wxmax, int wymax, int &x1, int &y1, int &x2, int &y2)
Clip a line to fit inside a viewport [wxmin...wxmax[ x [wymin...wymax[.
void convertCvGRAYtoCvYUYV(cv::Mat const &src, cv::Mat &dst)
OpenCV does not provide conversion from GRAY to YUYV in cvtColor(), so this function provides it.
cv::Mat cvImage(RawImage const &src)
Create an OpenCV image from the existing RawImage data, sharing the pixel memory rather than copying ...
Definition RawImageOps.C:31
void writeText(RawImage &img, std::string const &txt, int x, int y, unsigned int col, Font font=Font6x10)
Write some text in an image.
cv::Mat convertToCvGray(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV gray byte.
void convertCvBGRtoCvYUYV(cv::Mat const &src, cv::Mat &dst)
OpenCV does not provide conversion from BGR to YUYV in cvtColor(), so this function provides it.
void pasteRGBtoYUYV(cv::Mat const &src, RawImage &dst, int dx, int dy)
Paste a RGB byte image into a YUYV image.
cv::Mat convertToCvRGB(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV RGB byte.
void drawFilledRect(RawImage &img, int x, int y, unsigned int w, unsigned int h, unsigned int col)
Draw a filled rectangle in a YUYV image.
void drawDisk(RawImage &img, int x, int y, unsigned int rad, unsigned int col)
Draw a disk in a YUYV image.
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.
Font
Available fonts for writeText()
cv::Mat convertToCvBGR(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV BGR byte.
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 drawLine(RawImage &img, int x1, int y1, int x2, int y2, unsigned int thick, unsigned int col)
Draw a line in a YUYV image.
void convertCvRGBtoCvYUYV(cv::Mat const &src, cv::Mat &dst)
OpenCV does not provide conversion from RGB to YUYV in cvtColor(), so this function provides it.
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 unpackCvRGBAtoGrayRawImage(cv::Mat const &src, RawImage &dst)
Split an RGBA cv::Mat into a 4x taller grey RawImage with already-allocated pixels.
void drawRect(RawImage &img, int x, int y, unsigned int w, unsigned int h, unsigned int thick, unsigned int col)
Draw a rectangle in a YUYV image.
void hFlipYUYV(RawImage &img)
Flip a YUYV RawImage horizontally while preserving color information.
void roipaste(RawImage const &src, int x, int y, unsigned int w, unsigned int h, RawImage &dest, int dx, int dy)
Paste an ROI from an image to within another of same pixel type.
void byteSwap(RawImage &img)
Swap pairs of bytes in a RawImage.
void convertCvRGBAtoCvYUYV(cv::Mat const &src, cv::Mat &dst)
OpenCV does not provide conversion from RGBA to YUYV in cvtColor(), so this function provides it.
void convertCvGRAYtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert a Gray cv::Mat to RawImage with already-allocated pixels and pixel type.
int itext(RawImage &img, std::string const &txt, int y=3, unsigned int col=jevois::yuyv::White, Font font=Font6x10)
Shorthand to write some text in an image, with x = 3 and return the next y position.
void drawCircle(RawImage &img, int x, int y, unsigned int rad, unsigned int thick, unsigned int col)
Draw a circle in a YUYV image.
void pasteGreyToYUYV(cv::Mat const &src, RawImage &dest, int dx, int dy)
Paste a grey byte image into a YUYV image.
void pasteBGRtoYUYV(cv::Mat const &src, RawImage &dst, int dx, int dy)
Paste a BGR byte image into a YUYV image.
void convertBayerToYUYV(RawImage const &src, RawImage &dst)
Convert from Bayer to YUYV, only used internally by Camera class.
void convertGreyToYUYV(RawImage const &src, RawImage &dst)
Convert from Grey (monochrome) to YUYV, only used internally by Camera class.
unsigned short constexpr White
YUYV color value.
Definition RawImage.H:59
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2