Ask Your Question

ajmartin's profile - activity

2015-11-01 07:03:14 -0600 asked a question Segmentation Fault with OpenCV 3.0.0 on Windows 8.1 with MinGW

I have a simple multi-threaded version with TBB which works fine on a linux machine but on occasions, not always, gives segmentation fault when executed on windows.

#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <math.h>
#include <dirent.h>
#include <string.h>
#include <chrono>
using namespace cv;

std::vector<std::string> fnames;
class Parallel_process : public cv::ParallelLoopBody
{
    public:
        Parallel_process() {}

        void operator()(const cv::Range &r) const {
            cv::Mat src, dst, color_dst;
                if(!(src=imread(fnames[r.start], 0)).data)
            {
                return;
            }
            Canny( src, dst, 50, 200, 3 );
            cvtColor( dst, color_dst, CV_GRAY2BGR );

            std::vector<Vec4i> lines;
            HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
            for( size_t i = 0; i < lines.size(); i++ )
                    line( color_dst, Point(lines[i][0], lines[i][1]), Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
            std::string ext = "_parallel.bmp";  
            std::size_t found = fnames[r.start].find_last_of("/\\");
            std::string fname = fnames[r.start].substr(found+1);
            std::string out_fname = fname.substr(0, fname.find(".")) + ext;
            cv::imwrite(out_fname.c_str(), color_dst);
        }
};

int main()
{
    DIR *dir;
    struct dirent *ent;
    std::string prefix = "c:\\Users\\foo\\Desktop\\sample_code\\imgs";
    if ((dir = opendir (prefix.c_str())) != NULL) {
        while ((ent = readdir (dir)) != NULL) {
            fnames.push_back(prefix + '\\' + std::string(ent->d_name));
        }
        closedir (dir);
    } 
    auto begin = std::chrono::high_resolution_clock::now();
    cv::parallel_for_(cv::Range(0, fnames.size()), Parallel_process());
    auto end = std::chrono::high_resolution_clock::now();
    std::cout<<std::chrono::duration_cast<std::chrono::seconds>(end-begin).count()<<"secs"<<std::endl;
    return(0);
}

Compilation is done using:

g++ -o parallel -I"c:\opencv\mingw_bld\install\include" edge_detection_parallel.cpp -L"c:\opencv\mingw_bld\install\x86\mingw\lib" -lopencv_imgproc300 -lopencv_core300 -lopencv_imgcodecs300 -lopencv_highgui300 -std=c++11

Compiler version: g++ (GCC) 4.8.1

TBB version:

#ifndef __TBB_tbb_stddef_H
#define __TBB_tbb_stddef_H

// Marketing-driven product version
#define TBB_VERSION_MAJOR 4
#define TBB_VERSION_MINOR 4

// Engineering-focused interface version
#define TBB_INTERFACE_VERSION 9000
#define TBB_INTERFACE_VERSION_MAJOR TBB_INTERFACE_VERSION/1000

// The oldest major interface version still supported
// To be used in SONAME, manifests, etc.
#define TBB_COMPATIBLE_INTERFACE_VERSION 2

GDB output (two runs; one was fine and the other received the segmentation fault:

gdb parallel.exe
GNU gdb (GDB) 7.6.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from C:\Users\foo\Desktop\sample_code\parallel.exe...(no
debugging symbols found)...done.
(gdb) r
Starting program: C:\Users\foo\Desktop\sample_code/parallel.exe
[New Thread 5788.0xb90]
[New Thread 5788.0x144c]
[New Thread 5788.0x168c]
[New Thread 5788.0xd1c]
30secs
[Inferior 1 (process 5788) exited normally]
(gdb) r
Starting program: C:\Users\foo\Desktop\sample_code/parallel.exe
[New Thread 620.0x1450]
[New Thread 620.0xf04]
[New Thread 620.0x100c]
[New Thread 620.0x159c]

Program received signal SIGSEGV, Segmentation fault.
[Switching ...
(more)