Borrar filtros
Borrar filtros

I need help optimizing my program that currently puts boxes of the respective color around notes in guitar hero 3. It does not do it fast enough or accurately enough.

1 visualización (últimos 30 días)
My code will all be below, my new idea is to put each scan to be run in parallel with the others by using parallel processing, but this is a new concept to me so I was wondering is it something worth looking into for this project. This is the code
function ObjectTrack()
clear all;
imaqreset;
a = imaqhwinfo;
[camera_name, camera_id, format] = getCameraInfo(a);
%Capture the video frames using the videoinput function
% You have to replace the resolution & your installed adaptor name.
cam = webcam('Logitech Camera');
snapshot(cam);
vid = videoinput('macvideo', camera_id, format);
% Set the properties of the video object
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 1; %the app grabs a frame every this number of frames
%start the video aquisition here
start(vid)
% Set a loop that stop after 100 frames of aquisition
while(vid.FramesAcquired<=800)
% Get the snapshot of the current frame
data = getsnapshot(vid);
cropData = imcrop(data, [950, 470, 550, 400]);
% comment
[BWgreen, greenImage] = createMaskGreen(cropData);
[BWred, redImage] = createMaskRed(cropData);
[BWyellow, yellowImage] = createMaskYellow(cropData);
[BWblue, blueImage] = createMaskBlue(cropData);
[BWorange, orangeImage] = createMaskOrange(cropData);
% Remove all those pixels less than 300px
BWgreen = bwareafilt(BWgreen,[20, 60]);
BWred = bwareafilt(BWred,[20, 60]);
BWyellow = bwareafilt(BWyellow,[20, 60]);
BWblue = bwareafilt(BWblue,[20, 60]);
BWorange = bwareafilt(BWorange,[20, 60]);
% Label all the connected components in the image.
bwG = bwlabel(BWgreen, 8);
bwR = bwlabel(BWred, 8);
bwY = bwlabel(BWyellow, 8);
bwB = bwlabel(BWblue, 8);
bwO = bwlabel(BWorange, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
statsG = regionprops(bwG, 'BoundingBox', 'Centroid');
statsR = regionprops(bwR, 'BoundingBox', 'Centroid');
statsY = regionprops(bwY, 'BoundingBox', 'Centroid');
statsB = regionprops(bwB, 'BoundingBox', 'Centroid');
statsO = regionprops(bwO, 'BoundingBox', 'Centroid');
% Display the image
imshow(cropData)
hold on
line([000, 550],[350, 350],'Color','black','LineWidth',7)
hold on
%This is a loop to bound the green objects in a rectangular box.
for object = 1:length(statsG)
bb = statsG(object).BoundingBox;
bc = statsG(object).Centroid;
rectangle('Position',bb,'EdgeColor','g','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', 'green');
end
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(statsR)
bb = statsR(object).BoundingBox;
bc = statsR(object).Centroid;
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', 'red');
end
%This is a loop to bound the yellow objects in a rectangular box.
for object = 1:length(statsY)
bb = statsY(object).BoundingBox;
bc = statsY(object).Centroid;
rectangle('Position',bb,'EdgeColor','y','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');
end
%This is a loop to bound the blue objects in a rectangular box.
for object = 1:length(statsB)
bb = statsB(object).BoundingBox;
bc = statsB(object).Centroid;
rectangle('Position',bb,'EdgeColor','b','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', 'blue');
end
%This is a loop to bound the orange objects in a rectangular box.
for object = 1:length(statsO)
bb = statsO(object).BoundingBox;
bc = statsO(object).Centroid;
rectangle('Position',bb,'EdgeColor','k','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', 'black');
end
hold off
end
% Both the loops end here.
hold off
% Stop the video aquisition.
stop(vid);
% Flush all the image data stored in the memory buffer.
flushdata(vid);
% Clear all variables
clear all
sprintf('%s','Program has now ended')
%%getCameraInfo class
function [camera_name, camera_id, resolution] = getCameraInfo(a)
camera_name = 'Logitech Camera';
camera_info = imaqhwinfo('macvideo');
camera_id = 2;
resolution = 'YCbCr422_1920x1080';
%%createMaskGreen class
function [BW,maskedRGBImage] = createMaskGreen(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 14-May-2018 %------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.269;
channel1Max = 0.420;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.266;
channel2Max = 0.512;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.422;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
%%createMaskRed class
function [BW,maskedRGBImage] = createMaskRed(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 14-May-2018 %------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.971;
channel1Max = 0.036;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.217;
channel2Max = 0.453;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.487;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds sliderBW = ( (I(:,:,1) >= channel1Min) | (I(:,:,1) <= channel1Max) ) & ... (I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ... (I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max); BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero. maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
%%createMaskYellow class
function [BW,maskedRGBImage] = createMaskYellow(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 14-May-2018 %------------------------------------------------------
% Convert RGB image to chosen color space I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.244;
channel1Max = 0.413;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.292;
channel2Max = 0.483;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.596;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image. maskedRGBImage = RGB;
% Set background pixels where BW is false to zero. maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
%%createMaskBlue class function [BW,maskedRGBImage] = createMaskBlue(RGB) %createMask Threshold RGB image using auto-generated code from colorThresholder app. % [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using % auto-generated code from the colorThresholder app. The colorspace and % range for each channel of the colorspace were set within the app. The % segmentation mask is returned in BW, and a composite of the mask and % original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 14-May-2018 %------------------------------------------------------
%Convert RGB image to chosen color space
I = rgb2hsv(RGB);
%Define thresholds for channel 1 based on histogram settings
channel1Min = 0.538;
channel1Max = 0.635;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.337;
channel2Max = 0.566;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.703;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
%%createMaskOrange class
function [BW,maskedRGBImage] = createMaskOrange(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 14-May-2018 %------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings channel1Min = 0.085; channel1Max = 0.203;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.225;
channel2Max = 0.401;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.625;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
  2 comentarios
Florian Morsch
Florian Morsch el 18 de Mayo de 2018
One thing i noticed might help you right on the spot:
Move every fixed values out of a loop. For example in your first while loop you have
% Set a loop that stop after 100 frames of aquisition
while(vid.FramesAcquired<=800)
% Get the snapshot of the current frame
data = getsnapshot(vid);
cropData = imcrop(data, [950, 470, 550, 400]);
% comment
[BWgreen, greenImage] = createMaskGreen(cropData);
[BWred, redImage] = createMaskRed(cropData);
[BWyellow, yellowImage] = createMaskYellow(cropData);
[BWblue, blueImage] = createMaskBlue(cropData);
[BWorange, orangeImage] = createMaskOrange(cropData);
With every run through the loop you re-asign the crop box. Move that out of the loop.
CropMask = [950, 470, 550, 400];
% Set a loop that stop after 100 frames of aquisition
while(vid.FramesAcquired<=800)
% Get the snapshot of the current frame
data = getsnapshot(vid);
cropData = imcrop(data, CropMask);
% comment
[BWgreen, greenImage] = createMaskGreen(cropData);
[BWred, redImage] = createMaskRed(cropData);
[BWyellow, yellowImage] = createMaskYellow(cropData);
[BWblue, blueImage] = createMaskBlue(cropData);
[BWorange, orangeImage] = createMaskOrange(cropData);
Things like that dont look like much, but can add up pretty quickly.
This link migh have some usefull information for you:
sloppydisk
sloppydisk el 18 de Mayo de 2018
Can you please add the code as an attachment instead of in text to make the post more readable?

Iniciar sesión para comentar.

Respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by