Segmentation Fault When OpenCV Program run as a Video Filter Plugin for VLC
I Downloaded the OpenCV 3.1 source code and compiled it using MinGW. Make Files are generated using CMake. I also turned on the CMake_GNUtoMS option CMake so that .lib files are generated along with .a files.
Now i have written a VLC OpenCV filter which conatins Code for FaceDetection (reference). This filter compiles fine (linked to opencv_world310.lib) but when i run a test program which tells VLC to use this Filter i get a Segmentation Fault at cv::CascadeClassifier::load(cv::String const&)
. I have tried running it after linking this Filter against opencv_world310.dll.a but the same Error persists.
Filter code:
using namespace cv;
struct filter_sys_t
{
CascadeClassifier detector;
};
static int OpenFilter ( vlc_object_t * );
static void CloseFilter( vlc_object_t * );
static picture_t *Filter( filter_t *, picture_t * );
vlc_module_begin ()
// Set vlc Description, Shortname, Capability, Category .etc
set_callbacks( OpenFilter, CloseFilter )
vlc_module_end ()
static int OpenFilter( vlc_object_t *p_this )
{
filter_t *p_filter = (filter_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 )
{
return VLC_ENOMEM;
}
//init the video_filter_event_info_t struct
//create the VIDEO_FILTER_EVENT_VARIABLE
cv::String f = "C:\\OpenCV\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
msg_Dbg(p_filter, "going to load detector");
bool loaded = p_sys->detector.load(f);
msg_Dbg( p_filter, "detector loaded %d", loaded);
if (!loaded)
{
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
/*****************************************************************************
* CloseFilter: clean up the filter
*****************************************************************************/
static void CloseFilter( vlc_object_t *p_this )
{
filter_t *p_filter = (filter_t*)p_this;
filter_sys_t *p_sys = p_filter->p_sys;
free( p_sys );
var_Destroy( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE);
}
/****************************************************************************
* Filter: Check for faces and raises an event when one is found.
****************************************************************************/
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
{
msg_Dbg( p_filter, "Entered Filter()");
int groundThreshold = 2;
double scaleStep = 1.1;
Size minimalObjectSize(80, 80);
Size maximalObjectSize(200, 200);
IplImage** p_img = NULL;
filter_sys_t *p_sys = p_filter->p_sys;
if ((!p_pic) )
{
msg_Err( p_filter, "no image array" );
return NULL;
}
//cast the picture_t to array of IplImage*
p_img = (IplImage**) p_pic;
//check the image array for validity
if ((!p_img[0])) //1st plane is 'I' i.e. greyscale
{
msg_Err( p_filter, "no image" );
return NULL;
}
msg_Dbg( p_filter, "Going to convert iplImage to MAT");
Mat img = cvarrToMat(p_img);
Mat image_grey;
// Convert input to greyscale
cvtColor(img, image_grey, CV_BGR2GRAY);
//perform face detection
std::vector<Rect> found;
msg_Dbg( p_filter, "Going to detect face");
//// Detect faces
p_sys->detector.detectMultiScale(image_grey, found, scaleStep, groundThreshold, 0 | 2, minimalObjectSize,maximalObjectSize);
////// Draw the results into mat
if (found.size() > 0) {
msg_Dbg( p_filter, "Faces Found: %d", found.size());
for (int i = 0; i <= found.size() - 1; i++) {
rectangle(img, found[i].br(), found[i].tl(), Scalar(0, 255, 0), 1, 8, 0);
}
}
return p_pic;
}
BUT when i run the FaceDetection Code as a independent program (linked against the same .lib/.a and Using Same opencv_world310.dll) it Works Good.
ERROR that i get:
Program received signal SIGSEGV, Segmentation fault.
0x0c300bcd in cv::CascadeClassifier::load(cv::String const&) ()
from C:\Windows\SysWOW64\libopencv_world310.dll
(gdb) bt
#0 0x0c300bcd in cv::CascadeClassifier::load(cv::String const&) ()
from C:\Windows\SysWOW64\libopencv_world310.dll
#1 0x088593a8 in ?? ()
#2 0x6a7bef8a in module_need ()
from C:\test\libvlccore.dll
#3 0x0925a2c1 in vlc_entry_license__1_1_0g ()
from C:\test\libopencv_wrapper_plugin.dll
#4 0x00000001 in ...