How to detect blue spot in an image

4 views (last 30 days)
Hazim Nasir
Hazim Nasir on 2 Nov 2019
Commented: Hazim Nasir on 3 Nov 2019
Hello
I have used web camera to detect the motion of specific objects that are shown in the attached image.
I can track red objects by set data(:,:,1), the only problem I can not detect blue objects or green objects even when I set data(:,:,3)
I have used the following code for this issue:
vid = videoinput('winvideo',3);
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 1;
start(vid)
h=axes;
while(ishandle(h))
data = getsnapshot(vid);
diff_im = imsubtract(data(:,:,3), rgb2gray(data)); % blue color
diff_im = medfilt2(diff_im, [3 3]);
diff_im = im2bw(diff_im,0.18);
diff_im = bwareaopen(diff_im,300);
bw = bwlabel(diff_im, 8);
stats = regionprops(bw, 'BoundingBox', 'Centroid');
imshow(data);
hold on
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
flushdata(vid);
end
stop(vid);
The blue and green colors can be seen by eye but the program can not detect them so any help to detect blue and green objects
thank you in advance
Hazim
  2 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 3 Nov 2019
Edited: KALYAN ACHARJYA on 3 Nov 2019
Can you clip attach the another image with marked on ROI (blue spot), which you are talking about?
Hazim Nasir
Hazim Nasir on 3 Nov 2019
thank you KAYAN, I have maked the blue spots in white circles
spot.jpg

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 3 Nov 2019
You're using a very crude way of detecting colour (working in RGB is not a good idea for colour detection) and the quality of your image is very poor (Huge light reflection and poor colour contrast) so it's no suprise that your algorithm doesn't work. In particular, the tiles that you call blue have equal amounts of green and blue, with some areas having more green than blue.
You'll have to use a better algorithm (working in hsv or lab) and improve the quality of your image.
  1 Comment
Hazim Nasir
Hazim Nasir on 3 Nov 2019
Thank you Guillaume
I actually the point is to work with poor resolution images
we can detect blue color by eye, so I hope there is away to detect them by algorthim

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!