mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2025-06-07 07:58:51 +00:00
opencv: account for sparse/padded formats when converting caps to cv image type
https://bugzilla.gnome.org/show_bug.cgi?id=774576
This commit is contained in:
parent
cfc420bf4d
commit
f6021e0016
2 changed files with 56 additions and 28 deletions
|
@ -26,17 +26,19 @@
|
||||||
#include "gstopencvutils.h"
|
#include "gstopencvutils.h"
|
||||||
#include <opencv2/core/core_c.h>
|
#include <opencv2/core/core_c.h>
|
||||||
|
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
gst_opencv_parse_iplimage_params_from_caps (GstCaps * caps, gint * width,
|
gst_opencv_parse_iplimage_params_from_caps (GstCaps * caps, gint * width,
|
||||||
gint * height, gint * ipldepth, gint * channels, GError ** err)
|
gint * height, gint * ipldepth, gint * channels, GError ** err)
|
||||||
{
|
{
|
||||||
GstVideoInfo info;
|
GstVideoInfo info;
|
||||||
|
gchar *caps_str;
|
||||||
|
|
||||||
if (!gst_video_info_from_caps (&info, caps)) {
|
if (!gst_video_info_from_caps (&info, caps)) {
|
||||||
GST_ERROR ("Failed to get the videoinfo from caps");
|
caps_str = gst_caps_to_string (caps);
|
||||||
|
GST_ERROR ("Failed to get video info from caps %s", caps_str);
|
||||||
g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION,
|
g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION,
|
||||||
"No width/heighti/depth/channels in caps");
|
"Failed to get video info from caps %s", caps_str);
|
||||||
|
g_free (caps_str);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,35 +50,58 @@ gboolean
|
||||||
gst_opencv_iplimage_params_from_video_info (GstVideoInfo * info, gint * width,
|
gst_opencv_iplimage_params_from_video_info (GstVideoInfo * info, gint * width,
|
||||||
gint * height, gint * ipldepth, gint * channels, GError ** err)
|
gint * height, gint * ipldepth, gint * channels, GError ** err)
|
||||||
{
|
{
|
||||||
gint depth = 0;
|
GstVideoFormat format;
|
||||||
guint i;
|
int cv_type;
|
||||||
|
|
||||||
|
format = GST_VIDEO_INFO_FORMAT (info);
|
||||||
|
if (!gst_opencv_cv_image_type_from_video_format (format, &cv_type, err)) {
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
*width = GST_VIDEO_INFO_WIDTH (info);
|
*width = GST_VIDEO_INFO_WIDTH (info);
|
||||||
*height = GST_VIDEO_INFO_HEIGHT (info);
|
*height = GST_VIDEO_INFO_HEIGHT (info);
|
||||||
if (GST_VIDEO_INFO_IS_RGB (info))
|
|
||||||
*channels = 3;
|
*ipldepth = cvIplDepth (cv_type);
|
||||||
else if (GST_VIDEO_INFO_IS_GRAY (info))
|
*channels = CV_MAT_CN (cv_type);
|
||||||
*channels = 1;
|
|
||||||
else {
|
return TRUE;
|
||||||
g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION,
|
}
|
||||||
"Unsupported video format %s",
|
|
||||||
gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (info)));
|
gboolean
|
||||||
return FALSE;
|
gst_opencv_cv_image_type_from_video_format (GstVideoFormat format,
|
||||||
|
int * cv_type, GError ** err)
|
||||||
|
{
|
||||||
|
const gchar *format_str;
|
||||||
|
|
||||||
|
switch (format) {
|
||||||
|
case GST_VIDEO_FORMAT_GRAY8:
|
||||||
|
*cv_type = CV_8UC1;
|
||||||
|
break;
|
||||||
|
case GST_VIDEO_FORMAT_RGB:
|
||||||
|
case GST_VIDEO_FORMAT_BGR:
|
||||||
|
*cv_type = CV_8UC3;
|
||||||
|
break;
|
||||||
|
case GST_VIDEO_FORMAT_RGBx:
|
||||||
|
case GST_VIDEO_FORMAT_xRGB:
|
||||||
|
case GST_VIDEO_FORMAT_BGRx:
|
||||||
|
case GST_VIDEO_FORMAT_xBGR:
|
||||||
|
case GST_VIDEO_FORMAT_RGBA:
|
||||||
|
case GST_VIDEO_FORMAT_ARGB:
|
||||||
|
case GST_VIDEO_FORMAT_BGRA:
|
||||||
|
case GST_VIDEO_FORMAT_ABGR:
|
||||||
|
*cv_type = CV_8UC4;
|
||||||
|
break;
|
||||||
|
case GST_VIDEO_FORMAT_GRAY16_LE:
|
||||||
|
case GST_VIDEO_FORMAT_GRAY16_BE:
|
||||||
|
*cv_type = CV_16UC1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
format_str = gst_video_format_to_string (format);
|
||||||
|
g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION,
|
||||||
|
"Unsupported video format %s", format_str);
|
||||||
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < GST_VIDEO_INFO_N_COMPONENTS (info); i++)
|
|
||||||
depth += GST_VIDEO_INFO_COMP_DEPTH (info, i);
|
|
||||||
|
|
||||||
if (depth / *channels == 8) {
|
|
||||||
/* TODO signdness? */
|
|
||||||
*ipldepth = IPL_DEPTH_8U;
|
|
||||||
} else if (depth / *channels == 16) {
|
|
||||||
*ipldepth = IPL_DEPTH_16U;
|
|
||||||
} else {
|
|
||||||
g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION,
|
|
||||||
"Unsupported depth/channels %d/%d", depth, *channels);
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,6 @@
|
||||||
#include <gst/gst.h>
|
#include <gst/gst.h>
|
||||||
#include <gst/video/video.h>
|
#include <gst/video/video.h>
|
||||||
|
|
||||||
|
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
gboolean gst_opencv_parse_iplimage_params_from_caps
|
gboolean gst_opencv_parse_iplimage_params_from_caps
|
||||||
|
@ -40,7 +39,11 @@ gboolean gst_opencv_iplimage_params_from_video_info
|
||||||
(GstVideoInfo * info, gint * width, gint * height, gint * depth,
|
(GstVideoInfo * info, gint * width, gint * height, gint * depth,
|
||||||
gint * channels, GError ** err);
|
gint * channels, GError ** err);
|
||||||
|
|
||||||
|
gboolean gst_opencv_cv_image_type_from_video_format (GstVideoFormat format,
|
||||||
|
int * cv_type, GError ** err);
|
||||||
|
|
||||||
GstCaps * gst_opencv_caps_from_cv_image_type (int cv_type);
|
GstCaps * gst_opencv_caps_from_cv_image_type (int cv_type);
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
|
|
||||||
#endif /* __GST_OPENCV_UTILS__ */
|
#endif /* __GST_OPENCV_UTILS__ */
|
||||||
|
|
Loading…
Reference in a new issue