27 #include <opencv2/core/core.hpp>
28 #include <opencv2/imgproc/imgproc.hpp>
43 for (
int row = 0; row < height; row++) {
44 for (
int col = 0; col < width; col++) {
46 cv::Vec3b &c = image.at<cv::Vec3b>(row, col);
49 int i = row * width + col;
50 img[2 * i + 1] = c[0];
51 img[2 * i] = col % 2 ? c[1] : c[2];
59 for (
int row = 0; row < height; row++) {
60 for (
int col = 0; col < width; col++) {
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);
66 c_m1 = image_in.at<cv::Vec3b>(row, col - 1);
69 c_p1 = image_in.at<cv::Vec3b>(row, col + 1);
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];
82 Mat M(width, height, CV_8UC2, img);
83 Mat image, edge_image, thresh_image;
86 cvtColor(M, M, CV_YUV2RGB_Y422);
87 cvtColor(M, M, CV_RGB2YUV);
94 vector<vector<Point> > contours;
95 vector<Vec4i> hierarchy;
96 edge_image = thresh_image;
98 Canny(edge_image, edge_image, edgeThresh, edgeThresh * 3);
99 findContours(edge_image, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
102 vector<Moments>
mu(contours.size());
103 for (
unsigned int i = 0; i < contours.size(); i++) {
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);
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));
117 drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
118 circle(drawing, mc[i], 4, color, -1, 8, 0);
122 int largest_contour_index = 0;
123 int largest_area = 0;
127 for (
unsigned int i = 0; i < contours.size(); i++) {
129 double a = contourArea(contours[i],
false);
130 if (a > largest_area) {
133 largest_contour_index = i;
135 bounding_rect = boundingRect(contours[i]);
138 Scalar color(255, 255, 255);
140 drawContours(M, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy);
142 rectangle(M, bounding_rect, Scalar(0, 255, 0), 2, 8, 0);
145 circle(M, mc[largest_contour_index], 4, Scalar(0, 255, 0), -1, 8, 0);
146 Point2f rect_center(bounding_rect.x + bounding_rect.width / 2 , bounding_rect.y + bounding_rect.height / 2);
147 circle(image, rect_center, 4, Scalar(0, 0, 255), -1, 8, 0);
151 float contour_distance_est;
153 float area = bounding_rect.width * bounding_rect.height;
155 contour_distance_est = 0.1;
157 if ((area > 16000.) && (area < 28000.)) {
158 contour_distance_est = 0.5;
160 if ((area > 11000.) && (area < 16000.)) {
161 contour_distance_est = 1;
163 if ((area > 3000.) && (area < 11000.)) {
164 contour_distance_est = 1.5;
167 contour_distance_est = 2.0;
170 float Im_center_w = width / 2.;
171 float Im_center_h = height / 2.;
172 float real_size = 1.;
173 cont_est.
contour_d_y = -(rect_center.x - Im_center_w) * real_size /
float(bounding_rect.width);
174 cont_est.
contour_d_z = -(rect_center.y - Im_center_h) * real_size /
float(bounding_rect.height);
float moments[MOMENT_DELAY]
static float mu
Physical model parameters.
struct contour_threshold cont_thres
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...