JeVois  1.6
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
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 
20 #include <jevois/Image/RawImage.H>
21 
22 #include <opencv2/core/core.hpp>
23 
24 namespace jevois
25 {
26  namespace rawimage
27  {
28  //! Create an OpenCV image from the existing RawImage data, sharing the pixel memory rather than copying it
29  /*! The pixel data is not copied, just pointed to, and no pixel format conversion is done. You should destroy the
30  cv::Mat before the RawImage to avoid that your cv::Mat ends up with de-allocated pixel memory. \ingroup image */
31  cv::Mat cvImage(RawImage const & src);
32 
33  //! Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV gray byte
34  /*! Supported RawImage pixel formats:
35 
36  - V4L2_PIX_FMT_YUYV
37  - V4L2_PIX_FMT_GREY
38  - V4L2_PIX_FMT_SRGGB8 (Bayer)
39  - V4L2_PIX_FMT_RGB565
40  - V4L2_PIX_FMT_BGR24
41 
42  \ingroup image */
43  cv::Mat convertToCvGray(RawImage const & src);
44 
45  //! Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV BGR byte
46  /*! For historical reasons, BGR is the "native" color format of OpenCV, check whether your algorithm needs RGB or
47  BGR and use the appropriate conversion for it.
48 
49  Supported RawImage pixel formats:
50 
51  - V4L2_PIX_FMT_YUYV
52  - V4L2_PIX_FMT_GREY
53  - V4L2_PIX_FMT_SRGGB8 (Bayer)
54  - V4L2_PIX_FMT_RGB565
55  - V4L2_PIX_FMT_BGR24
56 
57  \ingroup image */
58  cv::Mat convertToCvBGR(RawImage const & src);
59 
60  //! Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV RGB byte
61  /*! For historical reasons, BGR is the "native" color format of OpenCV, not RGB as created here, check whether your
62  algorithm needs RGB or BGR and use the appropriate conversion for it.
63 
64  Supported RawImage pixel formats:
65  - V4L2_PIX_FMT_YUYV
66  - V4L2_PIX_FMT_GREY
67  - V4L2_PIX_FMT_SRGGB8 (Bayer)
68  - V4L2_PIX_FMT_RGB565
69  - V4L2_PIX_FMT_BGR24
70 
71  \ingroup image */
72  cv::Mat convertToCvRGB(RawImage const & src);
73 
74  //! Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV RGB-A byte
75  /*! RGBA is seldom used by OpenCV itself, but is useful for many NEON and OpenGL (GPU) algorithms. For these
76  algorithms, we here just use cv::Mat as a convenient container for raw pixel data.
77 
78  Supported RawImage pixel formats:
79  - V4L2_PIX_FMT_YUYV
80  - V4L2_PIX_FMT_GREY
81  - V4L2_PIX_FMT_SRGGB8 (Bayer)
82  - V4L2_PIX_FMT_RGB565
83  - V4L2_PIX_FMT_BGR24
84 
85  \ingroup image */
86  cv::Mat convertToCvRGBA(RawImage const & src);
87 
88  //! Swap pairs of bytes in a RawImage
89  /*! This should never be needed, except maybe for RGB565 images, mainly for internal debugging, or to directly pass
90  an RGB565 camera image (which is big endian) to USB (which assumes little endian when using RGBP UVC
91  format). This function is NEON-accelerated when compiled for the platform hardware. \ingroup image */
92  void byteSwap(RawImage & img);
93 
94  //! Paste an image within another of same pixel type
95  /*! To keep this function fast, throws if the source image does not fully fit within the destination image, or if
96  the two images do not have the same pixel format. \ingroup image */
97  void paste(RawImage const & src, RawImage & dest, int dx, int dy);
98 
99  //! Paste a grey byte image into a YUYV image
100  /*! \ingroup image */
101  void pasteGreyToYUYV(cv::Mat const & src, RawImage & dest, int dx, int dy);
102 
103  //! Paste a BGR byte image into a YUYV image
104  /*! \ingroup image */
105  void pasteBGRtoYUYV(cv::Mat const & src, RawImage & dst, int dx, int dy);
106 
107  //! Paste a RGB byte image into a YUYV image
108  /*! \ingroup image */
109  void pasteRGBtoYUYV(cv::Mat const & src, RawImage & dst, int dx, int dy);
110 
111  //! Paste an ROI from an image to within another of same pixel type
112  /*! To keep this function fast, throws if the source image does not fully fit within the destination image or if the
113  pixel formats differ. \ingroup image */
114  void roipaste(RawImage const & src, int x, int y, unsigned int w, unsigned int h, RawImage & dest, int dx, int dy);
115 
116  //! Draw a disk in a YUYV image
117  /*! \ingroup image */
118  void drawDisk(RawImage & img, int x, int y, unsigned int rad, unsigned int col);
119 
120  //! Draw a circle in a YUYV image
121  /*! \ingroup image */
122  void drawCircle(RawImage & img, int x, int y, unsigned int rad, unsigned int thick, unsigned int col);
123 
124  //! Draw a line in a YUYV image
125  /*! \ingroup image */
126  void drawLine(RawImage & img, int x1, int y1, int x2, int y2, unsigned int thick, unsigned int col);
127 
128  //! Draw a rectangle in a YUYV image
129  /*! 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.
130  \ingroup image */
131  void drawRect(RawImage & img, int x, int y, unsigned int w, unsigned int h, unsigned int thick, unsigned int col);
132 
133  //! Draw a rectangle in a YUYV image
134  /*! This is really not flexible and is optimized for speed only. col will be used for both YU and YV pixels. For
135  more demanding drawings, use openCV functions. The pixels drawn go from (x,y) included to (x+w-1,y+h-1)
136  included. If w or h is 0, it is replaced by 1. \ingroup image */
137  void drawRect(RawImage & img, int x, int y, unsigned int w, unsigned int h, unsigned int col);
138 
139  //! Draw a filled 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 drawFilledRect(RawImage & img, int x, int y, unsigned int w, unsigned int h, unsigned int col);
144 
145  //! Available fonts for writeText()
146  /*! \ingroup image */
147  enum Font
148  {
161  };
162 
163  //! Write some text in an image
164  /*! \ingroup image */
165  void writeText(RawImage & img, std::string const & txt, int x, int y, unsigned int col, Font font = Font6x10);
166 
167  //! Write some text in an image
168  /*! \ingroup image */
169  void writeText(RawImage & img, char const * txt, int x, int y, unsigned int col, Font font = Font6x10);
170 
171  //! Convert a BGR cv::Mat to RawImage with already-allocated pixels and pixel type
172  /*! This is used to output video frames over USB from internally computed results in opencv BGR format. dst should
173  already have an allocated pixel buffer (typically, it should be obtained from the USB gadget), and its size
174  must match that of the src image.
175 
176  Supported dst pixel formats:
177  - V4L2_PIX_FMT_YUYV
178  - V4L2_PIX_FMT_GREY
179  - V4L2_PIX_FMT_SRGGB8 (Bayer)
180  - V4L2_PIX_FMT_RGB565
181  - V4L2_PIX_FMT_MJPG
182  - V4L2_PIX_FMT_BGR24
183 
184  quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
185  void convertCvBGRtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
186 
187  //! Convert a RGB cv::Mat to RawImage with already-allocated pixels and pixel type
188  /*! This is used to output video frames over USB from internally computed results in opencv BGR format. dst should
189  already have an allocated pixel buffer (typically, it should be obtained from the USB gadget), and its size
190  must match that of the src image.
191 
192  Supported dst pixel formats:
193  - V4L2_PIX_FMT_YUYV
194  - V4L2_PIX_FMT_GREY
195  - V4L2_PIX_FMT_SRGGB8 (Bayer)
196  - V4L2_PIX_FMT_RGB565
197  - V4L2_PIX_FMT_MJPG
198  - V4L2_PIX_FMT_BGR24
199 
200  quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
201  void convertCvRGBtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
202 
203  //! Convert an RGBA cv::Mat to RawImage with already-allocated pixels and pixel type
204  /*! This is used to output video frames over USB from internally computed results in opencv RGB-A format (which is
205  often the format of choice for NEON or OpenGL processing). dst should already have an allocated pixel buffer
206  (typically, it should be obtained from the USB gadget), and its size must match that of the src image.
207 
208  Supported dst pixel formats:
209  - V4L2_PIX_FMT_YUYV
210  - V4L2_PIX_FMT_GREY
211  - V4L2_PIX_FMT_SRGGB8 (Bayer)
212  - V4L2_PIX_FMT_RGB565
213  - V4L2_PIX_FMT_MJPG
214  - V4L2_PIX_FMT_BGR24
215 
216  quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
217  void convertCvRGBAtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
218 
219  //! Convert a Gray cv::Mat to RawImage with already-allocated pixels and pixel type
220  /*! This is used to output video frames over USB from internally computed results in opencv Gray format. dst should
221  already have an allocated pixel buffer (typically, it should be obtained from the USB gadget), and its size
222  must match that of the src image.
223 
224  Supported dst pixel formats:
225  - V4L2_PIX_FMT_YUYV
226  - V4L2_PIX_FMT_GREY
227  - V4L2_PIX_FMT_SRGGB8 (Bayer)
228  - V4L2_PIX_FMT_RGB565
229  - V4L2_PIX_FMT_MJPG
230  - V4L2_PIX_FMT_BGR24
231 
232  quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
233  void convertCvGRAYtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
234 
235  //! Split an RGBA cv::Mat into a 4x taller grey RawImage with already-allocated pixels
236  /*! This is used to run GPU shaders that apply different filters to an image and store their results separately into
237  the R, G, B and A components of an RGBA output image. Here, we just unpack the 4 components and stuff them into
238  the output image one above the other: R on top, then G below it, then B below it, then A below it. The
239  destination RawImage must be at least as wide as src and at least 4 times as tall as src, and must have
240  V4L2_PIX_FMT_GREY pixels. \ingroup image */
241  void unpackCvRGBAtoGrayRawImage(cv::Mat const & src, RawImage & dst);
242 
243  //! Flip a YUYV RawImage horizontally while preserving color information
244  /*! This function is to allow one to use JeVois with the PhotoBooth app on a Mac, whih flips the image horizontally
245  (about the vertical axis). Only YUYV pixels are supported. You might be able to use cv::flip() from OpenCV
246  instead with other pixel types. */
247  void hFlipYUYV(RawImage & img);
248 
249  //! OpenCV does not provide conversion from RGB to YUYV in cvtColor(), so this function provides it
250  void convertCvRGBtoCvYUYV(cv::Mat const & src, cv::Mat & dst);
251 
252  //! OpenCV does not provide conversion from BGR to YUYV in cvtColor(), so this function provides it
253  void convertCvBGRtoCvYUYV(cv::Mat const & src, cv::Mat & dst);
254 
255  //! OpenCV does not provide conversion from GRAY to YUYV in cvtColor(), so this function provides it
256  void convertCvGRAYtoCvYUYV(cv::Mat const & src, cv::Mat & dst);
257 
258  //! OpenCV does not provide conversion from RGBA to YUYV in cvtColor(), so this function provides it
259  void convertCvRGBAtoCvYUYV(cv::Mat const & src, cv::Mat & dst);
260 
261  } // namespace rawimage
262 } // namespace jevois
263 
cv::Mat convertToCvRGB(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV RGB byte...
Definition: RawImageOps.C:302
cv::Mat convertToCvRGBA(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV RGB-A byte...
Definition: RawImageOps.C:325
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:30
void drawCircle(RawImage &img, int x, int y, unsigned int rad, unsigned int thick, unsigned int col)
Draw a circle in a YUYV image.
Definition: RawImageOps.C:474
void writeText(RawImage &img, std::string const &txt, int x, int y, unsigned int col, Font font=Font6x10)
Write some text in an image.
Definition: RawImageOps.C:617
cv::Mat convertToCvBGR(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV BGR byte...
Definition: RawImageOps.C:279
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...
Definition: RawImageOps.C:1171
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.
Definition: RawImageOps.C:499
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.
Definition: RawImageOps.C:416
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition: RawImage.H:110
Font
Available fonts for writeText()
Definition: RawImageOps.H:147
void hFlipYUYV(RawImage &img)
Flip a YUYV RawImage horizontally while preserving color information.
Definition: RawImageOps.C:1321
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...
Definition: RawImageOps.C:949
void pasteBGRtoYUYV(cv::Mat const &src, RawImage &dst, int dx, int dy)
Paste a BGR byte image into a YUYV image.
Definition: RawImageOps.C:958
cv::Mat convertToCvGray(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV gray byte...
Definition: RawImageOps.C:245
void convertCvBGRtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert a BGR cv::Mat to RawImage with already-allocated pixels and pixel type.
Definition: RawImageOps.C:1180
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...
Definition: RawImageOps.C:1032
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.
Definition: RawImageOps.C:574
void convertCvRGBAtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert an RGBA cv::Mat to RawImage with already-allocated pixels and pixel type. ...
Definition: RawImageOps.C:1246
void unpackCvRGBAtoGrayRawImage(cv::Mat const &src, RawImage &dst)
Split an RGBA cv::Mat into a 4x taller grey RawImage with already-allocated pixels.
Definition: RawImageOps.C:1222
void pasteGreyToYUYV(cv::Mat const &src, RawImage &dest, int dx, int dy)
Paste a grey byte image into a YUYV image.
Definition: RawImageOps.C:439
void convertCvGRAYtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert a Gray cv::Mat to RawImage with already-allocated pixels and pixel type.
Definition: RawImageOps.C:1266
void pasteRGBtoYUYV(cv::Mat const &src, RawImage &dst, int dx, int dy)
Paste a RGB byte image into a YUYV image.
Definition: RawImageOps.C:1041
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...
Definition: RawImageOps.C:1101
void drawDisk(RawImage &img, int x, int y, unsigned int rad, unsigned int col)
Draw a disk in a YUYV image.
Definition: RawImageOps.C:456
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.
Definition: RawImageOps.C:535
void byteSwap(RawImage &img)
Swap pairs of bytes in a RawImage.
Definition: RawImageOps.C:359
void convertCvRGBtoRawImage(cv::Mat const &src, RawImage &dst, int quality)
Convert a RGB cv::Mat to RawImage with already-allocated pixels and pixel type.
Definition: RawImageOps.C:1201
void paste(RawImage const &src, RawImage &dest, int dx, int dy)
Paste an image within another of same pixel type.
Definition: RawImageOps.C:394