Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
opencv_image_functions.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Roland Meertens
3  *
4  * This file is part of Paparazzi.
5  *
6  * Paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * Paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  *
20  */
21 
29 #include "opencv_image_functions.h"
30 
31 
32 using namespace std;
33 #include <opencv2/core/core.hpp>
34 #include <opencv2/imgproc/imgproc.hpp>
35 using namespace cv;
36 
37 void coloryuv_opencv_to_yuv422(Mat image, char *img, int width, int height)
38 {
39  CV_Assert(image.depth() == CV_8U);
40  CV_Assert(image.channels() == 3);
41 
42  int nRows = image.rows;
43  int nCols = image.cols;
44 
45  // If the image is one block in memory we can iterate over it all at once!
46  if (image.isContinuous()) {
47  nCols *= nRows;
48  nRows = 1;
49  }
50 
51  // Iterate over the image, setting only the Y value
52  // and setting U and V to 127
53  int i, j;
54  uchar *p;
55  int index_img = 0;
56  for (i = 0; i < nRows; ++i) {
57  p = image.ptr<uchar>(i);
58  for (j = 0; j < nCols; j += 6) {
59  img[index_img++] = p[j + 1]; //U
60  img[index_img++] = p[j];//Y
61  img[index_img++] = p[j + 2]; //V
62  img[index_img++] = p[j + 3]; //Y
63 
64 
65  }
66  }
67 }
68 
69 void colorrgb_opencv_to_yuv422(Mat image, char *img, int width, int height)
70 {
71  // Convert to YUV color space
72  cvtColor(image, image, COLOR_BGR2YUV);
73  // then call the to color function
74  coloryuv_opencv_to_yuv422(image, img, width, height);
75 }
76 
77 
78 void grayscale_opencv_to_yuv422(Mat image, char *img, int width, int height)
79 {
80  CV_Assert(image.depth() == CV_8U);
81  CV_Assert(image.channels() == 1);
82 
83  int n_rows = image.rows;
84  int n_cols = image.cols;
85 
86  // If the image is one block in memory we can iterate over it all at once!
87  if (image.isContinuous()) {
88  n_cols *= n_rows;
89  n_rows = 1;
90  }
91 
92  // Iterate over the image, setting only the Y value
93  // and setting U and V to 127
94  int i, j;
95  uchar *p;
96  int index_img = 0;
97  for (i = 0; i < n_rows; ++i) {
98  p = image.ptr<uchar>(i);
99  for (j = 0; j < n_cols; j++) {
100  img[index_img++] = 127;
101  img[index_img++] = p[j];
102 
103 
104  }
105  }
106 }
void grayscale_opencv_to_yuv422(Mat image, char *img, int width, int height)
void coloryuv_opencv_to_yuv422(Mat image, char *img, int width, int height)
void colorrgb_opencv_to_yuv422(Mat image, char *img, int width, int height)
A small library with functions to convert between the Paparazzi used YUV422 arrays and the opencv ima...
static float p[2][2]