Recording, displaying and saving images in MATLAB
From Course Wiki
- Run the UsefulAcquisition tool
- Launch MATLAB and in the command window type:
foo = UsefulImageAcquisition; foo.Initialize
- The Image Acquisition window (Figure 1) will open with the controls for the camera.
- The "Manta_G-032B" CCD and "Manta G-040" CMOS cameras are configured to produce 12-bit, monochrome images. In this mode, the intensity of each pixel in the image will be represented by 12 binary digits, allowing a range of values from 0-4095.
- In the Image Acquisition window set "Frame Rate" to 20. This will cause the camera to take 20 complete images per second (this will only be relevant when recording movies in Assignments 4 and 5 of the lab).
- Click the "Start Preview" button. The live image from the camera should appear in the Preview pane.
- If this does not produce a live image, use an appropriate expletive, and ask an instructor to figure out what the heck went wrong.
- Change the "Exposure" setting to produce a good image. The value sets the exposure time for each frame in microseconds.
- Recording an image
- In the Image Acquisition window, set "Number of Frames" to 1. This setting controls how many images MATLAB will record each time you press the "Acquire" button.
- When you are happy with the image displayed by the live preview, press "Acquire". The live preview will stop.
- The image is now stored in the foo.ImageData variable, which will update each time you acquire a new image. To copy the data into a new variable, choose a descriptive name for your image like 'microruler10x' and save it to your workspace by typing the following into the MATLAB command window:
microruler10x = foo.ImageData;
- Next, in the MATLAB command window type
- This command will display relevant information about the new variable you’ve created. You should see that the image is represented as a 544x728 matrix of 16-bit integers.
- Displaying the image
- You can display images using a variety of commands in MATLAB. In the optics bootcamp we used the imagesc command which scales the image intensity (the brightest pixel is white, the darkest is black). In some cases, like when you have very dim images, this command can be misleading. It’s better to use the un-scaled imshow command for quantitative measurements.
- When the 12-bit numbers from the camera get transferred to the computer, they are converted to 16-bit numbers. 16-bit numbers can represent a range of values from 0-65535, while your 12-bit image only contains values from 0-4095. This leaves a considerable portion of the number range unoccupied. Consequently, if you type imshow( microruler10x ), you will see an image that looks almost completely black (try it!).
- One way to make this work is to rescale your image to 16 bits: imshow( 65535/4095 * microruler10x )
- An even better way to work with images in MATLAB is to convert them to double precision floating point format straightaway. Double precision floating point numbers can represent an extremely wide range of values with high precision. Convert the image to a double and rescale it using the following command:
microruler10x = double( microruler10x ) / 4095;
- This conversion has made your image into a double with a range of intensities from 0-1, with 1 being full intensity and 0 completely dark. Now try:
- Saving your image
- Save images in a .mat format so that you can easily reload them into Matlab for later use.
save microrulerImages % saves entire workspace to filename 'microrulerImages.mat' save microrulerImages image1 image2 %saves only variables image1 and image2 to filename 'microrulerImages.mat'
- To reload your data the next time you open matlab, navigate to the folder where you saved your workspace, type
- If you want to save individual images as a .PNG (a good format for use in your report or other programs), the command might look something like:
- Note that you can also use the File→Save As menu after displaying an image or figure. This is useful if you want to save additions to your images (like data cursors). However, we recommend always saving the raw data as .mat files so that you are able to re-process your images later.
whos microruler10x imshow( microruler10x )
imwrite( im2uint16( microruler10x ), 'microruler10x.png', 'png' );