Digital Image Processing Part(2)
In this part we will learn about the programing of RED Color Detection.As we know that the hardware is connected with matlab serially so there is also serial commands are used for interfacing purpose.
Lets move towards the Micro Controller programing and then Matlab with serial interfacing coding.
Compiler user for µ_Heart(Code):
The AVR software is used for the programming.In the keil there is the different types of controller
and all the have build in function but here we used our own functions,not buid in functions.
Blood of µ_Heart(Code):
#define F_CPU 1000000
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
//Macros definition
#define BitGet(p,m)((p) & (m))
#define BitSet(p,m)((p) |= (m))
#define BitFlip(p,m)((p) ^= (m))
#define Bit(x)(0x01 << (x))
#define BitClear(p, m) ((p) &= ~(m))
unsigned char x;
int main(void)
{
DDRA = 0xFF;
PORTA=0xFF;
UCSRB=(1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
UCSRA=1<<U2X;
UBRRL =12;
sei();
while(1)
{
if (x=='s')
{
PORTA=0xFF;
}
if (x=='f')
{
PORTA=0xFE;
}
if (x=='b')
{
PORTA=0xFD;
}
if (x=='l')
{
PORTA=0xFA;
}
if (x=='r')
{
PORTA=0xF6;
}
}
}
ISR (USART_RXC_vect)
{
x = UDR;
//Store the received character into x
Matlab is a compute program that combines computation and visualization power that makes it
particular useful tools for engineering.Here we used Matlab for coding to visualize the video image that
get from the camera and perform the operation on motion of CAR.
a = imaqhwinfo;
% [camera_name, camera_id, format] = getCameraInfo(a);
sobj=serial('com2');
fopen(sobj);
% videoinput function used for capture the video
% You have to replace the resolution & your installed adaptor name.
vid = videoinput( 'winvideo', 1, 'YUY2_640x480');
% Set the properties of Capute video
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;
ath=100000;
x0=320; y0=240;
%start the video aquisition here
start(vid)
%%
% Set a loop that stop after 100 frames of aquisition
while(vid.FramesAcquired<=200)
% Get the snapshot of the current frame
data = getsnapshot(vid);
[r c m]=size(data);
data=data(:,end:-1:1,:);
% Now to track red objects in real time
% we have to subtract the red component
% from the grayscale image to extract the red components in the image.
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
%Use a median filter to filter out noise
diff_im = medfilt2(diff_im, [3 3]);
% Convert the resulting grayscale image into a binary image.
diff_im = im2bw(diff_im,0.18);
% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);
% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid', 'Area');
% Display the image
imshow(data)
hold on
%This is a loop to bound the red objects in a rectangular box.
if(length(stats))
for object = 1:length(stats)
area(object)= stats(object).Area;
end
object=find(area==max(area));
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
ba= stats(object).Area;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), 'Y:
', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12,
'Color', 'yellow');
b=text(bc(1),bc(2)+35, strcat('Area: ', num2str(round(ba))));
set(b, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12,
'Color', 'cyan');
xn=bc(1);
if(xn>x0)
xn1=xn-x0;
if(xn1>100)
fprintf(sobj,'l');
end
else if(xn<x0)
xn1=x0-xn;
if(xn1>100)
fprintf(sobj,'r');
end
end
end
% for area
if(ba>ath)
area=ba-ath;
if(area>50000)
fprintf(sobj,'b');
end
else if(ba<ath)
area=ath-ba;
if(area>50000)
fprintf(sobj,'f');
end
end
end
if((ba>50000) & (ba<ath+50000) & (xn<420) & (xn>220))
fprintf(sobj,'s');
end
else
fprintf(sobj,'s');
end
hold off
end
% Both the loops end here.
% Stop the video aquisition.
stop(vid);
close figure 1;
fclose(sobj);
% Flush all the image data stored in the memory buffer.
flushdata(vid);
% Clear all variables