Thursday, December 26, 2013

Camera Input in Matlab GUI



Accessing camera through Matlab is very much wanted for doing  real time image/video processing via matlab. So here I will explain how to access camera input via Matlab and create a GUI with camera view.

First let's begin with the image acquisition tool of Matlab. Via this tool we can learn lot of about our camera.

So start matlab and just enter imaqtool command in comand editor.
command   imaqtool   opens a GUI.

Image Acquisition Tool

Here you can find your hardware in hardware browser panel. Select your hardware and choose available and preferred  format. And then click start preview. You will see a preview from camera input and you can see matlab code which does the magic in session log panel in right down, here it is.
Session Log


So you can enter these commands in command editor when ever you want to preview video without using imaqtool.  You can change image resolution by changing the format from available format window.
So copy commands from session log and paste into comand editor. now you will see a workspace variable named vid. double click on the workspace varialbe vid and you will see property list of vid object.
You can change some of these properties in order to get desired video input.
Ok, now we know how to get camera input, change its properties. Now lets design the GUI to display this camera input and to capture image.
type guide in command editor and create a new empty GUI. Then design following GUI with a axes, and a push button. double click on the axes and open its properties.

in properties  select tag property and give it a name, this name will be name you are to use in code. I gave the name ' cameraview '.



In GUI designer go to view-> M-file editor and open source code for your GUI.  Here we can write matlab code to get the camera input. In the opening function of your GUI (function whose name is ended with OpeningFcn) insert this code. This will create a video object and binds it to the handles object of the GUI. So change according to your device as previously mentioned.


you can download source code here.

First we create a video object and we changed its properties according to what we need. Then we create a image object of size of camera resolution . To learn much about it see help on image function.
And then we use preview function to draw preview on previously created image.
Now when you run your GUI you can see video input viewing on camera axes.




Next our target is to capture an image when you press capture button. To do that go to call back function of the capture button.  Here we can get captured image from using getsnapshot method and here I will save image data to base workspace so that we can use that image for another tasks. This is the code for capture button.


Click the capture button,Now you can see a variable named imqs in your base workspace. To understand more about assigning see matlab help on assignin. 
and in command editor enter 
imshow(imqs); and you will see captured image.
 Now we have camera input. It is processing time.
Download M-files here .