Compare commits
No commits in common. "cffd761d2e9c99acc591273eea898eb9ff3c4bf0" and "6377ebf568749de0bd07bafdd578cb85895a1ade" have entirely different histories.
cffd761d2e
...
6377ebf568
@ -65,75 +65,73 @@
|
|||||||
* ]| will correct camera distortion once camera calibration is done.
|
* ]| will correct camera distortion once camera calibration is done.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
#ifdef HAVE_CONFIG_H
|
||||||
# include <config.h>
|
# include <config.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "camerautils.hpp"
|
#include "camerautils.hpp"
|
||||||
#include "cameraevent.hpp"
|
#include "cameraevent.hpp"
|
||||||
|
|
||||||
#include "gstcameraundistort.h"
|
#include "gstcameraundistort.h"
|
||||||
|
|
||||||
#include <opencv2/imgproc.hpp>
|
#include <opencv2/imgproc.hpp>
|
||||||
#include <opencv2/calib3d.hpp>
|
#include <opencv2/calib3d.hpp>
|
||||||
|
|
||||||
#include <gst/opencv/gstopencvutils.h>
|
#include <gst/opencv/gstopencvutils.h>
|
||||||
|
|
||||||
#include <opencv2/core/ocl.hpp>
|
GST_DEBUG_CATEGORY_STATIC (gst_camera_undistort_debug);
|
||||||
|
#define GST_CAT_DEFAULT gst_camera_undistort_debug
|
||||||
|
|
||||||
GST_DEBUG_CATEGORY_STATIC (gst_camera_undistort_debug);
|
#define DEFAULT_SHOW_UNDISTORTED TRUE
|
||||||
#define GST_CAT_DEFAULT gst_camera_undistort_debug
|
#define DEFAULT_ALPHA 0.0
|
||||||
|
#define DEFAULT_CROP FALSE
|
||||||
|
|
||||||
#define DEFAULT_SHOW_UNDISTORTED TRUE
|
enum
|
||||||
#define DEFAULT_ALPHA 0.0
|
{
|
||||||
#define DEFAULT_CROP FALSE
|
|
||||||
|
|
||||||
enum
|
|
||||||
{
|
|
||||||
PROP_0,
|
PROP_0,
|
||||||
PROP_SHOW_UNDISTORTED,
|
PROP_SHOW_UNDISTORTED,
|
||||||
PROP_ALPHA,
|
PROP_ALPHA,
|
||||||
PROP_CROP,
|
PROP_CROP,
|
||||||
PROP_SETTINGS
|
PROP_SETTINGS
|
||||||
};
|
};
|
||||||
|
|
||||||
G_DEFINE_TYPE_WITH_CODE (GstCameraUndistort, gst_camera_undistort,
|
G_DEFINE_TYPE_WITH_CODE (GstCameraUndistort, gst_camera_undistort,
|
||||||
GST_TYPE_OPENCV_VIDEO_FILTER,
|
GST_TYPE_OPENCV_VIDEO_FILTER,
|
||||||
GST_DEBUG_CATEGORY_INIT (gst_camera_undistort_debug, "cameraundistort", 0,
|
GST_DEBUG_CATEGORY_INIT (gst_camera_undistort_debug, "cameraundistort", 0,
|
||||||
"Performs camera undistortion");
|
"Performs camera undistortion");
|
||||||
);
|
);
|
||||||
GST_ELEMENT_REGISTER_DEFINE (cameraundistort, "cameraundistort", GST_RANK_NONE,
|
GST_ELEMENT_REGISTER_DEFINE (cameraundistort, "cameraundistort", GST_RANK_NONE,
|
||||||
GST_TYPE_CAMERA_UNDISTORT);
|
GST_TYPE_CAMERA_UNDISTORT);
|
||||||
|
|
||||||
static void gst_camera_undistort_dispose (GObject * object);
|
static void gst_camera_undistort_dispose (GObject * object);
|
||||||
static void gst_camera_undistort_set_property (GObject * object, guint prop_id,
|
static void gst_camera_undistort_set_property (GObject * object, guint prop_id,
|
||||||
const GValue * value, GParamSpec * pspec);
|
const GValue * value, GParamSpec * pspec);
|
||||||
static void gst_camera_undistort_get_property (GObject * object, guint prop_id,
|
static void gst_camera_undistort_get_property (GObject * object, guint prop_id,
|
||||||
GValue * value, GParamSpec * pspec);
|
GValue * value, GParamSpec * pspec);
|
||||||
|
|
||||||
static gboolean gst_camera_undistort_set_info (GstOpencvVideoFilter * cvfilter,
|
static gboolean gst_camera_undistort_set_info (GstOpencvVideoFilter * cvfilter,
|
||||||
gint in_width, gint in_height, int in_cv_type,
|
gint in_width, gint in_height, int in_cv_type,
|
||||||
gint out_width, gint out_height, int out_cv_type);
|
gint out_width, gint out_height, int out_cv_type);
|
||||||
static GstFlowReturn gst_camera_undistort_transform_frame (GstOpencvVideoFilter
|
static GstFlowReturn gst_camera_undistort_transform_frame (GstOpencvVideoFilter
|
||||||
* cvfilter, GstBuffer * frame, cv::Mat img, GstBuffer * outframe,
|
* cvfilter, GstBuffer * frame, cv::Mat img, GstBuffer * outframe,
|
||||||
cv::Mat outimg);
|
cv::Mat outimg);
|
||||||
|
|
||||||
static gboolean gst_camera_undistort_sink_event (GstBaseTransform * trans,
|
static gboolean gst_camera_undistort_sink_event (GstBaseTransform * trans,
|
||||||
GstEvent * event);
|
GstEvent * event);
|
||||||
static gboolean gst_camera_undistort_src_event (GstBaseTransform * trans,
|
static gboolean gst_camera_undistort_src_event (GstBaseTransform * trans,
|
||||||
GstEvent * event);
|
GstEvent * event);
|
||||||
|
|
||||||
static void camera_undistort_run (GstCameraUndistort * undist, cv::Mat img,
|
static void camera_undistort_run (GstCameraUndistort * undist, cv::Mat img,
|
||||||
cv::Mat outimg);
|
cv::Mat outimg);
|
||||||
static gboolean camera_undistort_init_undistort_rectify_map (GstCameraUndistort
|
static gboolean camera_undistort_init_undistort_rectify_map (GstCameraUndistort
|
||||||
* undist);
|
* undist);
|
||||||
|
|
||||||
/* initialize the cameraundistort's class */
|
/* initialize the cameraundistort's class */
|
||||||
static void
|
static void
|
||||||
gst_camera_undistort_class_init (GstCameraUndistortClass * klass)
|
gst_camera_undistort_class_init (GstCameraUndistortClass * klass)
|
||||||
{
|
{
|
||||||
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
||||||
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
||||||
GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
|
GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
|
||||||
@ -185,14 +183,14 @@
|
|||||||
gst_element_class_add_pad_template (element_class, templ);
|
gst_element_class_add_pad_template (element_class, templ);
|
||||||
templ = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps);
|
templ = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps);
|
||||||
gst_element_class_add_pad_template (element_class, templ);
|
gst_element_class_add_pad_template (element_class, templ);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* initialize the new element
|
/* initialize the new element
|
||||||
* initialize instance structure
|
* initialize instance structure
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
gst_camera_undistort_init (GstCameraUndistort * undist)
|
gst_camera_undistort_init (GstCameraUndistort * undist)
|
||||||
{
|
{
|
||||||
undist->showUndistorted = DEFAULT_SHOW_UNDISTORTED;
|
undist->showUndistorted = DEFAULT_SHOW_UNDISTORTED;
|
||||||
undist->alpha = DEFAULT_ALPHA;
|
undist->alpha = DEFAULT_ALPHA;
|
||||||
undist->crop = DEFAULT_CROP;
|
undist->crop = DEFAULT_CROP;
|
||||||
@ -206,24 +204,24 @@
|
|||||||
undist->map2 = 0;
|
undist->map2 = 0;
|
||||||
|
|
||||||
undist->settings = NULL;
|
undist->settings = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
gst_camera_undistort_dispose (GObject * object)
|
gst_camera_undistort_dispose (GObject * object)
|
||||||
{
|
{
|
||||||
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (object);
|
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (object);
|
||||||
|
|
||||||
g_free (undist->settings);
|
g_free (undist->settings);
|
||||||
undist->settings = NULL;
|
undist->settings = NULL;
|
||||||
|
|
||||||
G_OBJECT_CLASS (gst_camera_undistort_parent_class)->dispose (object);
|
G_OBJECT_CLASS (gst_camera_undistort_parent_class)->dispose (object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
gst_camera_undistort_set_property (GObject * object, guint prop_id,
|
gst_camera_undistort_set_property (GObject * object, guint prop_id,
|
||||||
const GValue * value, GParamSpec * pspec)
|
const GValue * value, GParamSpec * pspec)
|
||||||
{
|
{
|
||||||
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (object);
|
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (object);
|
||||||
const char *str;
|
const char *str;
|
||||||
|
|
||||||
@ -253,12 +251,12 @@
|
|||||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
gst_camera_undistort_get_property (GObject * object, guint prop_id,
|
gst_camera_undistort_get_property (GObject * object, guint prop_id,
|
||||||
GValue * value, GParamSpec * pspec)
|
GValue * value, GParamSpec * pspec)
|
||||||
{
|
{
|
||||||
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (object);
|
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (object);
|
||||||
|
|
||||||
switch (prop_id) {
|
switch (prop_id) {
|
||||||
@ -278,39 +276,39 @@
|
|||||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
gst_camera_undistort_set_info (GstOpencvVideoFilter * cvfilter,
|
gst_camera_undistort_set_info (GstOpencvVideoFilter * cvfilter,
|
||||||
gint in_width, gint in_height, G_GNUC_UNUSED int in_cv_type,
|
gint in_width, gint in_height, G_GNUC_UNUSED int in_cv_type,
|
||||||
G_GNUC_UNUSED gint out_width, G_GNUC_UNUSED gint out_height,
|
G_GNUC_UNUSED gint out_width, G_GNUC_UNUSED gint out_height,
|
||||||
G_GNUC_UNUSED int out_cv_type)
|
G_GNUC_UNUSED int out_cv_type)
|
||||||
{
|
{
|
||||||
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (cvfilter);
|
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (cvfilter);
|
||||||
|
|
||||||
undist->imageSize = cv::Size (in_width, in_height);
|
undist->imageSize = cv::Size (in_width, in_height);
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Performs the camera undistort
|
* Performs the camera undistort
|
||||||
*/
|
*/
|
||||||
static GstFlowReturn
|
static GstFlowReturn
|
||||||
gst_camera_undistort_transform_frame (GstOpencvVideoFilter * cvfilter,
|
gst_camera_undistort_transform_frame (GstOpencvVideoFilter * cvfilter,
|
||||||
G_GNUC_UNUSED GstBuffer * frame, cv::Mat img,
|
G_GNUC_UNUSED GstBuffer * frame, cv::Mat img,
|
||||||
G_GNUC_UNUSED GstBuffer * outframe, cv::Mat outimg)
|
G_GNUC_UNUSED GstBuffer * outframe, cv::Mat outimg)
|
||||||
{
|
{
|
||||||
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (cvfilter);
|
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (cvfilter);
|
||||||
|
|
||||||
camera_undistort_run (undist, img, outimg);
|
camera_undistort_run (undist, img, outimg);
|
||||||
|
|
||||||
return GST_FLOW_OK;
|
return GST_FLOW_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
camera_undistort_run (GstCameraUndistort * undist, cv::Mat img, cv::Mat outimg)
|
camera_undistort_run (GstCameraUndistort * undist, cv::Mat img, cv::Mat outimg)
|
||||||
{
|
{
|
||||||
/* TODO is settingsChanged handling thread safe ? */
|
/* TODO is settingsChanged handling thread safe ? */
|
||||||
if (undist->settingsChanged) {
|
if (undist->settingsChanged) {
|
||||||
/* settings have changed, need to recompute undistort */
|
/* settings have changed, need to recompute undistort */
|
||||||
@ -326,19 +324,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (undist->showUndistorted && undist->doUndistort) {
|
if (undist->showUndistorted && undist->doUndistort) {
|
||||||
|
|
||||||
/* Declare input and output images as UMat to enable potential OpenCL acceleration. */
|
|
||||||
/* UMat allows OpenCV to automatically use GPU if available and configured. */
|
|
||||||
cv::UMat cv_input_umat, cv_output_umat;
|
|
||||||
|
|
||||||
/* convert Mat to UMat */
|
|
||||||
img.copyTo(cv_input_umat);
|
|
||||||
|
|
||||||
/* do the undistort */
|
/* do the undistort */
|
||||||
cv::remap (cv_input_umat, cv_output_umat, undist->map1, undist->map2, cv::INTER_LINEAR);
|
cv::remap (img, outimg, undist->map1, undist->map2, cv::INTER_LINEAR);
|
||||||
|
|
||||||
/* UMat output back to Mat */
|
|
||||||
cv_output_umat.copyTo(outimg);
|
|
||||||
|
|
||||||
if (undist->crop) {
|
if (undist->crop) {
|
||||||
/* TODO do the cropping */
|
/* TODO do the cropping */
|
||||||
@ -349,12 +336,12 @@
|
|||||||
/* FIXME should use pass through to avoid this copy when not undistorting */
|
/* FIXME should use pass through to avoid this copy when not undistorting */
|
||||||
img.copyTo (outimg);
|
img.copyTo (outimg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* compute undistort */
|
/* compute undistort */
|
||||||
static gboolean
|
static gboolean
|
||||||
camera_undistort_init_undistort_rectify_map (GstCameraUndistort * undist)
|
camera_undistort_init_undistort_rectify_map (GstCameraUndistort * undist)
|
||||||
{
|
{
|
||||||
cv::Size newImageSize;
|
cv::Size newImageSize;
|
||||||
cv::Rect validPixROI;
|
cv::Rect validPixROI;
|
||||||
cv::Mat newCameraMatrix =
|
cv::Mat newCameraMatrix =
|
||||||
@ -367,12 +354,12 @@
|
|||||||
undist->map2);
|
undist->map2);
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
camera_undistort_calibration_event (GstCameraUndistort * undist,
|
camera_undistort_calibration_event (GstCameraUndistort * undist,
|
||||||
GstEvent * event)
|
GstEvent * event)
|
||||||
{
|
{
|
||||||
g_free (undist->settings);
|
g_free (undist->settings);
|
||||||
|
|
||||||
if (!gst_camera_event_parse_calibrated (event, &(undist->settings))) {
|
if (!gst_camera_event_parse_calibrated (event, &(undist->settings))) {
|
||||||
@ -382,11 +369,11 @@
|
|||||||
undist->settingsChanged = TRUE;
|
undist->settingsChanged = TRUE;
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
gst_camera_undistort_sink_event (GstBaseTransform * trans, GstEvent * event)
|
gst_camera_undistort_sink_event (GstBaseTransform * trans, GstEvent * event)
|
||||||
{
|
{
|
||||||
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (trans);
|
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (trans);
|
||||||
|
|
||||||
const GstStructure *structure = gst_event_get_structure (event);
|
const GstStructure *structure = gst_event_get_structure (event);
|
||||||
@ -401,11 +388,11 @@
|
|||||||
return
|
return
|
||||||
GST_BASE_TRANSFORM_CLASS (gst_camera_undistort_parent_class)->sink_event
|
GST_BASE_TRANSFORM_CLASS (gst_camera_undistort_parent_class)->sink_event
|
||||||
(trans, event);
|
(trans, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
gst_camera_undistort_src_event (GstBaseTransform * trans, GstEvent * event)
|
gst_camera_undistort_src_event (GstBaseTransform * trans, GstEvent * event)
|
||||||
{
|
{
|
||||||
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (trans);
|
GstCameraUndistort *undist = GST_CAMERA_UNDISTORT (trans);
|
||||||
|
|
||||||
const GstStructure *structure = gst_event_get_structure (event);
|
const GstStructure *structure = gst_event_get_structure (event);
|
||||||
@ -420,5 +407,4 @@
|
|||||||
return
|
return
|
||||||
GST_BASE_TRANSFORM_CLASS (gst_camera_undistort_parent_class)->src_event
|
GST_BASE_TRANSFORM_CLASS (gst_camera_undistort_parent_class)->src_event
|
||||||
(trans, event);
|
(trans, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user