0

Red object detection in video

Posted by SUYOG PATIL on 11:36 PM in , , ,

After done with basics now let us try some programming.
Here is code written by me to recognize the red object in  live video.
I wrote this code for my project freestyle.






MatlabR2009 code:
clc
clear
vid=videoinput('winvideo',1,'YUY2_640x480');  
set(vid,'FramesPerTrigger',Inf);
set(vid,'ReturnedColorspace','rgb');
vid.FrameGrabInterval=3; 
bc=zeros(2,100);
while(vid.FramesAcquired<=100)%user defined no. of frames
      
    data = getdata(vid,1);% Get the snapshot of the current frame
    copy=data;
    r=data(:,:,1);
    g=data(:,:,2);
    b=data(:,:,3);
    copy(g>100)=0;
    copy(b>100)=0;
    copy(r<170)=0;
    r=copy(:,:,1);
    r=im2bw(r);

    % Remove all those pixels less than 300px
    r = bwareaopen(r,300);
   
    % Label all the connected components in the image.
    bw = bwlabel(r, 8);
   
    % Here we do the image blob analysis.
    % We get a set of properties for each labeled region.
    stats = regionprops(bw,'Centroid');
   
    % Display the image
    imshow(data)
    hold on
   
    %This is a loop to bound the red objects in a rectangular box.
    for object = 1:length(stats)
        bc(:,i) = stats(object).Centroid;
        plot(bc(1,i),bc(2,i), '-m+')
        a=text(bc(1,i)+15,bc(2,i), strcat('X: ', num2str(round(bc(1,i))), '    Y: ', num2str(round(bc(2,i)))));
        set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
        i=i+1;
    end
   
    hold off
   
end

% Stop the video acquisition
stop(vid);
% Flush all the image data stored in the memory buffer.
flushdata(vid);
delete(vid)
 clear all



Code is self explanatory and easy to understand....

This will help you guys 2 get started....

Thanks....

Don't forget to like  my facebook page....

















|
0

Interfacing with PC ports

Posted by SUYOG PATIL on 8:17 PM in , , ,
Many people asked me to put some Matlab interfacing with PC ports tutorials so this is it...
 

Interfacing with PC ports
MATLAB provides support to access serial port (also called as COM port) and parallel port (also called as printer port or LPT port) of a PC.
Note: If you are using a desktop PC or an old laptop, you will most probably have both, parallel and serial ports. However in newer laptops parallel port may not be available. 

Parallel Port
Parallel port has 25 pins as shown in figure below. Parallel port cables are locally available (commonly referred as printer port cables). These cables are handy to connect port pins with your circuit. Pins 2-9 are bi-directional data pins (pin 9 gives the most significant bit (MSB)), pins 10-13 and 15 are output pins (status pins), pins 1,14,16,17 are input pins (control pins), while pins 18-25 are Ground pins.









MATLAB has an adaptor to access the parallel port (similar to adaptor for image acquisition). To access the parallel port in MATLAB, define an object
>> parport= digitalio('parallel','LPT1');
You may obtain the port address using,
>> get(parport,'PortAddress')
>> daqhwinfo('parallel'); % To get data acquisition hardware information
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 to the parallel port into a matrix; e.g.
>> dataout = logical([1 0 1 0 1 0 1 1]);
Now to output these values, use the putvalue function
>> putvalue(parport,dataout);
Alternatively, you can write the decimal equivalent of the binary data and output it.
>> data = 23;
>> putvalue(parport,data);

You can connect the pins of the parallel port to the driver IC for the left and right motors of your robot, and control the left, right, forward and backward motion of the vehicle. You will need a H-bridge for driving the motor in both clockwise and anti-clockwise directions.

Serial Port
If you have to transmit one byte of data, the serial port will transmit 8 bits as one bit at a time. The advantage is that a serial port needs only one wire to transmit the 8 bits (while a parallel port needs 8).


Pin 3 is the Transmit (TX) pin, pin 2 is the Receive (RX) pin and pin 5 is Ground pin. Other pins are used for controlling data communication in case of a modem. For the purpose of data transmission, only the pins 3 and 5 are required.

At the receiver side, you need a voltage level converter called as RS232 IC which is a standard for serial communication. And to interpret the serial data, a microcontroller with UART (Universal asynchronous receiver transmitter) is required aboard the robot. Most of the microcontrollers like AVR ATMEGA 8, Atmel/Philips 8051 or PIC microcontrollers have a UART. The UART needs to be initialized to receive the serial data from PC.

In this case, the microcontroller is connected to the motor driver ICs which control the right and left motors. After processing the image, and deciding the motion of the robot, transmit codeword for left, right, forward and backward to the microcontroller through the serial port (say 1-Left, 2-Right, 3-Forward, 4-Backward).

MATLAB code for accessing the serial port is as follows:
>> ser= serial('COM1','BaudRate',9600,'DataBits',8);
>> fopen(ser);
To send data through the serial port, the available commands
>> fwrite (ser,1); % for left motion
>> fwrite (ser,2); % for right motion
You can close the port in case there are other applications using this port using the fclose command.
>> fclose(ser);
Microcontroller has an output port whose pins can be used to control the driver IC. Thus, microcontroller interprets the serial data from the PC and suitably controls the motors through the output pins and the motor driver.




All those who want to build Matlab controlled robotic car you can get all help and circuit diagrams here
download files here


Thank you for reading...
 






|
0
Posted by SUYOG PATIL on 6:57 PM in , , ,
Working with Images


Here i will demonstrate you some of the basic but very important image processing functions..

 [I,map]=imread('trees.tif'); % read a TIFF image

I2=ind2gray(I,map); % convert it to grayscale

imagesc(I2,[0 1]) % scale data to use full colormap  for values between 0 and 1
 
colormap('gray') % use gray colormap
 
axis('image') % make displayed aspect ratio proportional to image dimensions
 
I=imread('photo.jpg'); % read a JPEG image into 3D %array
figure
imshow(I)
 
rect=getrect ; % select rectangle
 
I2=imcrop(I,rect); % crop
 
I2=rgb2gray(I2); % convert cropped image to grayscale
imagesc(I2) % scale data to use full colormap
% between min and max values in I2
colormap('gray')
 
colorbar % turn on color bar
pixval % display pixel values interactively
truesize % display at resolution of one screen pixel per image pixel
truesize(2*size(I2)) % display at resolution of two screen pixels per image pixel

I3=imresize(I2,0.5,'bil'); % resize by 50% using bilinear interpolation
 
I3=imrotate(I2,45,'bil','same'); % rotate 45 degrees and crop to original size
 
I3=double(I2); % convert from uint8 to double, to allow math operations
 
imagesc(I3.^2) % display squared image (pixel-wise)
 
imagesc(log(I3)) % display log of image


|
6

Camera access and video input in Matlab

Posted by SUYOG PATIL on 10:24 AM in , , ,
Getting Hardware information
Till now we have been working on images already saved on our computer. But in actual practice, we need to work in real time, i.e., we need to take images continuously from the current environment using a webcam and then process them. Hence, the Image Acquisition toolbox of MATLAB provides support in this regard. To 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. You can check if the support is available for your camera in MATLAB by typing the following:
>> imaqhwinfo % stands for image acquisition hardware info
>> cam=imaqhwinfo;
>> cam.InstalledAdaptors
I tried this on my computer and I got the following information

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

Most probably you would have 'winvideo' installed and use that. If it is not available, use whichever adapter is shown by ‘imaqhwinfo’. 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)
*Note: From now onwards, I will refer Adapter by ‘winvideo’ and DeviceID by ‘1’. You must check yourself what is available on your system and change the commands described further accordingly. Note down the supported formats by your camera as
>>dev_info = imaqhwinfo('winvideo',1);
>>dev_info.SupportedFormats

As you can see, there are 5 supported formats in my camera. The numbers (160x120, 176x144….) denote the size of the image to be captured by the camera.

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
It should give information somewhat like this

‘vid’ is has been declared as the video input object, and now the camera can be referenced by this name. To see a preview of the video through your camera, use preview command
>> 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. A larger size gives greater clarity and is easier to work with, but consumes more memory and therefore is slow. But in a smaller image, it is difficult to differentiate between two objects.

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);
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);
If a format somewhat like ‘RGB24_160x120’ (anything starting with RGB24_....) is used, then you directly get the image in RGB format. If you want to store an image captured, so that you can view it later (like .jpg, .png, .bmp), you can use imwrite()
>>imwrite(im,'myimage.jpg');
The file myimage.jpg is saved in the current working directory.
Courtesy-Ankur Agrawal,Indian Institute of Technology, Kanpur

Thanks for reading ...
we will study some functions for Image processing next time....

|

Copyright © 2009 ALL ABOUT ROBOTICS!! All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.