Fawkes API Fawkes Development Version
cvmatadapter.cpp
1
2
3/***************************************************************************
4 * cvmatadapter.cpp - Helper to convert FireVision buffers to cv::Mat for OpenCV
5 *
6 * Created: Tue May 11 15:57:58 2021
7 * Copyright 2021 Sebastian Eltester
8 *
9 ****************************************************************************/
10
11/* This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version. A runtime exception applies to
15 * this software (see LICENSE.GPL_WRE file mentioned below for details).
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23 */
24
25#include <fvutils/adapters/cvmatadapter.h>
26#include <fvutils/color/conversions.h>
27
28#include <cstddef>
29#include <opencv2/opencv.hpp>
30
31namespace firevision {
32
33/** @class CvMatAdapter <fvutils/adapters/cvmatadapter.h>
34 * Adapter for OpenCV Mat.
35 * Conversion routines from FireVision buffers to OpenCV Mat.
36 * @author Sebastian Eltester
37 */
38
39/** Convert image from buffer into cv::Mat.
40 * @param buffer YUV422_PLANAR buffer of the same size as image
41 * @param image cv::Mat the result will be written to in BGR notation
42 */
43void
44CvMatAdapter::convert_image_bgr(unsigned char *buffer, cv::Mat &image)
45{
46 //8 bit color depth -> width * height + (width * height) * 0.5 + (width * height) * 0.5
47 // Y + U + V
48 unsigned char tmp[image.cols * image.rows * 2];
49 convert(YUV422_PLANAR, YUV422_PACKED, buffer, tmp, image.cols, image.rows);
50 cv::Mat tmp_mat = cv::Mat(image.size(), CV_8UC2, tmp);
51 cv::cvtColor(tmp_mat, image, cv::COLOR_YUV2BGR_UYVY, 3);
52 tmp_mat.release();
53}
54
55/** Convert image from cv::Mat into buffer.
56 * @param image cv::Mat with BGR notation
57 * @param buffer YUV422_PLANAR of the same size to write the converted cv::Mat
58 */
59void
60CvMatAdapter::convert_image_yuv422_planar(cv::Mat &image, unsigned char *buffer)
61{
62 cv::Mat tmp_mat = cv::Mat(image.size(), CV_8UC3, 3);
63 cv::cvtColor(image, tmp_mat, cv::COLOR_BGR2YUV, 3);
64 convert(YUV422_PACKED, YUV422_PLANAR, tmp_mat.data, buffer, image.cols, image.rows);
65 tmp_mat.release();
66}
67
68/* Creates a new IplImage for a ROI.
69 * This will create a new IplImage with the size of the ROI and convert the data of the
70 * passed YUV422_PLANAR buffer to the IplImage.
71 * @param buffer YUV422_PLANAR buffer
72 * @param roi ROI to take the image from
73 * @return new IplImage instance with the image from the ROI. Use cvReleaseImage after you
74 * are done with it.
75IplImage *
76IplImageAdapter::create_image_from_roi(unsigned char *buffer, ROI *roi)
77{
78 IplImage *image = cvCreateImage(cvSize(roi->extent.width, roi->extent.height), IPL_DEPTH_8U, 3);
79
80 unsigned int to_line = roi->start.y + roi->extend.height;
81 unsigned char *
82 for ( unsigned int h = roi->start.y; h < to_line; ++h) {
83
84 }
85 convert(YUV422_PLANAR, BGR, _src, (unsigned char *)image_->imageData, _width, _height);
86
87}
88 */
89
90} // end namespace firevision
static void convert_image_bgr(unsigned char *buffer, cv::Mat &image)
Convert image from buffer into cv::Mat.
static void convert_image_yuv422_planar(cv::Mat &image, unsigned char *buffer)
Convert image from cv::Mat into buffer.