Hi,
I have a function(assume the function is A) that capture image from my webcam, and another function play a video (assume the function is B). Both of them can run independently well. but when I mix them by using thread mThead(B) in function A. The B function will fail.
I have found that in function A if I execute the flip(...), the program can run into function B but it cannot play the video. If without flip(), the function B will correctly run.
Is there anyone know what happen to this? And how can I fix it?
Thank you very much.
This is some of my code:
using namespace cv;
using namespace
std; std;
// Global variables
Mat frame; //current frame
int keyboard; //input from
keyboard keyboard
void processVideo();
void
playVideo(); playVideo();
int main()
{
{
//create GUI windows
namedWindow("Frame");
processVideo();
//destroy GUI windows
destroyAllWindows();
return EXIT_SUCCESS;
}
}
void playVideo(){
VideoCapture cap("A.avi");
Mat
myframe;
myframe;
if (!cap.isOpened()) {
cout << "Cannot open the video file on C++ API" << std::endl;
exit(EXIT_FAILURE);
}
}
for (;;) {
cap >> myframe;
if (myframe.empty())
break;
imshow("Video Frame", myframe);
int c = waitKey(10);
if ((char)c == 27) { break; }
}
}
cap.release();
}
}
void processVideo() {
Mat
dst;
dst;
//create the capture object
VideoCapture cap(0);
//open capture
if (!cap.isOpened()){
cout << "Open camera failed";
exit(EXIT_FAILURE);
}
}
//read input data. ESC or 'q' for quitting
while ((char)keyboard != 'q' && (char)keyboard != 27){
//read the current frame
if (!cap.read(dst)) {
cerr << "Unable to read next frame." << endl;
cerr << "Exiting..." << endl;
exit(EXIT_FAILURE);
}
}
flip(dst, frame, 1); //something image processing
if (condition is true){
thread mThread(playVideo);
// wait the thread stop
mThread.join();
}
}
//show the current frame and the fg masks
imshow("Frame", frame);
//get the input from the keyboard
keyboard = waitKey(30);
}
}
//delete capture object
cap.release();
}
}