santosh patil
santosh patil

Reputation: 85

excessive green color extraction implementation

i want to segment image pixels into vegetation and Non vegetation. For this excessive green color extraction algorithm is developed.the algorithm is shown below.

Outimage (x,y,z) = inimage (x,y,z)

if { inimage (x,y,r) < (x,y,g) inimage (x,y,b) < (x,y,g) }

outimage(x,y,z) = 0 otherwise*

where outimage (x,y,z) is the output image after excessive green segmentation saved in jpg format, inimage(x,y,z) is the image acquired by an camera, x is the no of pixels in each row, y is the no of pixels in each column and z is the primary color plane for red the z is equal to 1, for green the z is 2 and for blue the z is 3.

i am not getting how to implement this so please help me to implement it.or just give some rough idea or suggestion how i can implement it.

input image:

input image

output:

i want the output to be in this format after applying above mentioned algorithm

Upvotes: 1

Views: 1724

Answers (2)

andrew
andrew

Reputation: 2469

An alternative solution is to convert the image to HSV colorspace if you aren't familiar with it, it converts the RGB values to Hue (color), Saturation(how vivid the color is), ~brightness(the illumination level). The nice thing about this is that you can look for the same color in all lighting conditions.

Other than how we obtain our mask (using green hues) it's the exact same process as the rgb version took

inimage = imread('plants.png');
hsv_im = rgb2hsv(inimage);

%plots only the hues so we can get an idea of what to segment out
hue_channel = 1;
figure(1)
imshow(hsv_im(:,:,hue_channel));   %displays only the hue channel/layer
colormap(hsv)            %uses the hue colormap
colorbar                 %displays the colorbar

%masks the greenish regions (this is subjective)
mask = hsv_im(:,:,hue_channel) < 0.5 & hsv_im(:,:,hue_channel) > 0.2;

%applies the mask to all 3 color layers
outimage = bsxfun(@times, inimage, uint8(mask));
figure(2)
subplot(1,2,1);imshow(inimage);title('original image')
subplot(1,2,2),imshow(outimage);title('segmented image')

enter image description here enter image description here

Upvotes: 1

Luis Mendo
Luis Mendo

Reputation: 112749

Build a 2D mask and then use bsxfun to apply it to all color components (third-dim slices):

inimage = imread('filename'); %// type uint8
mask = inimage(:,:,1)<inimage(:,:,2) & inimage(:,:,3)<inimage(:,:,2); %// 2D mask
outimage = bsxfun(@times, inimage, uint8(mask)); %// apply mask replicated along 3rd dim

enter image description here

Upvotes: 4

Related Questions