You are on page 1of 30

Image processing Image acquisition Data acquisition

Important terms and types of Images

Pixel: Pixels are the building blocks of an image. In other words, a pixel is the smallest possible image that can be depicted on your screen.
Binary Image: An image that consists of only black and white pixels. 0 or 1

Grayscale 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 *Note : In daily language what we refer to as black-and-white (as in old photos) are actually grayscale. Hence avoid confusion here in technical terms.

Color Image: We all have seen this! Such an image is composed of the three primary colors, Red, Green and Blue, hence also called an RGB image.

RGB value: 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].

Representation of an Image in MATLAB


stored

as a 2D matrix (of size mxn)

represents Hence,

the intensity of light/color of that particular pixel for a binary image, the value of each element of the matrix is either 0 or 1 and for a grayscale image each value lies between 0 and 255.

Reading and displaying Images


imread(): To read an image and store in a matrix. Syntax: IM=imread(filename) where IM is a matrix.

>>im=imread('peppers.png');

imshow(): Displays the image.


Syntax: imshow(filename) or imshow(im) Example,

>>imshow('cameraman.tif'); 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).

>>imtool(peppers.png);

First,

clear the MATLAB workspace of any variables, close open figurewindows, and close all open Image Tools.close allRead and display the grayscale image rice.png. I = imread('rice.png'); imshow(I)

Estimate the Value of Background Pixels

background

= imopen(I,strel('disk',15)); Figure, imshow(background)


Create

an Image with a Uniform Background I2 = imsubtract(I,background); figure, imshow(I2)

Adjust

the Contrast in the Processed Image After subtraction, the image has a uniform background but is now a bit too dark. Use imadjust to adjust the contrast of the image.imadjust increases the contrast of the image

I3

= imadjust(I2); figure, imshow(I3);

Create level

a Binary Version of the Image

= graythresh(I3); bw = im2bw(I3,level); figure, imshow(bw)


Determine

Image

the Number of Objects in the

[labeled,numObjects]
numObjects

= bwlabel(bw,4);

figure,

Examine the Label Matrix


imshow(labeled); impixelregion

Display the Label Matrix as a Pseudocolor Indexed Image


pseudo_color = label2rgb(labeled, @spring, 'c', 'shuffle'); figure, imshow(pseudo_color);

Measure Object Properties in the Image


graindata To

= regionprops(labeled,'basic')

find the area of the 51st labeled component (grain of rice), access the Area field in the 51st element in the graindata structure array. Note that structure field names are case sensitive. area51 = graindata(51).Area

maxArea

= max([graindata.Area]) = mean([graindata.Area])

meanArea

hist([graindata.Area],20)

Cropping

Tool

an Image Using the Crop Image

Converting a Multiframe Image to a Movie


To

create a MATLAB movie from a multiframe image array, use the immovie function To play the movie, use the movie function.movie(mov); mri = uint8(zeros(128,128,1,27)); for frame=1:27 [mri(:,:,:,frame),map] = imread('mri.tif',frame); end
mov

= immovie(mri,map); movie(mov);

RGB

= imread(`peppers.png'); imshow(RGB)
himage

= imshow('pout.tif') hpixelinfopanel = impixelinfo(himage); hdrangepanel = imdisplayrange(himage); hpixreg = impixelregion(himage);

Scroll panal
hfig

= figure('Toolbar','none',... 'Menubar', 'none'); himage = imshow('concordaerial.png'); hpanel = imscrollpanel(hfig,himage);

Larger

image tool big_image = imread('peppers.png'); my_large_image_display(big_image)

Resizing an image
I

= imread('circuit.tif'); J = imresize(I,1.25); imshow(I) figure, imshow(J) Rotating an Image I = imread('circuit.tif'); J = imrotate(I,35,'bilinear'); imshow(I) figure, imshow(J)

Read

image and display it.rgb=imread('onion.png'); imshow(rgb);


Create

an indexed image with eight colors and without dithering.


[X_no_dither,map]= rgb2ind(rgb,8,'nodither'); figure, imshow(X_no_dither,map); Create an indexed image using eight colors with dithering. [X_dither,map]=rgb2ind(rgb,8,'dither'); figure, imshow(X_dither,map);

Working in Real Time


Getting To

Hardware information

start with working in real time, you must have a functional USB webcam connected to your PC and its driver installed. MATLAB has built-in adaptors for accessing these devices. An adaptor is a software that MATLAB uses to communicate with an image acquisition device.

>>

imaqhwinfo % stands for image acquisition hardware info >> cam=imaqhwinfo; >> cam.InstalledAdaptors
To

get more information about the device, type >>dev_info = imaqhwinfo('winvideo',1)

If

you are using a laptop, you may also have a webcam in it. So note down the DeviceName shown as above. If it is not the USB webcam, then probably DeviceID = 2 should work. Hence, type >>dev_info = imaqhwinfo('winvideo',2)

Previewing

video 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_160x120') or >>vid=videoinput('winvideo',1, 'RGB24_160x120') % depends on availability

>>

preview(vid)

You

can check out the preview with different formats. You will see that the size of preview window changes. Use a format that suits your size requirement.

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); % 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);

>>im=ycbcr2rgb(im); >>imshow(im); >>imwrite(im,'myimage.jpg'); The

file myimage.jpg is saved in the current working directory.

vid=videoinput('winvideo',1,

'YUY2_160x120') ; preview(vid); pause(3); while(1) img=getsnapshot(vid); % Do all image processing and analysis here end

You

have to define the pins 2-9 as output pins, by using addline() function >> addline(parport,0:7,'out') Now put the data which you want to output (depends on motions of robot required) to the parallel port into a matrix (See function logical() in MATLAB Help); e.g. >> dataout = logical([1 1 0 1 0 1 0 1]);

To

access the parallel port in MATLAB, define an object; lets say by the name parport

>>

parport = digitalio('parallel','LPT1'); You may obtain the port address using, >> get(parport,'PortAddress') >> daqhwinfo('parallel');

Data acquisition programming

LED

blinking Counter display My robo. Home appliances system Buzzer(speaker )