0
$\begingroup$

I need to start a new thread inside a mathematica function. My example is about image aquisition and processing.

Do[Pause[.5] im = 
   CurrentImage[];(*start an other thread for im processing*), \
Infinity]
$\endgroup$
  • $\begingroup$ Why do you need to do that? Can you provide some context? $\endgroup$ – MarcoB Feb 14 '17 at 18:59
  • 1
    $\begingroup$ Mathematica does not have threads—it is not possible. Some things that threads are used for can be accomplished using Dynamic functionality. Some other things can be accomplished using the parallel tools. In order to get an answer with an alternative solution, you would need to explain in detail what you want to do. Without that all I can say is that it's not possible. $\endgroup$ – Szabolcs Feb 14 '17 at 19:01
  • $\begingroup$ If you go the parallel tools route, the solution will likely involve ParallelSubmit, so do take a look a that. You will explicitly need to share data with the subkernel. Since it is a separate process (and not a thread), it does not share any memory with the main kernel. $\endgroup$ – Szabolcs Feb 14 '17 at 19:10
  • $\begingroup$ @Szabolcs, I am developing a video surveillance application to detect abnormal action of a person in a corridor. I need to capture images and process it and send an alarm if I detect an abnormal action. So, I need to run a multi-thread application. $\endgroup$ – BetterEnglish Feb 14 '17 at 19:14
  • $\begingroup$ Why exactly can you not do it sequentially? 1. Capture image 2. Analyse it. 3. Wait until the time when the next frame should be captured, then loop from step 1. If analysis is too slow compared to capture, won't the analysis requests just pile up beyond control? $\endgroup$ – Szabolcs Feb 14 '17 at 19:17
6
$\begingroup$

The video surveillance application mentioned by the OP in the comments can be approached using Dynamic. This version takes the difference between consecutive images, if the total value of the differences is large enough, then it sends an alert (i.e., it prints the word "Alert" in the messages box).

Dynamic[img = ImageDifference @@ CurrentImage[2]; 
 If[Total[ImageData[img], 3] > 10000, Print["Alert"]]; img]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.