Tải bản đầy đủ - 0 (trang)
E. Nút Detection & Tracking.

E. Nút Detection & Tracking.

Tải bản đầy đủ - 0trang

frameLost = 20;



% Cho gia tri Rho 35:45

startIdxRho_R = 415;

NumRhos_R = 51;



% Cho gia tri Theta -90:-70deg

startIdxTheta_R = 1;

NumThetas_R = 21;



% Cho gia tri Rho 379:415

startIdxRho_L = 380;

NumRhos_L = 36;



% Cho gia tri Theta 55:85deg

startIdxTheta_L = 146;

NumThetas_L = 21;



% Hien thi duong line offset.

offset = int32([0, NumRows, 0, NumRows]);



%%

% Tao doi tuong |VideoFileReader| de doc video tu file.

hVideoSrc = imaq.VideoDevice('winvideo',2,'RGB24_640x480', ...

'ROI', [140 1 360 240], ...

'ReturnedColorSpace', 'rgb');



%imaq.VideoDevice('winvideo',2,'RGB24_640x480', ...

%vision.VideoFileReader('viplanedeparture.avi');

%%

% Tao doi tuong |ColorSpaceConverter| de chuyen anh RGB sang anh cuong

do.

hColorConv1 = vision.ColorSpaceConverter( ...

'Conversion', 'RGB to intensity');



%%



91



% Tao doi tuong |ColorSpaceConverter| de chuyen anh RGB



sang anh Y'CbCr.



hColorConv2 = vision.ColorSpaceConverter( ...

'Conversion', 'RGB to YCbCr');



%%

% Tao |ImageFilter| de thuc hien loc 2-D FIR de xac dinh bien trong

video.

hFilter2D = vision.ImageFilter( ...

'Coefficients', [-1 0 1], ...

'OutputSize', 'Same as first input', ...

'PaddingMethod', 'Replicate', ...

'Method', 'Correlation');



%%

% Tao |Autothresholder| de chuyen doi anh cuong do sang anh nhi phan.

hAutothreshold = vision.Autothresholder;



%%

% Tao |HoughTransform| de tao duong thang duoc dinh nghia trong bd Hough.

hHough = vision.HoughTransform( ...

'ThetaRhoOutputPort', true, ...

'OutputDataType', 'single');



%%

% Tao |LocalMaximaFinder| de tim dinh trong matran bien doi Hough.

hLocalMaxFind1 = vision.LocalMaximaFinder( ...

'MaximumNumLocalMaxima', ExpLaneNum, ...

'NeighborhoodSize', [301 81], ...

'Threshold', 1, ...

'HoughMatrixInput', true, ...

'IndexDataType', 'uint16');

hLocalMaxFind2 = vision.LocalMaximaFinder( ...

'MaximumNumLocalMaxima', 1, ...

'NeighborhoodSize', [7 7], ...

'Threshold', 1, ...

'HoughMatrixInput', true, ...

'IndexDataType', 'uint16');



92



hLocalMaxFind3 = vision.LocalMaximaFinder( ...

'MaximumNumLocalMaxima', 1, ...

'NeighborhoodSize', [7 7], ...

'Threshold', 1, ...

'HoughMatrixInput', true, ...

'IndexDataType', 'uint16');



%%

% Tao doi tuong |HoughLines| de tim toa do Decac cua duong thang duoc xac

% dinh trong dau duong.

hHoughLines1 = vision.HoughLines('SineComputation', 'Trigonometric

function');

hHoughLines3 = vision.HoughLines('SineComputation', 'Trigonometric

function');



%%

% Dinh nghia tham so de chen text vao trong canh bao lan duong.

warnText = {sprintf('Ben phai'), '', sprintf('Ben trai')};

warnTextLoc = [120 170;-1 -1; 2 170];



%%

% dinh nghia tham so dac diem text chen vao trong video.

%lineText = {'', ...

%



sprintf('Duong dut'), sprintf('Duong lien'), ...



%



sprintf('Duong dut'), sprintf('Duong lien')};



% Tao doi tuong |VideoPlayer| de xuat ra man hinh video.

%hVideoOut = vision.VideoPlayer;



%%

% Khoi tao bien trong vong lap.

Frame = 0;

NumNormalDriving = 0;

OutMsg = int8(-1);

OutMsgPre = OutMsg;

Broken = false;



93



%% Vong lap xu ly.

% Tao vong lap phat hien lan duong trong video nhap vao.

% Vong lap nay dung nhung doi tuong da duoc tao.

warningTextColors = {[1 0 0], [1 0 0], [0 0 0], [0 0 0]};

while strcmp(get(hObject,'String'),'Processing')

RGB = step(hVideoSrc);

% Chon vung duoi cua video dau vao (dinh nghia vung quan sat)

Imlow



= RGB(NumRows+1:end, :, :);



% Phat hien bien va bien doi Hough.

Imlow = step(hColorConv1, Imlow); % Chuyen anh RGB sang anh cuong do.

I = step(hFilter2D, Imlow);

% Bao hoa cac gia tri giua 0 va 1.

I(I < 0) = 0;

I(I > 1) = 1;

Edge = step(hAutothreshold, I);

[H, Theta, Rho] = step(hHough, Edge);



% Phat hien dinh.

H1 = H;

% Mo rong ma tran H voi theta < -78 do va theta >= 78 do.

H1(:, 1:12) = 0;

H1(:, end-12:end) = 0;

Idx1 = step(hLocalMaxFind1, H1);

Count1 = size(Idx1,1);



% Chon Rhos va Thetas tuong ung voi dinh.

Line = [Rho(Idx1(:, 2)); Theta(Idx1(:, 1))];

Enable = [ones(1,Count1) zeros(1, ExpLaneNum-Count1)];



% Theo doi tap hop cac dau phan cach lan duong.

[Rep_ref, Count_ref] = vilama(Rep_ref, Count_ref, ...

MaxLaneNum, ExpLaneNum, Enable, Line, ...

TrackThreshold, frameFound+frameLost);



% Chuyen duong thang tu toa do Polar sang Cartesian.

Pts = step(hHoughLines1, Rep_ref(2,:), Rep_ref(1,:), Imlow);



94



% Lan duong trai va phai.

[TwoValidLanes, NumNormalDriving, TwoLanes, OutMsg] = ...

videwa(Pts, Imlow, MaxLaneNum, Count_ref, ...

NumNormalDriving, OutMsg);

% Dien giai: 0 = Right lane departure,

%



1 = Normal driving, 2 = Left lane departure



% Output

Frame = Frame + 1;

if Frame >= 5

TwoLanes1 = TwoLanes + [offset; offset]';

if DrawPoly && TwoValidLanes

if TwoLanes(4,1) >= 239

Templ = TwoLanes1(3:4, 1);

else

Templ = [0 239]';

end

if TwoLanes(4,2) >= 239

Tempr = TwoLanes1(3:4, 2);

else

Tempr = [359 239]';

end

Pts_poly = [TwoLanes1(:,1); Templ; Tempr; ...

TwoLanes1(3:4,2); TwoLanes1(1:2,2)];



% Ve lan duong.

RGB = insertShape(RGB,'FilledPolygon',Pts_poly.',...

'Color',[1 1 1],'Opacity',0.0);

end



% Ve duong line.

RGB = insertShape(RGB,'Line',TwoLanes1',...

'Color',{'yellow','yellow'});

% Chen text canh bao.

txt = warnText{OutMsg+1};

txtLoc = warnTextLoc(OutMsg+1, :);

txtColor = single(warningTextColors{mod(Frame-1,4)+1});

RGB = insertText(RGB,txtLoc,txt,'TextColor', txtColor, ...



95



'FontSize',20, 'BoxOpacity', 0);

%% Truyen tin hieu dieu khien

if OutMsg == 0

axes(handles.right);

imshow('right.jpg');

set(handles.edit_TX,'String','Right');

% Gui tin hieu toi cong COM

IsConnect = handles.status_com;

s = handles.com;

if (IsConnect == 1)

strTX = get(handles.edit_TX,'String');

%s=handles.com

fprintf(s,strTX);

else

% no anything

end

handles.com = s;

else

axes(handles.right);

cla(handles.right);

set(handles.edit_TX,'String','');



end

if OutMsg == 2

axes(handles.left);

imshow('left.jpg');

set(handles.edit_TX,'String','Left');

IsConnect = handles.status_com;

s = handles.com;

if (IsConnect == 1)

strTX = get(handles.edit_TX,'String');

%s = handles.com;

fprintf(s,strTX);

else

% no anything

end

handles.com = s;

else



96



axes(handles.left);

cla(handles.left);

set(handles.edit_TX,'String','');

end



if OutMsg == 1

axes(handles.center);

imshow('center.jpg');

set(handles.edit_TX,'String','Center');

IsConnect = handles.status_com;

s = handles.com;

if (IsConnect == 1)

strTX = get(handles.edit_TX,'String');

%s = handles.com;

fprintf(s,strTX);

else

% no anything

end

handles.com = s;

else

axes(handles.center);

cla(handles.center);

set(handles.edit_TX,'String','');

end

end

OutMsgPre = OutMsg;

%step(hVideoOut,RGB);

axes(handles.display);

cla(handles.display);

guidata(hObject, handles);

set(handles.display);

imshow(RGB);

end



F. Nút Stop.

% hObject



handle to stop_pushbutton6 (see GCBO)



% eventdata



reserved - to be defined in a future version of MATLAB



% handles



structure with handles and user data (see GUIDATA)



97



global vid hVideoSrc

cla(handles.webcam);

closepreview;

delete(vid);

cla(handles.left);

cla(handles.right);

cla(handles.center);

cla(handles.webcam);

cla(handles.display);

set(handles.startwebcam_pushbutton,'String','Start Webcam');

set(handles.startwebcam_pushbutton,'Enable','on');

set(handles.dentr_pushbutton5,'String','Detection & Tracking');

set(handles.dentr_pushbutton5,'Enable','on');

set(handles.edit_RX,'String','');

set(handles.edit_TX,'String','');

set(handles.edit_Send,'String','');



clear all;



G. Nút Exit.

% hObject



handle to exit_pushbutton7 (see GCBO)



% eventdata



reserved - to be defined in a future version of MATLAB



% handles



structure with handles and user data (see GUIDATA)



IsConnect = handles.status_com;

user_response = modalog('Title','Confirm Exit');

switch user_response

case 'No'

% do nothing

case 'Yes'

IsConnect = handles.status_com;

if (IsConnect == 1)

s = handles.com;

%record(s,'off');

fclose(s);

clear s;

end;

closereq;

end



98



H. Nút Connect.

% hObject



handle to button_Connect (see GCBO)



% eventdata



reserved - to be defined in a future version of MATLAB



% handles



structure with handles and user data (see GUIDATA)



IsConnect = handles.status_com;



if (IsConnect == 0), % if comport is disconnected so set up and open it.



IsConnect = 1;



% Setting for Serial Object

s = serial(get_stringPopup(handles.popup_ComPort));



%



COM port

s.BaudRate



= get_doublePopup(handles.popup_BaudRate);



Baud rate



% Setting for reat direction

s.ReadAsyncMode = 'continuous';



s.BytesAvailableFcnCount = 1;



s.Terminator = 'CR'

s.BytesAvailableFcnMode = 'terminator';%'terminator';



%

% Declare some callback functions or interrupt functions.

%

s.BytesAvailableFcn = @BytesAvailable_Callback;

s.OutputEmptyFcn



= @OutputEmpty_Callback;



s.BreakInterruptFcn = @BreakInterrupt_Callback;

s.ErrorFcn



= @Error_Callback;



s.PinStatusFcn



= @PinStatus_Callback;



s.BreakInterruptFcn = @BreakInterrupt_Callback;

s.TimerFcn



= @Timer_Callback;



99



%



% Open port to operate

fopen(s);



%record(s,'on')



% Changing the text displayed on the button named Connect or

Disconnect

% depend on the status of comport.



temp = s.status;

if(temp == 'open'),

set(handles.button_Connect,'String','Disconnect');

set(handles.edit_StatusConnect,'String','Connected');

else

set(handles.edit_StatusConnect,'String','A Problem occour..');

end;



% Hide all setting popup menu.

set(handles.popup_ComPort,'Enable','off');

set(handles.popup_BaudRate,'Enable','off');



else

%

% If Com Port is opened so close it

%

s = handles.com;



set(handles.button_Connect,'String','Connect');

set(handles.edit_StatusConnect,'String','Disconnected');

%record(s,'off')



% close serial port and delete serial object.

fclose(s);

IsConnect = 0;

delete(s);



100



% Show all setting popup menu.

set(handles.popup_ComPort,'Enable','on');

set(handles.popup_BaudRate,'Enable','on');



end % end of if.



% Save all data to all fields of handles.

handles.com = s;

handles.status_com = IsConnect;

guidata(hObject,handles);



I. Nút Send.

% hObject



handle to button_Send (see GCBO)



% eventdata



reserved - to be defined in a future version of MATLAB



% handles



structure with handles and user data (see GUIDATA)



IsConnect = handles.status_com;

s = handles.com;



if (IsConnect == 1)

strTX = get(handles.edit_Send,'String');

fprintf(s,strTX);

end



handles.com = s;

guidata(hObject,handles);



101



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

E. Nút Detection & Tracking.

Tải bản đầy đủ ngay(0 tr)

×