Is this a good GUI for vehicle detection?
2 次查看(过去 30 天)
显示 更早的评论
function ImageDetectionGUI
% Create the GUI
fig = uifigure('Name', 'Vehicle Image Detection GUI', 'Position', [100 200 400 350]);
% Add a button for video detection
btn_video_detection = uibutton(fig, 'push', 'Text', 'Switch to Video Detection', 'Position', [50 260 300 50], 'ButtonPushedFcn', @videoDetection);
% Add a button to start the detection process
btn_start_detection = uibutton(fig, 'push', 'Text', 'Start Detection', 'Position', [50 200 300 50], 'ButtonPushedFcn', @startDetection);
% Add a button for pedestrian detection
btn_pedestrian_detection = uibutton(fig, 'push', 'Text', 'Pedestrian Detection', 'Position', [50 140 300 50], 'ButtonPushedFcn', @pedestrianDetection);
% Add a button for lane detection
btn_lane_detection = uibutton(fig, 'push', 'Text', 'Lane Detection', 'Position', [50 80 300 50], 'ButtonPushedFcn', @laneDetection);
% Add a button for plate detection
btn_plate_detection = uibutton(fig, 'push', 'Text', 'Plate Number Detection', 'Position', [50 20 300 50], 'ButtonPushedFcn', @plateDetection);
% Initialize variables
image_path = '';
the_Image = [];
foregroundDetector = [];
structuringElement = [];
blobAnalysis = [];
% Define the callback function for the "Start Detection" button
function startDetection(~, ~)
% Get path of the image
[file, path] = uigetfile({'*.jpg;*.png;*.bmp;*.tif'}, 'Select an image');
if isequal(file,0)
return
end
image_path = fullfile(path, file);
% Read the image
frame = imread(image_path);
% Create a foreground detector
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, 'NumTrainingFrames', 50);
% Create a morphological structuring element for noise removal
structuringElement = strel('square', 3);
% Create a blob analysis object for car detection
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, 'AreaOutputPort', false, 'CentroidOutputPort', false, 'MinimumBlobArea', 150);
% Process the first frame to initialize variables
foregroundMask = step(foregroundDetector, frame);
noiseFreeMask = imopen(foregroundMask, structuringElement);
bbox = step(blobAnalysis, noiseFreeMask);
numCars = size(bbox, 1);
% Display the image with bounding boxes and number of cars
figure;
imshow(insertShape(frame, 'Rectangle', bbox, 'Color', 'green'));
title(sprintf('%d Cars Detected', numCars));
end
function pedestrianDetection(~, ~)
% Get path of the image
[file, path] = uigetfile({'*.jpg;*.png;*.bmp;*.tif'}, 'Select an image');
if isequal(file,0)
return
end
image_path = fullfile(path, file);
% Read the image
frame = imread(image_path);
% Initialize YOLOv2 object detector for car, people and bicycles detection
net = yolov2ObjectDetector;
% Detect cars, people and bicycles in frame
[bboxes, scores, labels] = detect(net, frame);
indices = find(labels=='car' | labels=='truck' | labels=='person' | labels=='bicycle');
bboxes = bboxes(indices,:);
scores = scores(indices);
labels = labels(indices);
% Draw bounding box around detected objects and change the box color based on the label
detectedObjects = frame;
for j=1:size(bboxes,1)
switch labels(j)
case 'car'
color = 'green';
case 'truck'
color = 'red';
case 'person'
color = 'yellow';
case 'bicycle'
color = 'blue';
otherwise
color = 'green';
end
detectedObjects = insertObjectAnnotation(detectedObjects, 'rectangle', bboxes(j,:), labels(j), 'LineWidth', 2, 'Color', color);
end
% Add text showing number of objects detected
numberOfObjects = size(bboxes, 1);
detectedObjects = insertText(detectedObjects, [10 10], numberOfObjects, 'BoxOpacity', 1, 'FontSize', 14);
% Show the detected objects
imshow(detectedObjects);
end
% Define the callback function for the "Video Detection" button
function videoDetection(~, ~)
% Call the VideoDetectionGUI
menu
end
function laneDetection(~, ~)
% Get path of the image
[file, path] = uigetfile({'*.jpg;*.png;*.bmp;*.tif'}, 'Select an image');
if isequal(file,0)
return
end
image_path = fullfile(path, file);
I = imread(image_path);
% this reads the original image
%---------------------------------------
%---------------------------------------
bin = im2bw(I, 0.7);
% 0.69 is a luminance value and takes values from 0 to 1
% the value is chosen according to quality of output image
% im2bw changes rgb image to black and white image
imshow(I), title('Original') %this command shows images
%---------------------------------------
%---------------------------------------
bin2 = imdilate(bin,strel('line',2,0));
% strel: line --> is to tell the dilation function it is about to dilate a line
% 3: is the required dilation thickness, original thickness is 1
% 0: is the angle of dialtion, if 90 degs is chosed the dialted lines will be perpindicular on the road
% while 0 degree dialted lines while be tangent and colinear with the road
%figure(2), imshow(bin2), title('Dilated')
%---------------------------------------
%---------------------------------------
bin3 = bwmorph(bin2,'thin',inf);
% bwmorph: this function does morphing to shapes in images here we use it to make lines thinner
% inf is somehow a degree of thinning
%figure(3), imshow(bin3), title('Thinned')
%---------------------------------------
%---------------------------------------
% Hough
% hough is based on 3 steps
%--> first step is to transform the black white color after thinning to its hough transform
[H,theta,rho] = hough(bin3);
%figure(4), imshow(H,[],'XData',theta,'YData',rho,'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
%--> second step is to find the peak points (white points) on the hough transform which shouldbe our lines
pek = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = theta(pek(:,2)); y = rho(pek(:,1));
%plot(x,y,'s','color','white');
%--> third step is to use the peak points in hough so that lines in the black white pic can be detected when we reverse the hough transform
lines = houghlines(bin3,theta,rho,pek,'FillGap',5,'MinLength',7);
%figure(5), imshow(bin3), title('Driving Pathes') ,hold on
max_len = 0;
xy(:,:,1) = zeros(2);
for k = 1:length(lines)
xy(:,:,k) = [lines(k).point1; lines(k).point2];
plot(xy(:,1,k),xy(:,2,k),'LineWidth',2,'Color','green');
end
end
1 个评论
Rik
2023-3-20
I don't think this is a question with a good answer. What would make this a good GUI? Should it be easy to use? Should it have a certain succes rate? Should it meet a certain speed performance?
At least it doesn't use GUIDE, so this code will run without any external file requirement and should be easy to maintain.
回答(1 个)
Dinesh
2023-4-26
Hi Ahmad,
I tried running your code on my side. The GUI is simple and is good for a simple application. But still, there are a lot of factors. Ask yourself about
- Is this app covering all the needs that you wanted?
- How visually good it is.
- Do you want it to be simple or bring in more options and add other components?
- How fast it is (w.r.to loading times)
The opinions would change from person to person. Overall, the App is simple, the code does not depend on external dependencies so it can run anywhere and serves the purpose is what I think.
However, if you require any specific help, please elaborate more about the issue that you are facing.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing and Computer Vision 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!