Professional Documents
Culture Documents
BMP
HDF
JPEG
PCX
TIFF
XWB
Most of the time we are using a JPEG format. Suppose an image named
myspace.jpg, it means it is in the JPEG format.
Types of Images
Types of Images
Color Image:
Data Cursor
Data cursor: To see the values of the colors in the figure window, go to
Tools>Data Cursor (or select from the toolbar), and click over any point
in the image. You can see the RGB values of the pixel at location (X,Y).
A better option of data cursor is the function imtool(). Type the following
>>imtool (cells.jpg);
and see the pixel info on lower left corner as you move mouse pointer
over different pixels.
im=imread('C:\Electronics club\image1.jpg');
fr= im(:,:,1);
fg=im(:,:,2);
fb=im(:,:,3);
imshow(im1)
Above code will extract yellow color image .
Yellow color
Removing Noise
As you can see, the binary image obtained above has quite a lot of noise. You
need to smoothen the edges, remove the tiny dots scattered here and there so
that at last you have some countable number of objects to work upon. There are
several functions available in MATLAB to remove noise in an Image.
BW2 = imfill(BW,'holes');
It fills holes in the binary image BW. A hole is a set of background pixels that cannot
be reached by filling in the background from the edge of the image.
BW2 = bwareaopen(BW, P) ;
It removes from a binary image all connected components (objects) that have fewer
than P pixels, producing another binary image, BW2.
se2=strel('square',4);
It is easy to find the centroid, area, number of objects and many more in
Binary images.
Capture
Image
Send signal to
Process Image
microcontrolle
in Matlab
r
Previewing Video
If you are using a laptop, you may also have a webcam in it. So note
down the DeviceName . If it is not the USB webcam, then probably
DeviceID = 2 should work. Hence, type
>>dev_info = imaqhwinfo('winvideo',2)
and check if it is working or not, if not then go for 3 or 4.
>>dev_info.SupportedFormats
You can preview the video captured by the camera by defining an object
(say by the name vid) and associate it with the device.
>>vid=videoinput('winvideo',1, 'YUY2_640x480') ;
>> preview(vid)
Capturing Images
Now you have a video stream available and you need to capture still images
from it. For that, use
getsnapshot() command.
>>im=getsnapshot(vid);
Here im is the 3D matrix and you can see the image by the usual imshow()
command
>>imshow(im);
>>im=ycbcr2rgb(im);
>>imshow(im);
vid=videoinput('winvideo',1, 'YUY2_160x120');
It takes time for the camera to be active and sufficient light to enter it after giving preview()
command. Hence, there should be a time gap of 2-3 seconds between start() and getsnapshot().
If you are typing in command window, then you can maintain this time gap manually. But generally
you will use these commands in an m-file, hence, the delay must be given using pause()
command.
Also in a vision based robot, the images have to be taken continuously, so use an infinite loop.
>>start(vid);
>>pause(3);
>>while(1)
img=getsnapshot(vid);
end
bwboundaries
Note:
bwboundaries(bw,'noholes)
regionprops
Used to measure properties of image regions.
>>stats =regionprops(L,properties)
where STATS is a structure array with length equal to the number of
labelled objects in L. The fields of the structure array denote different
properties for each region, as specified by properties.
properties are be a comma-separated list of strings. See the help
section of MATLAB for all the properties available. We will use Centroid
and Area.
Example,
>>stats=regionprops(L,'Area','Centroid');
The properties in the array stats are obtained by dot(.) operator.
>>a=stats(1).Area;
Important
Serial communication is when we transfer data one bit at a time, one right after the
other.
MATLAB can read/send the data from/to the serial port of the computer and process it.
The Arduino will be given a COM Port Number. You should figure it out by following
these steps-
2 . Select Device Manager in the tree view you will see on the left side in the new
window opened.
4 .There, you will find lists of all the ports attached to your computer.
5. Figure out the one you are concerned with. Refresh the window if you don't fi nd
any!
contd.
>> s
>>get(s)
>>set(s)
This is like a JAVA lock. Only one entity can acquire the lock at a time.
>> fopen(s)
>> s.Status
ans = open
Make sure the execution of this step even in case the data is not transferred.
>> fclose(s)
Delete the serial port object if you are done with communication.
>> delete(s)
>> clear s
To remember
You cannot simultaneously view the data in MATLAB and Arduino serial port
because the data can only be read once.
Always check for the property before setting its new value.
For example before you use fopen, check whether the serial port is already
used.
Check the BytesAvailable property before you read the data in MATLAB.