実現したいこと
Thread
を用いたOpenCVでの画像表示
発生している問題・エラーメッセージ
ShowManyImages()
はスレッド化していないimg0
,img1
,img2
を入力としたら,問題なく動作しました.
そこで各img
をスレッド化しようということで,以下のように関数化したのですが,ShowManyImages()の引数に各multi_imread
を入力したところ以下のようにエラーが出てき,うまくスレッド化して表示させることができませんでした.
恐らく,スレッドとして立てた際に返り値img
にならないことに起因していると思うのですが,どのように直せばいいかわからなかったのでここに質問するに至りました.
sh
multi_client.cpp:180:44: error: use of deleted function ‘std::thread::thread(std::thread&)’ ShowManyImages("IMAGE",3,th_0,th_1,th_2); ^ In file included from multi_client.cpp:8:0: /usr/include/c++/7/thread:109:5: note: declared here thread(thread&) = delete; ^~~~~~ multi_client.cpp:180:44: error: use of deleted function ‘std::thread::thread(std::thread&)’ ShowManyImages("IMAGE",3,th_0,th_1,th_2); ^ In file included from multi_client.cpp:8:0: /usr/include/c++/7/thread:109:5: note: declared here thread(thread&) = delete; ^~~~~~ multi_client.cpp:180:44: error: use of deleted function ‘std::thread::thread(std::thread&)’ ShowManyImages("IMAGE",3,th_0,th_1,th_2); ^ In file included from multi_client.cpp:8:0: /usr/include/c++/7/thread:109:5: note: declared here thread(thread&) = delete; ^~~~~~ <ビルトイン>: recipe for target 'multi_client.o' failed make: *** [multi_client.o] Error 1
該当のソースコード
cpp
# include <opencv2/core/core.hpp> # include <opencv2/opencv.hpp> # include <opencv2/highgui/highgui.hpp> # include <iostream> # include <stdio.h> # include <stdarg.h> # include <zmq.hpp> # include <thread> # include <mutex> # include <vector> std::mutex mtx; using namespace cv; using namespace std; Mat multi_imread0() { mtx.lock(); Mat img0; img0 = imread("images.jpeg"); resize(img0,img0,cv::Size(),500.0/img0.cols, 500.0/img0.rows); if(img0.empty()) perror; else return img0; mtx.unlock(); } Mat multi_imread1() { mtx.lock(); Mat img1; img1 = imread("images1.jpeg"); resize(img1,img1,cv::Size(),500.0/img1.cols, 500.0/img1.rows); if(img1.empty()) perror; else return img1; mtx.unlock(); } Mat multi_imread2() { mtx.lock(); Mat img2; img2 = imread("image2.jpg"); resize(img2,img2,cv::Size(),500.0/img2.cols, 500.0/img2.rows); if(img2.empty()) perror; else return img2; mtx.unlock(); } int main(void) { int i; std::thread th_0(multi_imread0); std::thread th_1(multi_imread1); std::thread th_2(multi_imread2); th_0.join(); th_1.join(); th_2.join(); ShowManyImages("IMAGE",3,th_0,th_1,th_2); }
自分で調べたことや試したこと
OpenCVはスレッドセーフな設計になっていないこと
multi_imread
のコードは恐らく大丈夫(簡単な例でやってみて動作したので)
使っているツールのバージョンなど補足情報
OpenCV2,C++14,thread
以上です.ご指摘のほどよろしくお願いいたします.
まだ回答がついていません
会員登録して回答してみよう