SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
CVImage.h
Go to the documentation of this file.
1 /**
2  * \file cv/CVImage.h
3  * \date Spring 2017
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef CVIMAGE_H
11 #define CVIMAGE_H
12 
13 #include <CVTypedefs.h>
14 
15 using std::string;
16 
17 //-----------------------------------------------------------------------------
18 //! Pixel format according to OpenGL pixel format defines
19 /*!
20  * This is a pretty delicate topic in OpenGL because bugs in GLSL due to pixel
21  * format errors are very hard to detect and debug.
22  */
24 {
26  PF_yuv_420_888 = 1, // YUV format from Android not supported in GL
27  PF_alpha = 0x1906, // ES2 ES3 GL2
28  PF_luminance = 0x1909, // ES2 ES3 GL2
29  PF_luminance_alpha = 0x190A, // ES2 ES3 GL2
30  PF_intensity = 0x8049, // GL2
31  PF_green = 0x1904, // GL2
32  PF_blue = 0x1905, // GL2
33  PF_depth_component = 0x1902, // ES3 GL2 GL4
34  PF_red = 0x1903, // ES3 GL2 GL3 GL4
35  PF_rg = 0x8227, // ES3 GL3 GL4
36  PF_rgb = 0x1907, // ES2 ES3 GL2 GL3 GL4
37  PF_rgba = 0x1908, // ES2 ES3 GL2 GL3 GL4
38  PF_bgr = 0x80E0, // GL2 GL3 GL4
39  PF_bgra = 0x80E1, // GL2 GL3 GL4
40  PF_rg_integer = 0x8228, // ES3 GL4
41  PF_red_integer = 0x8D94, // ES3 GL4
42  PF_rgb_integer = 0x8D98, // ES3 GL4
43  PF_rgba_integer = 0x8D99, // ES3 GL4
44  PF_bgr_integer = 0x8D9A, // GL4
45  PF_bgra_integer = 0x8D9B, // GL4
46  PF_r16f = 0x822D,
47  PF_r32f = 0x822E, // ES3 GL3 GL4
48  PF_rg16f = 0x822F,
49  PF_rg32f = 0x8230,
50  PF_rgba32f = 0x8814,
51  PF_rgb32f = 0x8815,
52  PF_rgba16f = 0x881A,
53  PF_rgb16f = 0x881B
54 };
55 //-----------------------------------------------------------------------------
56 //! OpenCV image class with the same interface as the former SLImage class
57 /*! The core object is the OpenCV matrix _cvMat. Be aware the OpenCV accesses its
58 matrix of type mat often by row and columns. In that order it corresponds to
59 the y and x coordinates and not x and y as we are used to!
60 See the OpenCV docs for more information:
61 http://docs.opencv.org/2.4.10/modules/core/doc/basic_structures.html#mat
62 */
63 class CVImage
64 {
65 public:
66  CVImage();
67  CVImage(int width,
68  int height,
70  string name);
71  explicit CVImage(const string& imageFilename,
72  bool flipVertical = true,
73  bool loadGrayscaleIntoAlpha = false);
74  CVImage(CVImage& srcImage);
75  explicit CVImage(const CVVVec3f& colors);
76  explicit CVImage(const CVVVec4f& colors);
77  ~CVImage();
78 
79  // Misc
80  void clearData();
81  bool allocate(int width,
82  int height,
83  CVPixelFormatGL pixelFormatGL,
84  bool isContinuous = true);
85  void load(const string& filename,
86  bool flipVertical = true,
87  bool loadGrayscaleIntoAlpha = false);
88  bool load(int inWidth,
89  int inHeight,
90  CVPixelFormatGL srcPixelFormatGL,
91  CVPixelFormatGL dstPixelFormatGL,
92  uchar* data,
93  bool isContinuous,
94  bool isTopLeft);
95  void savePNG(const string& filename,
96  int compressionLevel = 6,
97  bool flipY = true,
98  bool convertBGR2RGB = true);
99  void saveJPG(const string& filename,
100  int compressionLevel = 95,
101  bool flipY = true,
102  bool convertBGR2RGB = true);
103  CVVec4f getPixeli(int x, int y);
104  CVVec4f getPixelf(float x, float y);
105  void setPixeli(int x, int y, CVVec4f color);
106  void setPixeliRGB(int x, int y, CVVec3f color);
107  void setPixeliRGB(int x, int y, CVVec4f color);
108  void setPixeliRGBA(int x, int y, CVVec4f color);
109  void resize(int width, int height);
110  void convertTo(int cvDataType);
111  void flipX();
112  void flipY();
113  void fill(uchar r, uchar g, uchar b);
114  void fill(uchar r, uchar g, uchar b, uchar a);
115  void crop(float targetWdivH, int& cropW, int& cropH);
116  static CVPixelFormatGL cvType2glPixelFormat(int cvType);
117  static int glPixelFormat2cvType(CVPixelFormatGL pixelFormatGL);
118  static string formatString(CVPixelFormatGL pixelFormatGL);
119 
120  // Getters
121  string name() { return _name; }
122  CVMat cvMat() const { return _cvMat; }
123  uchar* data() { return _cvMat.data; }
124  bool empty() const { return _cvMat.empty(); }
125  uint width() { return (uint)_cvMat.cols; }
126  uint height() { return (uint)_cvMat.rows; }
127  uint bytesPerPixel() { return _bytesPerPixel; }
128  uint bytesPerLine() { return _bytesPerLine; }
129  uint bytesPerImage() { return _bytesPerImage; }
130  uint bytesInFile() { return _bytesInFile; }
132  string formatString() { return formatString(_format); }
133  string path() { return _path; }
134  static string typeString(int cvMatTypeInt);
135 
136 protected:
137  static uint bytesPerPixel(CVPixelFormatGL pixelFormat);
138  static uint bytesPerLine(uint width,
139  CVPixelFormatGL pixelFormat,
140  bool isContinuous = false);
141 
142  string _name; //!< Image name (e.g. from the filename)
143  CVMat _cvMat; //!< OpenCV mat matrix image type
144  CVPixelFormatGL _format; //!< OpenGL pixel format
145  uint _bytesPerPixel; //!< Number of bytes per pixel
146  uint _bytesPerLine; //!< Number of bytes per line (stride)
147  uint _bytesPerImage; //!< Number of bytes per image
148  uint _bytesInFile; //!< Number of bytes in file
149  string _path; //!< path on the filesystem
150 };
151 //-----------------------------------------------------------------------------
152 typedef vector<CVImage*> CVVImage;
153 //-----------------------------------------------------------------------------
154 #endif
CVPixelFormatGL
Pixel format according to OpenGL pixel format defines.
Definition: CVImage.h:24
@ PF_rgb_integer
Definition: CVImage.h:42
@ PF_rgba16f
Definition: CVImage.h:52
@ PF_luminance
Definition: CVImage.h:28
@ PF_bgr_integer
Definition: CVImage.h:44
@ PF_alpha
Definition: CVImage.h:27
@ PF_rgba_integer
Definition: CVImage.h:43
@ PF_green
Definition: CVImage.h:31
@ PF_rg_integer
Definition: CVImage.h:40
@ PF_red
Definition: CVImage.h:34
@ PF_luminance_alpha
Definition: CVImage.h:29
@ PF_rgb
Definition: CVImage.h:36
@ PF_bgra_integer
Definition: CVImage.h:45
@ PF_rg
Definition: CVImage.h:35
@ PF_depth_component
Definition: CVImage.h:33
@ PF_bgra
Definition: CVImage.h:39
@ PF_unknown
Definition: CVImage.h:25
@ PF_rgba
Definition: CVImage.h:37
@ PF_yuv_420_888
Definition: CVImage.h:26
@ PF_r32f
Definition: CVImage.h:47
@ PF_bgr
Definition: CVImage.h:38
@ PF_r16f
Definition: CVImage.h:46
@ PF_rg16f
Definition: CVImage.h:48
@ PF_red_integer
Definition: CVImage.h:41
@ PF_rgb32f
Definition: CVImage.h:51
@ PF_rgba32f
Definition: CVImage.h:50
@ PF_blue
Definition: CVImage.h:32
@ PF_rg32f
Definition: CVImage.h:49
@ PF_intensity
Definition: CVImage.h:30
@ PF_rgb16f
Definition: CVImage.h:53
vector< CVImage * > CVVImage
Definition: CVImage.h:152
cv::Vec4f CVVec4f
Definition: CVTypedefs.h:54
vector< cv::Vec3f > CVVVec3f
Definition: CVTypedefs.h:85
vector< cv::Vec4f > CVVVec4f
Definition: CVTypedefs.h:86
cv::Vec3f CVVec3f
Definition: CVTypedefs.h:52
cv::Mat CVMat
Definition: CVTypedefs.h:38
OpenCV image class with the same interface as the former SLImage class.
Definition: CVImage.h:64
uint _bytesPerPixel
Number of bytes per pixel.
Definition: CVImage.h:145
CVImage(const CVVVec4f &colors)
void resize(int width, int height)
Definition: CVImage.cpp:938
CVVec4f getPixelf(float x, float y)
Definition: CVImage.cpp:788
void setPixeliRGB(int x, int y, CVVec3f color)
setPixeli sets the RGB pixel color at the integer pixel coordinate x, y
Definition: CVImage.cpp:893
void fill(uchar r, uchar g, uchar b)
Fills the image with a certain rgb color.
Definition: CVImage.cpp:985
uint width()
Definition: CVImage.h:125
~CVImage()
Definition: CVImage.cpp:95
void flipX()
Flip X coordinates used to make JPEGs from top-left to bottom-left images.
Definition: CVImage.cpp:963
uint bytesInFile()
Definition: CVImage.h:130
uchar * data()
Definition: CVImage.h:123
CVPixelFormatGL _format
OpenGL pixel format.
Definition: CVImage.h:144
void setPixeliRGBA(int x, int y, CVVec4f color)
setPixeli sets the RGBA pixel color at the integer pixel coordinate x, y
Definition: CVImage.cpp:921
string formatString()
Definition: CVImage.h:132
void clearData()
Deletes all data and resets the image parameters.
Definition: CVImage.cpp:102
uint _bytesPerLine
Number of bytes per line (stride)
Definition: CVImage.h:146
static string typeString(int cvMatTypeInt)
Returns the cv::Mat.type()) as string.
Definition: CVImage.cpp:1069
void savePNG(const string &filename, int compressionLevel=6, bool flipY=true, bool convertBGR2RGB=true)
Save as PNG at a certain compression level (0-9)
Definition: CVImage.cpp:637
CVMat cvMat() const
Definition: CVImage.h:122
CVPixelFormatGL format()
Definition: CVImage.h:131
bool empty() const
Definition: CVImage.h:124
static int glPixelFormat2cvType(CVPixelFormatGL pixelFormatGL)
Converts OpenGL pixel format to OpenCV mat type.
Definition: CVImage.cpp:563
uint _bytesPerImage
Number of bytes per image.
Definition: CVImage.h:147
string name()
Definition: CVImage.h:121
static CVPixelFormatGL cvType2glPixelFormat(int cvType)
Converts OpenCV mat type to OpenGL pixel format.
Definition: CVImage.cpp:522
uint bytesPerImage()
Definition: CVImage.h:129
uint bytesPerPixel()
Definition: CVImage.h:127
uint height()
Definition: CVImage.h:126
CVVec4f getPixeli(int x, int y)
getPixeli returns the pixel color at the integer pixel coordinate x, y
Definition: CVImage.cpp:710
void saveJPG(const string &filename, int compressionLevel=95, bool flipY=true, bool convertBGR2RGB=true)
Save as JPG at a certain compression level (0-100)
Definition: CVImage.cpp:675
string _path
path on the filesystem
Definition: CVImage.h:149
uint bytesPerLine()
Definition: CVImage.h:128
CVMat _cvMat
OpenCV mat matrix image type.
Definition: CVImage.h:143
void convertTo(int cvDataType)
Converts the data type of the cvMat.
Definition: CVImage.cpp:951
bool allocate(int width, int height, CVPixelFormatGL pixelFormatGL, bool isContinuous=true)
Memory allocation function.
Definition: CVImage.cpp:119
void flipY()
Flip Y coordinates used to make JPEGs from top-left to bottom-left images.
Definition: CVImage.cpp:974
void load(const string &filename, bool flipVertical=true, bool loadGrayscaleIntoAlpha=false)
Loads the image with the appropriate image loader.
Definition: CVImage.cpp:379
void setPixeli(int x, int y, CVVec4f color)
setPixeli sets the RGB pixel color at the integer pixel coordinate x, y
Definition: CVImage.cpp:834
uint _bytesInFile
Number of bytes in file.
Definition: CVImage.h:148
void crop(float targetWdivH, int &cropW, int &cropH)
Definition: CVImage.cpp:1014
string path()
Definition: CVImage.h:133
string _name
Image name (e.g. from the filename)
Definition: CVImage.h:142
CVImage()
Default constructor.
Definition: CVImage.cpp:22