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 to Thread 620.0x159c]
0x777cd9e5 in ?? ()
(gdb) bt
#0 0x777cd9e5 in ?? ()
#1 0x777cd3d8 in ?? ()
#2 0x7787184a in ?? ()
#3 0x778319c7 in ?? ()
#4 0x777cd3d8 in ?? ()
#5 0x7541b0f9 in msvcrt!free () from C:\Windows\system32\msvcrt.dll
#6 0x00740000 in ?? ()
#7 0x6c372f3b in cv::Canny(cv::_InputArray const&, cv::_OutputArray const&, dou
ble, double, int, bool) ()
from C:\opencv\mingw_bld\bin\libopencv_imgproc300.dll
#8 0x00408ef6 in Parallel_process::operator()(cv::Range const&) const ()
#9 0x69824e46 in tbb::interface7::internal::start_for<tbb::blocked_range<int>,
(anonymous namespace)::ProxyLoopBody, tbb::auto_partitioner const>::execute()
() from C:\opencv\mingw_bld\bin\libopencv_core300.dll
#10 0x678d7e49 in tbb::internal::custom_scheduler<tbb::internal::IntelSchedulerT
raits>::local_wait_for_all (this=0x913e80, parent=..., child=0x947620)
at ../../src/tbb/custom_scheduler.h:463
#11 0x678cc94f in tbb::internal::arena::process (this=this@entry=0x93ed00,
s=...) at ../../src/tbb/arena.cpp:114
#12 0x678cbcc1 in tbb::internal::market::process (this=<optimized out>, j=...)
at ../../src/tbb/market.cpp:494
#13 0x678c86dc in tbb::internal::rml::private_worker::run (
this=this@entry=0x93fc00) at ../../src/tbb/private_server.cpp:275
#14 0x678c8893 in tbb::internal::rml::private_worker::thread_routine (
arg=0x93fc00) at ../../src/tbb/private_server.cpp:228
#15 0x75427fb0 in msvcrt!_cexit () from C:\Windows\system32\msvcrt.dll
#16 0x0093fc00 in ?? ()
#17 0x754280f5 in msvcrt!_beginthreadex () from C:\Windows\system32\msvcrt.dll
#18 0x75b04198 in KERNEL32!BaseThreadInitThunk ()
from C:\Windows\system32\kernel32.dll
#19 0x777e445d in ?? ()
#20 0x777e442b in ?? ()
#21 0x00000000 in ?? ()
OS Details: