Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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  int byte_index = 0;
46  for(int r = 0; r < nRows; ++r) {
47  for(int c = 0; c < nCols; ++c) {
48  Vec3b yuv = image.at<Vec3b>(r, c);
49  if((byte_index % 4) == 0) {
50  img[byte_index++] = yuv.val[1]; // U
51  } else {
52  img[byte_index++] = yuv.val[2]; // V
53  }
54  img[byte_index++] = yuv.val[0]; // Y
55  }
56  }
57 }
58 
59 void colorbgr_opencv_to_yuv422(Mat image, char *img, int width, int height)
60 {
61  // Convert to YUV color space
62  cvtColor(image, image, COLOR_BGR2YUV);
63  // then call the to color function
64  coloryuv_opencv_to_yuv422(image, img, width, height);
65 }
66 
67 
68 void grayscale_opencv_to_yuv422(Mat image, char *img, int width, int height)
69 {
70  CV_Assert(image.depth() == CV_8U);
71  CV_Assert(image.channels() == 1);
72 
73  int n_rows = image.rows;
74  int n_cols = image.cols;
75 
76  // If the image is one block in memory we can iterate over it all at once!
77  if (image.isContinuous()) {
78  n_cols *= n_rows;
79  n_rows = 1;
80  }
81 
82  // Iterate over the image, setting only the Y value
83  // and setting U and V to 127
84  int i, j;
85  uchar *p;
86  int index_img = 0;
87  for (i = 0; i < n_rows; ++i) {
88  p = image.ptr<uchar>(i);
89  for (j = 0; j < n_cols; j++) {
90  img[index_img++] = 127;
91  img[index_img++] = p[j];
92 
93 
94  }
95  }
96 }
static float p[2][2]
void coloryuv_opencv_to_yuv422(Mat image, char *img, int width, int height)
void grayscale_opencv_to_yuv422(Mat image, char *img, int width, int height)
void colorbgr_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...