I am afraid that I cannot get good results from the image after your processing (the 2nd image link). I tried to use a slightly different method to extract the red light in your original image. Hopefully it can help you a little bit. First of all, you may need to convert the RGB to the Lab space. Actually it is very suitable to handle the image in that space, and some operations can maximize the difference between red and green light. Since there is only a red light in your image, I won't go that complicated. I only used b-channel for further processing.
file='https://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-prn1/1524779_10153642995125182_414999862_n.jpg';
I=imread(file);Irgb=I;
colorTransform = makecform('srgb2lab');
I = applycform(I, colorTransform);
b = I(:,:,3);
I=double(b);
Imin=min(I(:));
Imax=max(I(:));
I=(I-Imin)/(Imax-Imin);
imshow(I)
Will get you:
Next, we remove the line/square structure, indeed they have very strong intensity that may impact our further result:
se = strel('line',20,0);
I = imtophat(I,se);
figure,imshow(I)
You will have:
In the next step we are trying to find out the local maximum value in the image:
roi=20;thresh=0.5;
local_extr = ordfilt2(I, roi^2, ones(roi));
% Get local maxima and reject candidates below a threshold
result = (I == local_extr) & (I > thresh);
% Get indices of extrema
[r, c] = find(result);
% Show them
figure;imshow(I, []); hold on;
plot(c, r, 'r.');hold off
You will see:
In fact the red light has already been tagged (I saw another red point in your original image, but I assume that is a walking sign with the red hand shape instead of a circle red light, so herein I won't consider that part.)
Finally we use the regionprops
with the eccentricity
property, and find out the region with the least eccentricity, that will look more alike a circle shape:
for i=1:length(c)
I1=I(r(i)-roi:r(i)+roi,c(i)-roi:c(i)+roi);
I1=im2bw(I1);
labeledImage = bwlabel(I1);
blobMeasurements = regionprops(labeledImage,'eccentricity');
if isempty(blobMeasurements)
e(i)=Inf;
else
e(i)=blobMeasurements.Eccentricity;
end
end
[a,idx]=min(e);
res = zeros(size(I,1),size(I,2),3);
i=idx;
res( r(i)-roi:r(i)+roi,c(i)-roi:c(i)+roi ,1) = Irgb(r(i)-roi:r(i)+roi,c(i)-roi:c(i)+roi,1);
res( r(i)-roi:r(i)+roi,c(i)-roi:c(i)+roi ,2) = Irgb(r(i)-roi:r(i)+roi,c(i)-roi:c(i)+roi,2);
res( r(i)-roi:r(i)+roi,c(i)-roi:c(i)+roi ,3) = Irgb(r(i)-roi:r(i)+roi,c(i)-roi:c(i)+roi,3);
figure,imshow(uint8(res))
The result: