What's the most efficient way of identifying items of a specific shade of colour in an image with matlab?

64 Views Asked by At

im trying to identify a specific shade of green leaves (e.g. navy green) from the attached image. how do i do that in the most efficient way? So far, i'm converting the RGB to HSV and then thresholding the image based on some specific range of saturation and value that will isolate my desired shade. it's working on some images and it's just all over the place on others. i want something that can isolate a specific shade of green in any different image that has slightly different saturation and value (e.g. if the picture was taken with too much light) Image link

pic=imread('image.jpg');
q=rgb2hsv(pic);
H=q(:,:,1);
S=q(:,:,2);
V=q(:,:,3);
thresh=S>0.6111 & S<0.6666 & V>0.3888 & V<0.4583;
st=strel('diamond',20);
w=imdilate(thresh,st);
comps=bwconncomp(w,8);
num=comps.NumObjects;
fprintf('The number of leaves is %i',num)

% then i try to have some pointers on the image to show me where matlab has identified the the shade. m = regionprops(w,'centroid');

boxes = cat(1, m.Centroid); 

imshow(pic) 

hold on
plot(boxes(:,1),boxes(:,2), 'b*') 
hold off

Your help will be highly appreciated.

1

There are 1 best solutions below

2
On

Either the HSV color space (hey, S is saturation and V value), where H will give you the hue,or CIE-Lab color space, where euclidean distance will give you how close 2 specific pixel are to each other in color.

This answer explains how to do it for HSV: Segment pixels in an image based on colour (Matlab)

Using combined with CIE-LAB may help if the colors are very close together (like the greens in each leaf), but you should give HSV a shot