image processing with matlab

30
IMAGE PROCESSING WITH MATLAB PAWAN KUMAR ASHUTOSH PANDEY IIT GUWAHATI ELECTRONICS CLUB

Upload: ayush-bansal

Post on 03-Oct-2015

25 views

Category:

Documents


2 download

DESCRIPTION

RoboticsElectronicsBasic Understanding of Image Processing robot using MATLAB

TRANSCRIPT

IMAGE PROCESSING WITH MATLAB

IMAGE PROCESSING WITH MATLABPAWAN KUMARASHUTOSH PANDEYIIT GUWAHATIELECTRONICS CLUBImage format supported by MatlabBMPHDFJPEGPCXTIFFXWB

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 ImagesBinary image: An image that consists of only black and white pixels.

Gray scale image: : It contains intensity values ranging from a minimum (depicting absolute black) to a maximum (depicting absolute white) and in between varying shades of gray. Typically, this range is between 0 and 255.RGB image: It represents an image with three matrices of sizes matching the image format. Each matrix corresponds to one of the colors red, green or blue and gives an instruction of how much of each of these colors a certain pixel should use.

Types of ImagesColor Image: RGB value: All colors which we see around us can be made by adding red, blue and green components in varying proportions. Hence, any color of the world can uniquely be described by its RGB value, which stands for Red, Blue and Green values. This triplet has each value ranging from 0 to 255, with 0 obviously meaning no component of that particular color and 255 meaning full component. For example, pure red color has RGB value [255 0 0], pure white has [255 255 255], pure black has [0 0 0] and has RGB value [55 162 170].

How to Read an Image>>Im=imread(cells.jpg); >>imshow(im)

Data CursorData 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.

How to make a M FileGo to File->New->Blank M-fileHere we have saved the m-file by the name okay.m. Now as you type >>okay in MATLAB command window, all the commands will execute.

Extraction of Color from Imageim=imread('C:\Electronics club\image1.jpg');fr= im(:,:,1);fg=im(:,:,2);fb=im(:,:,3);im1= fr>210 & fg>210 &fb> imaqhwinfo >> cam=imaqhwinfo; >> cam.InstalledAdaptors To get more information about the device, type >>dev_info = imaqhwinfo('winvideo',1) Note: Instead of winvideo, if imaqhwinfo shows another adaptor, then type that adaptor name instead of winvideo. Previewing VideoIf 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.SupportedFormatsYou 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 ImagesNow you have a video stream available and you need to capture still images from it. For that, use getsnapshot() command. >>im=getsnapshot(vid); % where vid is video input object Here im is the 3D matrix and you can see the image by the usual imshow() command >>imshow(im); If you get an unexpected image (with shade of violet/green/pink and low clarity), there is nothing to worry. You must be using a format starting with YUY2_... which means that your image is in YCbCr format and not RGB format. Therefore, you must convert it in RGB format by using >>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); % Do all image processing and analysis here end

WHAT NEXT ????Finding Centroid and Areabwboundaries It traces the exterior boundries of objects in Binary image. >> B=bwboundries(bw);

bwboundaries returns B, a P-by-1 cell array, where P is the number of objects and holes.(A cell array is one where each element of the array is a matrix itself). Each cell in the array B contains a Q -by-2 matrix. Each row in the matrix contains the coordinates (row and column indices) of a boundary pixel. Q is the number of boundary pixels (perimeter) for the corresponding region. In other words, B contains the coordinates of pixels constituting the perimeter of each object and hole.

Note:- In the coordinate matrix, the first coordinate is the row number (y - coordinate) and second coordinate is the column number (x-coordinate) of the boundary pixel. Take this always into consideration as it can create a lot of confusion. bwboundaries(bw) will trace the hole boundaries too. So, a better option is to use bwboundaries(bw,'noholes)Label Matrix (L)

A label matrix(L) corresponding to binary image is a 2D matrix of the same size as that of the image. Each object in the binary image is numbered 1,2,3,. and all the pixels of L corresponding to the objects in binary image have value respectively 1,2,3. The background pixels are 0 by default. In other words, the region in L corresponding to first object in the image is marked 1, corresponding to second object is marked 2 and so on. [B L]=bwboundaries(bw,'noholes');

regionpropsUsed 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; %to get area of 3rd object >>c=stats(2).Centroid; %to get centroid of 2nd object length(stats) = no. of objects in bwImportant>>img=rgb2gray(im); % covert rgb to gray>> img= rgb2hsv(im); % rgb to hsv image

Serial Communication between Matlab and Arduino.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.We have to connect the Arduino board to the PC.Each serial port on the PC is labeled COM1, COM2 etc.The Arduino will be given a COM Port Number. You should figure it out by following these steps-1. Right click on "My Computer icon" and select Manage.2 . Select Device Manager in the tree view you will see on the left side in the new window opened.3. Find and select Ports(COM& LPT) in the center panel.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!

Setting up serial port object in MatlabFirst we need to create a serial port object.Serial port object is just a name given to that serial port so that we can use it in later commands.>> s = serial ('COM4');In MATLAB, s is a structure which has all the above properties. We can access/modify them using dot(.) operator.Note that the if Status shows closed. It implies that the serial port is not connected.It is the rate of transfer of the data in bits/sec.We can change the BaudRate using the set method as follows->> set(s, 'BaudRate', 4800);>> s.BaudRate = 4800;You can also setup different BaudRate while making the serial port object as follows:->> s = serial('COM4','BaudRate', 9600);

contd.You can also do the following to verify the change->> s>>get(s)>>set(s)This is like a JAVA lock. Only one entity can acquire the lock at a time.Use fopen to acquire the lock and setup the connection.>> fopen(s)Notice the status property of the serial port object->> s.Statusans = openWriting to serial port in MatlabTo write a string->> fwrite(s, 'string');Transfer an int/float array->> fwrite(s, vector array, 'precision');The precision species the datatype of the vector array. It can be 'int8', 'int16', float32', float64', 'uint8', 'char' etc.

Reading from Serial port in ArduinoYou can follow these steps to read data in Arduino: Void setup(){Serial.begin(9600);}void loop(){if (Serial.Available()>0){b= Serial.read();}}End the ConnectionThis is very important step while Serial Communication.Make sure the execution of this step even in case the data is not transferred.Use fclose to end the connection->> fclose(s)Delete the serial port object if you are done with communication.>> delete(s)>> clear sSo if fopen is executed then fclose must be executed anyhow otherwise it will create problem when you use it next timeTo rememberYou cannot simultaneously view the data in MATLAB and Arduino serial port because the data can only be read once.The data is removed as soon as it is read first time.The synchronisation of data transfer is very important while communication.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.