Opencv image conversion (#1677)

A small library with functions to convert between the Paparazzi used YUV422 arrays
and the opencv image functions.
This commit is contained in:
Roland Meertens
2016-05-23 12:04:42 +02:00
committed by Felix Ruess
parent 2e3e97d874
commit 836448e5fe
7 changed files with 107 additions and 12 deletions
+1 -1
View File
@@ -15,7 +15,7 @@
<makefile>
<file name="cv_opencvdemo.c"/>
<file name="opencv_example.cpp"/>
<file name="opencv_image_functions.cpp"/>
<flag name="CXXFLAGS" value="I$(PAPARAZZI_SRC)/sw/ext/opencv_bebop/install/include"/>
<flag name="LDFLAGS" value="L$(PAPARAZZI_SRC)/sw/ext/opencv_bebop/install/lib" />
@@ -20,7 +20,7 @@
/**
* @file "modules/computer_vision/cv_opencvdemo.c"
* @author C. De Wagter
* opencv
* A simple module showing what you can do with opencv on the bebop.
*/
#include "modules/computer_vision/cv.h"
@@ -20,7 +20,7 @@
/**
* @file "modules/computer_vision/opencv_example.cpp"
* @author C. De Wagter
* opencv
* A simple module showing what you can do with opencv on the bebop.
*/
@@ -32,7 +32,7 @@ using namespace std;
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
#include "opencv_image_functions.h"
int opencv_example(char *img, int width, int height)
{
@@ -52,11 +52,6 @@ int opencv_example(char *img, int width, int height)
Canny(image, image, edgeThresh, edgeThresh * 3);
// Convert back to YUV422, and put it in place of the original image
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
img[(row * width + col) * 2 + 1] = image.at<uint8_t>(row, col);
img[(row * width + col) * 2] = 127;
}
}
grayscale_opencv_to_yuv422(image, img, width, height);
return 0;
}
@@ -20,7 +20,7 @@
/**
* @file "modules/computer_vision/cv_opencvdemo.h"
* @author C. De Wagter
* opencv
* A simple module showing what you can do with opencv on the bebop.
*/
#ifndef OPENCV_EXAMPLE_H
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2016 Roland Meertens
*
* This file is part of Paparazzi.
*
* Paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* Paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
/**
* @file modules/computer_vision/opencv_image_functions.cpp
*
* A small library with functions to convert between the Paparazzi used YUV422 arrays
* and the opencv image functions.
*/
#include "opencv_image_functions.h"
using namespace std;
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
void color_opencv_to_yuv422(Mat image, char *img, int width, int height)
{
//Turn the opencv RGB colored image back in a YUV colored image for the drone
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
cv::Vec3b pixelHere = image.at<cv::Vec3b>(row, col);
img[(row * width + col) * 2 + 1] = 0.299 * pixelHere[0] + 0.587 * pixelHere[1] + 0.114 * pixelHere[2];
if (col % 2 == 0) { // U
img[(row * width + col) * 2] = 0.492 * (pixelHere[2] - img[(row * width + col) * 2 + 1] + 127);
} else { // V
img[(row * width + col) * 2] = 0.877 * (pixelHere[0] - img[(row * width + col) * 2 + 1] + 127);
}
}
}
}
void grayscale_opencv_to_yuv422(Mat image, char *img, int width, int height)
{
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
img[(row * width + col) * 2 + 1] = image.at<uint8_t>(row, col);
img[(row * width + col) * 2 ] = 127;
}
}
}
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2016 Roland Meertens
*
* This file is part of Paparazzi.
*
* Paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* Paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
/**
* @file modules/computer_vision/opencv_image_functions.h
*
* A small library with functions to convert between the Paparazzi used YUV422 arrays
* and the opencv image functions.
*/
#ifndef OPENCV_IMAGE_FUNCTIONS_H
#define OPENCV_IMAGE_FUNCTIONS_H
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
void color_opencv_to_yuv422(cv::Mat image, char *img, int width, int height);
void grayscale_opencv_to_yuv422(cv::Mat image, char *img, int width, int height);
#endif