How can I change this code to detect and track black objects of white background?
Mostra commenti meno recenti
I got this code from the internet for detecting and tracking red objects, I made some modifications in it to detect black objects
function redObjectTrack()
%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.
vid = videoinput('winvideo',2);
% Set the properties of the video object
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;
%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);
% 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(25*data(:,:,3), rgb2gray(data));
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,10);
% 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');
% Display the image
imshow(data)
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(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', 'yellow');
end
hold off
end
% Both the loops end here.
% Stop the video aquisition.
stop(vid);
% Flush all the image data stored in the memory buffer.
flushdata(vid);
% Clear all variables
clear all
The change I made is subtracting the blue color component and strengthening to act like black instead of subtracting the red colors components %diff_im = imsubtract(25*data(:,:,3), rgb2gray(data)); but my application is to detect black rope with knots on it, what I did is detecting the whole rope as a one object. how can I detect the knots on the rope so that each knot appear as a single object?? I'm taking input from the Webcam and the rope with knots looks like the attached image

%
Risposte (3)
Image Analyst
il 16 Ago 2017
Usually when people ask for image processing advice, they show us the image. Failing that about all I can say is to try putting a ~ in front of the im2bw() to flip the foreground and background:
diff_im = ~im2bw(diff_im,0.18);
Or simply do
diff_im = diff_im < (0.18 * 255); % If it's an 8 bit image.
If not, then attach your image and we'll see what I can do.
1 Commento
Ahmad Ali
il 16 Ago 2017
Image Analyst
il 16 Ago 2017
Modificato: Image Analyst
il 16 Ago 2017
0 voti
See my attached code for tracking a green Sharpie marker in a video. Adapt the thresholds to get a red object instead.
See other color segmentation demos in my File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862&sort=downloads_desc
1 Commento
Hershey Wiseman
il 11 Mag 2018
Could you answer this question when you get a chance please? https://www.mathworks.com/matlabcentral/answers/399980-how-do-i-change-my-program-to-recognizes-red-images-from-a-video-and-puts-a-red-box-around-it-to-gre?s_tid=prof_contriblnk
Usman Ali
il 12 Ott 2017
0 voti
How can i write code in matlab and Android development software for Plant disease detection and diagnosis
Categorie
Scopri di più su Video Formats and Interfaces in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!