Problem extracting data in a for loop
1 view (last 30 days)
Show older comments
Hi everyone
I'm having a problem about extracting data from a for loop
function [BWdfill,CC,X,Y] = ObjectDetection(RGB)
%Color detection part
% Convert RGB image to chosen color space
B = step(RGB);
A = rgb2hsv(B);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.679;
channel1Max = 0.836;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.322;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.361;
channel3Max = 0.790;
% Create mask based on chosen histogram thresholds
sliderBW = (A(:,:,1) >= channel1Min ) & (A(:,:,1) <= channel1Max) & ...
(A(:,:,2) >= channel2Min ) & (A(:,:,2) <= channel2Max) & ...
(A(:,:,3) >= channel3Min ) & (A(:,:,3) <= channel3Max);
BW = sliderBW;
BWs = bwareaopen(BW,100);
%Edgedetection part
BWss = edge(BWs,'canny',0.4);
se90 = strel('line',3,90);
se0 = strel('line',3,0);
BWsdil = imdilate(BWss,[se90 se0]);
BWdfill = imfill(BWsdil,'holes');
CC = bwconncomp(BWdfill,8);
info = regionprops(CC,'Boundingbox');
[labeledImage, numBlobs] = bwlabel(BWdfill);
props = regionprops(labeledImage, 'Centroid', 'BoundingBox');
[labeledImage, numberOfObject] = bwlabel(BWdfill);
%Draw a bounding box
hold on
for k = 1:length(props)
bc = props(k).Centroid;
BB = props(k).BoundingBox;
rectangle('Position', [BB(1),BB(2),BB(3),BB(4)],'EdgeColor','c','LineWidth',2) ;
% rectangle('Position',props(k).BoundingBox,'EdgeColor','b','LineWidth',2);
x = props(k).Centroid(1);
y = props(k).Centroid(2);
X(k) = x;
Y(k) = y;
% ValueX = props(k).Centroid(1);
% ValueY = props(k).Centroid(2);
plot(x, y, 'r+', 'MarkerSize', 13, 'LineWidth', 2);
% Find the left edge and right edge of the bounding box.
% leftColumn = BB(1) + 0.5;
% rightColumn = BB(1) - 0.5;
% % Find the distance of them to the centroid.
% leftDistance(k) = x - leftColumn
% rightDistance(k) = rightColumn - x
% Plot a line between them
% line([leftColumn, rightColumn], [y, y], 'Color', 'g');
% 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');
drawnow
end
end
My code above show you the way i draw a bounding box to cover an object
However, The x,y value from the for loop which I need to use can't be extracted
This is the error:
Output argument "Value" (and maybe others) not
assigned during call to
"testfunction>Objectdetection".
Error in testfunction (line 10)
[z,n,x] = Objectdetection(vid);
Can someone help me
Thanks a lot
0 Comments
See Also
Categories
Find more on Color in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!