JeVois  1.3
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 an ROI from an image to within another of same pixel type
108  /*! To keep this function fast, throws if the source image does not fully fit within the destination image or if the
109  pixel formats differ. \ingroup image */
110  void roipaste(RawImage const & src, int x, int y, unsigned int w, unsigned int h, RawImage & dest, int dx, int dy);
111 
112  //! Draw a disk in a YUYV image
113  /*! \ingroup image */
114  void drawDisk(RawImage & img, int x, int y, unsigned int rad, unsigned int col);
115 
116  //! Draw a circle in a YUYV image
117  /*! \ingroup image */
118  void drawCircle(RawImage & img, int x, int y, unsigned int rad, unsigned int thick, unsigned int col);
119 
120  //! Draw a line in a YUYV image
121  /*! \ingroup image */
122  void drawLine(RawImage & img, int x1, int y1, int x2, int y2, unsigned int thick, unsigned int col);
123 
124  //! Draw a rectangle in a YUYV image
125  /*! \ingroup image */
126  void drawRect(RawImage & img, int x, int y, unsigned int w, unsigned int h, unsigned int thick, unsigned int col);
127 
128  //! Draw a rectangle in a YUYV image
129  /*! This is really not flexible and is optimized for speed only. col will be used for both YU and YV pixels. For
130  more demanding drawings, use openCV functions. \ingroup image */
131  void drawRect(RawImage & img, int x, int y, unsigned int w, unsigned int h, unsigned int col);
132 
133  //! Draw a filled 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. \ingroup image */
136  void drawFilledRect(RawImage & img, int x, int y, unsigned int w, unsigned int h, unsigned int col);
137 
138  //! Available fonts for writeText()
139  /*! \ingroup image */
140  enum Font
141  {
154  };
155 
156  //! Write some text in an image
157  /*! \ingroup image */
158  void writeText(RawImage & img, std::string const & txt, int x, int y, unsigned int col, Font font = Font6x10);
159 
160  //! Write some text in an image
161  /*! \ingroup image */
162  void writeText(RawImage & img, char const * txt, int x, int y, unsigned int col, Font font = Font6x10);
163 
164  //! Convert a BGR cv::Mat to RawImage with already-allocated pixels and pixel type
165  /*! This is used to output video frames over USB from internally computed results in opencv BGR format. dst should
166  already have an allocated pixel buffer (typically, it should be obtained from the USB gadget), and its size
167  must match that of the src image.
168 
169  Supported dst pixel formats:
170  - V4L2_PIX_FMT_YUYV
171  - V4L2_PIX_FMT_GREY
172  - V4L2_PIX_FMT_SRGGB8 (Bayer)
173  - V4L2_PIX_FMT_RGB565
174  - V4L2_PIX_FMT_MJPG
175  - V4L2_PIX_FMT_BGR24
176 
177  quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
178  void convertCvBGRtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
179 
180  //! Convert a RGB cv::Mat to RawImage with already-allocated pixels and pixel type
181  /*! This is used to output video frames over USB from internally computed results in opencv BGR format. dst should
182  already have an allocated pixel buffer (typically, it should be obtained from the USB gadget), and its size
183  must match that of the src image.
184 
185  Supported dst pixel formats:
186  - V4L2_PIX_FMT_YUYV
187  - V4L2_PIX_FMT_GREY
188  - V4L2_PIX_FMT_SRGGB8 (Bayer)
189  - V4L2_PIX_FMT_RGB565
190  - V4L2_PIX_FMT_MJPG
191  - V4L2_PIX_FMT_BGR24
192 
193  quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
194  void convertCvRGBtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
195 
196  //! Convert an RGBA cv::Mat to RawImage with already-allocated pixels and pixel type
197  /*! This is used to output video frames over USB from internally computed results in opencv RGB-A format (which is
198  often the format of choice for NEON or OpenGL processing). dst should already have an allocated pixel buffer
199  (typically, it should be obtained from the USB gadget), and its size must match that of the src image.
200 
201  Supported dst pixel formats:
202  - V4L2_PIX_FMT_YUYV
203  - V4L2_PIX_FMT_GREY
204  - V4L2_PIX_FMT_SRGGB8 (Bayer)
205  - V4L2_PIX_FMT_RGB565
206  - V4L2_PIX_FMT_MJPG
207  - V4L2_PIX_FMT_BGR24
208 
209  quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
210  void convertCvRGBAtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
211 
212  //! Convert a Gray cv::Mat to RawImage with already-allocated pixels and pixel type
213  /*! This is used to output video frames over USB from internally computed results in opencv Gray format. dst should
214  already have an allocated pixel buffer (typically, it should be obtained from the USB gadget), and its size
215  must match that of the src image.
216 
217  Supported dst pixel formats:
218  - V4L2_PIX_FMT_YUYV
219  - V4L2_PIX_FMT_GREY
220  - V4L2_PIX_FMT_SRGGB8 (Bayer)
221  - V4L2_PIX_FMT_RGB565
222  - V4L2_PIX_FMT_MJPG
223  - V4L2_PIX_FMT_BGR24
224 
225  quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
226  void convertCvGRAYtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
227 
228  //! Split an RGBA cv::Mat into a 4x taller grey RawImage with already-allocated pixels
229  /*! This is used to run GPU shaders that apply different filters to an image and store their results separately into
230  the R, G, B and A components of an RGBA output image. Here, we just unpack the 4 components and stuff them into
231  the output image one above the other: R on top, then G below it, then B below it, then A below it. The
232  destination RawImage must be at least as wide as src and at least 4 times as tall as src, and must have
233  V4L2_PIX_FMT_GREY pixels. \ingroup image */
234  void unpackCvRGBAtoGrayRawImage(cv::Mat const & src, RawImage & dst);
235 
236  //! Flip a YUYV RawImage horizontally while preserving color information
237  /*! This function is to allow one to use JeVois with the PhotoBooth app on a Mac, whih flips the image horizontally
238  (about the vertical axis). Only YUYV pixels are supported. You might be able to use cv::flip() from OpenCV
239  instead with other pixel types. */
240  void hFlipYUYV(RawImage & img);
241 
242  } // namespace rawimage
243 } // namespace jevois
244 
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:610
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 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:140
void hFlipYUYV(RawImage &img)
Flip a YUYV RawImage horizontally while preserving color information.
Definition: RawImageOps.C:1267
void pasteBGRtoYUYV(cv::Mat const &src, RawImage &dst, int dx, int dy)
Paste a BGR byte image into a YUYV image.
Definition: RawImageOps.C:942
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:1126
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:569
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:1192
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:1168
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:1212
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:1147
void paste(RawImage const &src, RawImage &dest, int dx, int dy)
Paste an image within another of same pixel type.
Definition: RawImageOps.C:394