Can sharpcap output data to an external program for image analysis?

Somewhere to ask questions about the best way to use SharpCap
Forum rules
‍

If you have a problem or question, please check the FAQ to see if it already has an answer : https://www.sharpcap.co.uk/sharpcap-faqs
Post Reply
bokemon
Posts: 30
Joined: Wed May 26, 2021 3:21 am

Can sharpcap output data to an external program for image analysis?

#1

Post by bokemon »

Hello,
I would like to use sharpcap for a computer vision application where it outputs continuous images to another program such as MATLAB which then can run various image analysis algorithms on it in real time, at the fastest possible fps. What I like about Sharpcap is that the developer already figured out the UI controls and settings to make a camera run as fast as possible. Plus I am not a programmer and it would take me a long time to figure out all the SDK stuff.

I don't know what would be the best way to do this, but I imagine something like having it output to a buffer which only holds one frame and updating it continuously. I could probably manage with my meager programming skills to at least make the other program read this buffer. Maybe ramdisk?
User avatar
admin
Site Admin
Posts: 17496
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Can sharpcap output data to an external program for image analysis?

#2

Post by admin »

Hi,

I think this will require at least some programming as there isn't a way at the moment to do the sort of thing you have in mind (anyway, the hard part is not writing to a buffer, but making sure that the other program doesn't read it while SharpCap is updating it...)

If you don't need to be completely live, then you could consider setting up SharpCap to capture to video files (say AVI), for a capture time of 5 seconds, then stop capturing and repeat with a new file. Your processing application could come along and read the latest AVI file and process it a few seconds later.

If you do need to process in real time then you might be able to do something using SharpCap's built in Python scripting - you can intercept the frame just after it is grabbed from the camera and do whatever you want with the frame data at that point (write it to a file, set it all to zero, etc).

There is some discussion of how to set up the python scripting to intercept frames when they are captured here : viewtopic.php?t=1141

The trickier part is going to be how to transmit that data to your target (MATLAB). You may be able to use 'named pipes' which are a way of passing data from one process to another on windows. It looks like MATLAB may be able to access them via .NET (see https://stackoverflow.com/questions/126 ... pes-matlab) and the SharpCap scripting is IronPython, which is also .NET based, so in theory you should be able to

* Create a named pipe server in SharpCap to send data out
* Create a named pipe client in Matlab to read data, use the same name, so it connects to the above server
* On the SharpCap side, with each frame, write the frame data to the pipe (maybe send the width, height, etc first)
* Read the same data on the matlab side

I'm afraid it's more than just a little bit of programming though :(

cheers,

Robin
bokemon
Posts: 30
Joined: Wed May 26, 2021 3:21 am

Re: Can sharpcap output data to an external program for image analysis?

#3

Post by bokemon »

Thanks for the extensive reply. Seems like it would be "faster" to just have MATLAB talk directly to the camera via the SDK's C libraries. (Yes, I need this to run real time) But I assume I can then no longer have Sharpcap running to provide a live view, adjust exposure and ROI settings, etc?
User avatar
admin
Site Admin
Posts: 17496
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Can sharpcap output data to an external program for image analysis?

#4

Post by admin »

Hi,

you are correct that in general you can only have one program talk to a particular camera at a time. Depending on the brand of camera, if you try to make a second program run the camera at the same time, it will either fail to open, freeze, crash or just generally behave weirdly.

If you happen to be using a QHY camera and are happy with 8 bit images then QHY have a slightly odd thing called 'broadcast mode' which you can turn on in SharpCap - it forwards the images in SharpCap on to a software defined DirectShow camera, which can be opened in another program or streamed. That might be of interest, although I wouldn't necesarily go out and buy a camera on the strength of that feature alone (anyway, it's a long time since I even tried using it...)

cheers,

Robin
bokemon
Posts: 30
Joined: Wed May 26, 2021 3:21 am

Re: Can sharpcap output data to an external program for image analysis?

#5

Post by bokemon »

Well, I got a ZWO 183mc camera to work with in this case.
Just for context, the situation is that the camera monitors a process that is ongoing, MATLAB does an image analysis on each frame (e.g. look for a color change or measures focus via edge detection), and then has to tell a motor to stop running ASAP when it detects that change.
Assuming I use MATLAB and call the C libraries, which method would have the least gap between "actual situation" and "program can process that data"?
a) call up each frame individually
b) put the camera in some kind of video mode and try to grab the most recent one from the buffer.
User avatar
admin
Site Admin
Posts: 17496
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Can sharpcap output data to an external program for image analysis?

#6

Post by admin »

Hi,

that's actually quite a tricky one to be sure about - it will probably be camera dependent and also may even depend on the parameters being used (exposure length, etc).

You can probably test by pointing the camera at a clock with seconds (even better 1/10 or 1/100s) and capturing both video and still frames to see how much the computer time when the image is ready to process lags the time in the clock you are imaging.

cheers,

Robin
Post Reply