Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
opencv_contour.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Roland Meertens and Peng Lu
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 */
26#include "opencv_contour.h"
27#include <opencv2/core/core.hpp>
28#include <opencv2/imgproc/imgproc.hpp>
30
31using namespace cv;
32using namespace std;
33
36
37RNG rng(12345);
38
39// YUV in opencv convert to YUV on Bebop
40void yuv_opencv_to_yuv422(Mat image, char *img, int width, int height)
41{
42//Turn the opencv RGB colored image back in a YUV colored image for the drone
43 for (int row = 0; row < height; row++) {
44 for (int col = 0; col < width; col++) {
45 // Extract pixel color from image
46 cv::Vec3b &c = image.at<cv::Vec3b>(row, col);
47
48 // Set image buffer values
49 int i = row * width + col;
50 img[2 * i + 1] = c[0]; // y;
51 img[2 * i] = col % 2 ? c[1] : c[2]; // u or v
52 }
53 }
54}
55
56void uyvy_opencv_to_yuv_opencv(Mat image, Mat image_in, int width, int height)
57{
58//Turn the opencv RGB colored image back in a YUV colored image for the drone
59 for (int row = 0; row < height; row++) {
60 for (int col = 0; col < width; col++) {
61 // Extract pixel color from image
62 cv::Vec3b c = image_in.at<cv::Vec3b>(row, col);
63 cv::Vec3b c_m1 = image_in.at<cv::Vec3b>(row, col);
64 cv::Vec3b c_p1 = image_in.at<cv::Vec3b>(row, col);
65 if (col > 0) {
66 c_m1 = image_in.at<cv::Vec3b>(row, col - 1);
67 }
68 if (col < width) {
69 c_p1 = image_in.at<cv::Vec3b>(row, col + 1);
70 }
71 image.at<cv::Vec3b>(row, col)[0] = c[1] ;
72 image.at<cv::Vec3b>(row, col)[1] = col % 2 ? c[0] : c_m1[0];
73 image.at<cv::Vec3b>(row, col)[2] = col % 2 ? c_p1[0] : c[0];
74
75 }
76 }
77}
78
79void find_contour(char *img, int width, int height)
80{
81 // Create a new image, using the original bebop image.
82 Mat M(width, height, CV_8UC2, img); // original
84
85 // convert UYVY in paparazzi to YUV in opencv
88
89 // Threshold all values within the indicted YUV values.
92
97 int edgeThresh = 35;
100
101 // Get the moments
103 for (unsigned int i = 0; i < contours.size(); i++) {
104 mu[i] = moments(contours[i], false);
105 }
106
107 // Get the mass centers:
108 vector<Point2f> mc(contours.size());
109 for (unsigned int i = 0; i < contours.size(); i++) {
110 mc[i] = Point2f(mu[i].m10 / mu[i].m00 , mu[i].m01 / mu[i].m00);
111 }
112
114 Mat drawing = Mat::zeros(edge_image.size(), CV_8UC3);
115 for (unsigned int i = 0; i < contours.size(); i++) {
116 Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
118 circle(drawing, mc[i], 4, color, -1, 8, 0);
119 }
120
121 // Find Largest Contour
122 int largest_contour_index = 0;
123 int largest_area = 0;
125
126 // iterate through each contour.
127 for (unsigned int i = 0; i < contours.size(); i++) {
128 // Find the area of contour
129 double a = contourArea(contours[i], false);
130 if (a > largest_area) {
131 largest_area = a;
132 // Store the index of largest contour
134 // Find the bounding rectangle for biggest contour
136 }
137 }
138 Scalar color(255, 255, 255);
139 // Draw the contour and rectangle
141
142 rectangle(M, bounding_rect, Scalar(0, 255, 0), 2, 8, 0);
143
144 // some figure can cause there are no largest circles, in this case, do not draw circle
145 circle(M, mc[largest_contour_index], 4, Scalar(0, 255, 0), -1, 8, 0);
147 circle(image, rect_center, 4, Scalar(0, 0, 255), -1, 8, 0);
148
149 // Convert back to YUV422, and put it in place of the original image
150 grayscale_opencv_to_yuv422(M, img, width, height);
152 //estimate the distance in X, Y and Z direction
153 float area = bounding_rect.width * bounding_rect.height;
154 if (area > 28000.) {
156 }
157 if ((area > 16000.) && (area < 28000.)) {
159 }
160 if ((area > 11000.) && (area < 16000.)) {
162 }
163 if ((area > 3000.) && (area < 11000.)) {
165 }
166 if (area < 3000.) {
168 }
170 float Im_center_w = width / 2.;
171 float Im_center_h = height / 2.;
172 float real_size = 1.; // real size of the object
173 cont_est.contour_d_y = -(rect_center.x - Im_center_w) * real_size / float(bounding_rect.width); // right hand
174 cont_est.contour_d_z = -(rect_center.y - Im_center_h) * real_size / float(bounding_rect.height); // point downwards
175}
176
177
178
179
180
181
182
183
184
185
186
187
188
189
float moments[MOMENT_DELAY]
Definition ins_flow.c:169
uint16_t foo
Definition main_demo5.c:58
static float mu
Physical model parameters.
struct contour_threshold cont_thres
RNG rng(12345)
void find_contour(char *img, int width, int height)
void uyvy_opencv_to_yuv_opencv(Mat image, Mat image_in, int width, int height)
void yuv_opencv_to_yuv422(Mat image, char *img, int width, int height)
struct contour_estimation cont_est
Detects contours of an obstacle used in the autonomous drone racing.
void grayscale_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...