JeVois  1.0
JeVois Smart Embedded Machine Vision Toolkit
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 an ROI from an image to within another of same pixel type
104  /*! To keep this function fast, throws if the source image does not fully fit within the destination image or if the
105  pixel formats differ. \ingroup image */
106  void roipaste(RawImage const & src, int x, int y, unsigned int w, unsigned int h, RawImage & dest, int dx, int dy);
107 
108  //! Draw a disk in a YUYV image
109  /*! \ingroup image */
110  void drawDisk(RawImage & img, int x, int y, unsigned int rad, unsigned int col);
111 
112  //! Draw a circle in a YUYV image
113  /*! \ingroup image */
114  void drawCircle(RawImage & img, int x, int y, unsigned int rad, unsigned int thick, unsigned int col);
115 
116  //! Draw a line in a YUYV image
117  /*! \ingroup image */
118  void drawLine(RawImage & img, int x1, int y1, int x2, int y2, unsigned int thick, unsigned int col);
119 
120  //! Draw a rectangle in a YUYV image
121  /*! \ingroup image */
122  void drawRect(RawImage & img, int x, int y, unsigned int w, unsigned int h, unsigned int thick, unsigned int col);
123 
124  //! Draw a rectangle in a YUYV image
125  /*! This is really not flexible and is optimized for speed only. col will be used for both YU and YV pixels. For
126  more demanding drawings, use openCV functions. \ingroup image */
127  void drawRect(RawImage & img, int x, int y, unsigned int w, unsigned int h, unsigned int col);
128 
129  //! Draw a filled rectangle in a YUYV image
130  /*! This is really not flexible and is optimized for speed only. col will be used for both YU and YV pixels. For
131  more demanding drawings, use openCV functions. \ingroup image */
132  void drawFilledRect(RawImage & img, int x, int y, unsigned int w, unsigned int h, unsigned int col);
133 
134  //! Available fonts for writeText()
135  /*! \ingroup image */
136  enum Font
137  {
150  };
151 
152  //! Write some text in an image
153  /*! \ingroup image */
154  void writeText(RawImage & img, std::string const & txt, int x, int y, unsigned int col, Font font = Font6x10);
155 
156  //! Write some text in an image
157  /*! \ingroup image */
158  void writeText(RawImage & img, char const * txt, int x, int y, unsigned int col, Font font = Font6x10);
159 
160  //! Convert a BGR cv::Mat to RawImage with already-allocated pixels and pixel type
161  /*! This is used to output video frames over USB from internally computed results in opencv BGR format. dst should
162  already have an allocated pixel buffer (typically, it should be obtained from the USB gadget), and its size
163  must match that of the src image.
164 
165  Supported dst pixel formats:
166  - V4L2_PIX_FMT_YUYV
167  - V4L2_PIX_FMT_GREY
168  - V4L2_PIX_FMT_SRGGB8 (Bayer)
169  - V4L2_PIX_FMT_RGB565
170  - V4L2_PIX_FMT_MJPG
171  - V4L2_PIX_FMT_BGR24
172 
173  quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
174  void convertCvBGRtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
175 
176  //! Convert an RGBA cv::Mat to RawImage with already-allocated pixels and pixel type
177  /*! This is used to output video frames over USB from internally computed results in opencv RGB-A format (which is
178  often the format of choice for NEON or OpenGL processing). dst should already have an allocated pixel buffer
179  (typically, it should be obtained from the USB gadget), and its size must match that of the src
180  image.
181 
182  Supported dst pixel formats:
183  - V4L2_PIX_FMT_YUYV
184  - V4L2_PIX_FMT_GREY
185  - V4L2_PIX_FMT_SRGGB8 (Bayer)
186  - V4L2_PIX_FMT_RGB565
187  - V4L2_PIX_FMT_MJPG
188  - V4L2_PIX_FMT_BGR24
189 
190  quality is used only when dst is MJPG and should be between 1 and 100. \ingroup image */
191  void convertCvRGBAtoRawImage(cv::Mat const & src, RawImage & dst, int quality);
192 
193  //! Split an RGBA cv::Mat into a 4x taller grey RawImage with already-allocated pixels
194  /*! This is used to run GPU shaders that apply different filters to an image and store their results separately into
195  the R, G, B and A components of an RGBA output image. Here, we just unpack the 4 components and stuff them into
196  the output image one above the other: R on top, then G below it, then B below it, then A below it. The
197  destination RawImage must be at least as wide as src and at least 4 times as tall as src, and must have
198  V4L2_PIX_FMT_GREY pixels. \ingroup image */
199  void unpackCvRGBAtoGrayRawImage(cv::Mat const & src, RawImage & dst);
200 
201  //! Flip a YUYV RawImage horizontally while preserving color information
202  /*! This function is to allow one to use JeVois with the PhotoBooth app on a Mac, whih flips the image horizontally
203  (about the vertical axis). Only YUYV pixels are supported. You might be able to use cv::flip() from OpenCV
204  instead with other pixel types. */
205  void hFlipYUYV(RawImage & img);
206 
207  } // namespace rawimage
208 } // namespace jevois
209 
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:249
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:272
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:421
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:557
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:226
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:446
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:363
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:136
void hFlipYUYV(RawImage &img)
Flip a YUYV RawImage horizontally while preserving color information.
Definition: RawImageOps.C:961
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:203
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:860
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:516
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:905
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:881
void pasteGreyToYUYV(cv::Mat const &src, RawImage &dest, int dx, int dy)
Paste a grey byte image into a YUYV image.
Definition: RawImageOps.C:386
void drawDisk(RawImage &img, int x, int y, unsigned int rad, unsigned int col)
Draw a disk in a YUYV image.
Definition: RawImageOps.C:403
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:482
void byteSwap(RawImage &img)
Swap pairs of bytes in a RawImage.
Definition: RawImageOps.C:306
void paste(RawImage const &src, RawImage &dest, int dx, int dy)
Paste an image within another of same pixel type.
Definition: RawImageOps.C:341