问题
I am having problem to implement the following scenario. My problem statement goes like this:
- I have 3 threads. ThreadCamera for grabbing frames from a camera. ThreadProcess for processing (doing some image processing with OpenCV on the image/frame grabbed) the frame and main GUI Thread for displaying the image.
- I don't know how much time ThreadProcess will take to process an image. So I want to pass the image from ThreadCamera to ThreadProcess , do some image processing on the image and pass it to the main GUI Thread for display.
- When ThreadProcess processes the image the ThreadCamera should sleep. I.e. it should not grab further frames from the camera. When the ThreadProcess finishes the image processing task it should pass the image and some information to the main GUI Thread. After this only the ThreadCamera should wake up and grab the next frame/image from the camera runnig in that(ThreadCamera) thread.
Thanx Guys...after some comments to put Camera and Image Processing job in a single thread i would like to know another point..which is..
- What if don't want to sleep the camera while the processing is going on?It does not matter to me if I loose some of the frames grabbed by CameraThread(which in any case I am loosing if i sleep or not sleep the camera)
I am using QObject for each process(Camera Process and Image Processing job) and movetoThread command to make it run in a particular thread.
Any insight about the implementation and signal/slot design will be helpful..
回答1:
What you're looking for is a simple "Publish/Subscribe" pattern. In this type of 'distribution' pattern, all messages are sent and just dropped by the client when it's not in a state to receive images
I would implement this as the following in your application:
Have all separate threads (Camera,Processing,Gui) like you already do.
Have the CameraThread peridocally (through a qTimer signal maybe if you want to make it simple) capture an image and sent it over a signal/slot connection to the processingThread.
When the processingThread is processing an image, it sets a state flag (can just be a member variable, a bool would work) to say that its currently processing an image. When you're done processing the image you set the flag to say that you're not processing.
In the processingThreads slot, which receives the images from the CameraThread, you will first check to see if you're currently processing an image. If you are, you do not do anything with the signals data and you just return. If you are not processing an image, you will store the signals data and call the process function.
The trick to making this work is to include this function call (QCoreApplication::processEvents()
) in your ProcessingThreads main loop in the processing function. This will allow your ProcessingThread to process any signals it gets WHILE it's doing something useful.
The state variable checking will allow you to 'drop' all new images sent to you while you're processing a current one and not queue them up.
来源:https://stackoverflow.com/questions/13789344/implementation-of-inter-thread-communication-in-qt