Merge branch 'opencv4' into 'master'

Compile opencv modules in C++ mode, detect OpenCV4 pkgconfig

See merge request videolan/vlc!927
This commit is contained in:
StefanBruens 2024-04-28 07:11:06 +00:00
commit 5c2efda0b1
5 changed files with 89 additions and 74 deletions

View File

@ -2129,7 +2129,32 @@ PKG_ENABLE_MODULES_VLC([BLURAY], [libbluray], [libbluray >= 0.6.2], (libbluray f
dnl
dnl OpenCV wrapper and example filters
dnl
PKG_ENABLE_MODULES_VLC([OPENCV], [opencv_example opencv_wrapper], [opencv > 2.0], (OpenCV (computer vision) filter), [auto])
AC_ARG_ENABLE(opencv,
[AS_HELP_STRING([--enable-opencv],
[OpenCV (computer vision) filter (default auto)])])
AS_IF([test "${enable_opencv}" != "no"], [
have_opencv="no"
PKG_CHECK_MODULES([OPENCV], [opencv4], [
have_opencv="yes"
], [
PKG_CHECK_MODULES([OPENCV], [opencv > 2.0], [
have_opencv="yes"
], [
AS_IF([test -n "${enable_opencv}"], [
AC_MSG_ERROR([${OPENCV_PKG_ERRORS}.])
])
])
])
AS_IF([test "${have_opencv}" != "no"], [
VLC_ADD_PLUGIN([opencv_example opencv_wrapper])
VLC_ADD_LIBS([opencv_example opencv_wrapper], [$OPENCV_LIBS])
VLC_ADD_CFLAGS([opencv_example opencv_wrapper], [$OPENCV_CFLAGS])
], [
AC_MSG_WARN([${OPENCV_PKG_ERRORS}.])
enable_opencv="no"
])
])
AM_CONDITIONAL(HAVE_OPENCV, [test "$enable_opencv" != "no"])
dnl

View File

@ -204,7 +204,7 @@ video_filter_LTLIBRARIES += libglblend_plugin.la
endif
endif
libopencv_wrapper_plugin_la_SOURCES = video_filter/opencv_wrapper.c
libopencv_wrapper_plugin_la_SOURCES = video_filter/opencv_wrapper.cpp
libopencv_wrapper_plugin_la_CPPFLAGS = $(AM_CPPFLAGS) $(OPENCV_CFLAGS)
libopencv_wrapper_plugin_la_LIBADD = $(OPENCV_LIBS)
libopencv_wrapper_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(video_filterdir)'

View File

@ -40,11 +40,8 @@
#include <vlc_image.h>
#include "filter_event_info.h"
#include <opencv2/opencv.hpp>
#include <opencv2/core/core_c.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
/*****************************************************************************
* filter_sys_t : filter descriptor
@ -52,10 +49,11 @@
namespace {
struct filter_sys_t
class filter_sys_t
{
CvMemStorage* p_storage;
CvHaarClassifierCascade* p_cascade;
public:
cv::CascadeClassifier cascade;
std::vector<video_filter_region_info_t> event_info_storage;
video_filter_event_info_t event_info;
int i_id;
};
@ -96,15 +94,15 @@ static int OpenFilter( vlc_object_t *p_this )
filter_sys_t *p_sys;
/* Allocate the memory needed to store the decoder's structure */
if( ( p_filter->p_sys = p_sys =
(filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
p_filter->p_sys = p_sys = new (std::nothrow) filter_sys_t;
if (p_sys == nullptr)
{
return VLC_ENOMEM;
}
//init the video_filter_event_info_t struct
p_sys->event_info.i_region_size = 0;
p_sys->event_info.p_region = NULL;
p_sys->event_info.p_region = nullptr;
p_sys->i_id = 0;
static const struct FilterOperationInitializer {
@ -128,8 +126,9 @@ static int OpenFilter( vlc_object_t *p_this )
//OpenCV init specific to this example
char* filename = var_InheritString( p_filter, "opencv-haarcascade-file" );
p_sys->p_cascade = (CvHaarClassifierCascade*)cvLoad( filename, 0, 0, 0 );
p_sys->p_storage = cvCreateMemStorage(0);
if (!p_sys->cascade.load(filename)) {
msg_Err( p_filter, "Could not load %s", filename);
}
free( filename );
return VLC_SUCCESS;
@ -142,14 +141,7 @@ static void CloseFilter( filter_t *p_filter )
{
filter_sys_t *p_sys = static_cast<filter_sys_t *>(p_filter->p_sys);
if( p_sys->p_cascade )
cvReleaseHaarClassifierCascade( &p_sys->p_cascade );
if( p_sys->p_storage )
cvReleaseMemStorage( &p_sys->p_storage );
free( p_sys->event_info.p_region );
free( p_sys );
delete p_sys;
var_Destroy( vlc_object_instance(p_filter), VIDEO_FILTER_EVENT_VARIABLE);
}
@ -159,15 +151,13 @@ static void CloseFilter( filter_t *p_filter )
****************************************************************************/
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
{
IplImage** p_img = NULL;
CvPoint pt1, pt2;
int scale = 1;
IplImage** p_img = nullptr;
filter_sys_t *p_sys = static_cast<filter_sys_t *>(p_filter->p_sys);
if ((!p_pic) )
{
msg_Err( p_filter, "no image array" );
return NULL;
return nullptr;
}
//(hack) cast the picture_t to array of IplImage*
p_img = (IplImage**) p_pic;
@ -176,50 +166,50 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
if ((!p_img[0])) //1st plane is 'I' i.e. greyscale
{
msg_Err( p_filter, "no image" );
return NULL;
return nullptr;
}
// Wrapper for IplImage* -> cv::Mat (reference, not copy)
cv::Mat gray = cv::cvarrToMat(p_img[0]);
//perform face detection
cvClearMemStorage(p_sys->p_storage);
if( p_sys->p_cascade )
{
//we should make some of these params config variables
CvSeq *faces = cvHaarDetectObjects( p_img[0], p_sys->p_cascade,
p_sys->p_storage, 1.15, 5,
CV_HAAR_DO_CANNY_PRUNING,
cvSize(20, 20) );
//create the video_filter_region_info_t struct
if (faces && (faces->total > 0))
{
//msg_Dbg( p_filter, "Found %d face(s)", faces->total );
free( p_sys->event_info.p_region );
p_sys->event_info.p_region = (video_filter_region_info_t*)
calloc( faces->total, sizeof(video_filter_region_info_t));
if( !p_sys->event_info.p_region )
return NULL;
p_sys->event_info.i_region_size = faces->total;
}
//populate the video_filter_region_info_t struct
for( int i = 0; i < (faces ? faces->total : 0); i++ )
{
CvRect *r = (CvRect*)cvGetSeqElem( faces, i );
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
cvRectangle( p_img[0], pt1, pt2, CV_RGB(0,0,0), 3, 8, 0 );
*(CvRect*)(&(p_sys->event_info.p_region[i])) = *r;
p_sys->event_info.p_region[i].i_id = p_sys->i_id++;
p_sys->event_info.p_region[i].p_description = "Face Detected";
}
if (faces && (faces->total > 0)) //raise the video filter event
var_TriggerCallback( vlc_object_instance(p_filter), VIDEO_FILTER_EVENT_VARIABLE );
}
else
if (p_sys->cascade.empty()) {
msg_Err( p_filter, "No cascade - is opencv-haarcascade-file valid?" );
return p_pic;
}
//we should make some of these params config variables
std::vector<cv::Rect> faces;
p_sys->cascade.detectMultiScale(gray, faces,
1.15, 5,
cv::CASCADE_DO_CANNY_PRUNING,
cv::Size(20, 20));
//create the video_filter_region_info_t struct
if (faces.empty())
return p_pic;
//msg_Dbg( p_filter, "Found %d face(s)", faces.size());
p_sys->event_info_storage.resize(faces.size());
p_sys->event_info.i_region_size = faces.size();
p_sys->event_info.p_region = p_sys->event_info_storage.data();
if( !p_sys->event_info.p_region )
return nullptr;
//populate the video_filter_region_info_t struct
for( size_t i = 0; i < faces.size(); i++ )
{
const cv::Rect& r = faces[i];
cv::rectangle(gray, r, CV_RGB(0,0,0), 3);
p_sys->event_info.p_region[i].i_x = r.x;
p_sys->event_info.p_region[i].i_y = r.y;
p_sys->event_info.p_region[i].i_width = r.width;
p_sys->event_info.p_region[i].i_height = r.height;
p_sys->event_info.p_region[i].i_id = p_sys->i_id++;
p_sys->event_info.p_region[i].p_description = "Face Detected";
}
var_TriggerCallback( vlc_object_instance(p_filter), VIDEO_FILTER_EVENT_VARIABLE );
return p_pic;
}

View File

@ -1,5 +1,5 @@
/*****************************************************************************
* opencv_wrapper.c : OpenCV wrapper video filter
* opencv_wrapper.cpp : OpenCV wrapper video filter
*****************************************************************************
* Copyright (C) 2006-2012 VLC authors and VideoLAN
* Copyright (C) 2012 Edward Wang
@ -146,8 +146,8 @@ static int Create( filter_t* p_filter )
char *psz_chroma, *psz_output;
/* Allocate structure */
p_sys = malloc( sizeof( filter_sys_t ) );
if( p_sys == NULL )
p_sys = new (std::nothrow) filter_sys_t;
if( !p_sys )
return VLC_ENOMEM;
/* Load the internal OpenCV filter.
@ -159,9 +159,9 @@ static int Create( filter_t* p_filter )
* We don't need to set up video formats for this filter as it not
* actually using a picture_t.
*/
p_sys->p_opencv = vlc_object_create( p_filter, sizeof(filter_t) );
p_sys->p_opencv = static_cast<filter_t*>( vlc_object_create( p_filter, sizeof(filter_t) ) );
if( !p_sys->p_opencv ) {
free( p_sys );
delete p_sys;
return VLC_ENOMEM;
}
@ -178,7 +178,7 @@ static int Create( filter_t* p_filter )
msg_Err( p_filter, "can't open internal opencv filter: %s", psz_inner_name );
free( psz_inner_name );
vlc_object_delete(p_sys->p_opencv);
free( p_sys );
delete p_sys;
return VLC_ENOENT;
}
@ -270,7 +270,7 @@ static void Destroy( filter_t* p_filter )
module_unneed( p_sys->p_opencv, p_sys->p_opencv->p_module );
vlc_object_delete(p_sys->p_opencv);
free( p_sys );
delete p_sys;
}
/*****************************************************************************

View File

@ -1329,7 +1329,7 @@ modules/video_filter/motionblur.c
modules/video_filter/motiondetect.c
modules/video_filter/oldmovie.c
modules/video_filter/opencv_example.cpp
modules/video_filter/opencv_wrapper.c
modules/video_filter/opencv_wrapper.cpp
modules/video_filter/posterize.c
modules/video_filter/postproc.c
modules/video_filter/psychedelic.c