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






|

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