Red and Green color filterRed and Green color segmentation

Color Segmentation is very basic problem in digital image processing. Most of the time we need to segment Object based on their colors. So Color segmentation comes in handy in such scenarios. Today, we are going to explain how to segment RGB Image based on some basic Primary Colors. This technique could also be applied to segment colors other than primary colors. So We will also see that Yellow and Orange Colors are also segmented while applying threshold for RED Colors.

Read Image In MATLAB

First of all lets take a sample image on which we are going to apply all our color segmentation algorithm. This Image comes with MATLAB Image Processing Toolbox. So You can just type this command in command window in MATLAB.

>>figure,imshow(x);Code language: JavaScript (javascript)

Convert Image to Gray-scale and Get Red Matrix

Now we have our image which is in RGB Color Space. We can extract Red Matrix and Convert RGB Image into grayscale Image.

>>gray_x = rgb2gray(x);
>>red_x = x(:,:,1);
Original Peppers Image
Original RGB Image

Subtract Image

Now if we subtract this gray scale Image from Red matrix then we will get the correct light shadings and only red objects. Also note that here we will also get red color objects which are mixture of other colors as well. Like Yellow and Orange Color who have Green color mixed with Red Color. Because these colors contain 255 value of RED and some portion of Green in them. Because of High Red portion we will not able to directly eliminate these objects. We will see how to eliminate these colors at end of this tutorial. For now just get a subtracted image like this

>>sub_x = imsubtract(red_x,gray_x);
After subtracting Grayscale image from Red Matrix of original RGB Image.
After Subtracting Gray-scale from Red Matrix

Convert to binary and Mask RED Objects

Now we need to convert this image into binary image with specified threshold value and After that threshing we need to create a mask and finally convert that mask into 3 dimensions so that we would be able to apply that mask onto our RGB Image. Which is also a 3 dimensional Image, so we need a 3 dimensional mask to apply on. Finally We will remove all other pixels than the mask[2] and get the result as follows.

>>bin_x  = im2bw(sub_x,0.21);
>>mask  = ~bin_x;
>>mask3 = cat(3,mask,mask,mask) 
>>new_x = x;   %create a backup copy of original image
>> new_x(mask3) = 0;
Red Objects including Yellow and Orange
Red color segmentation with yellow and Orange

Here you can see how Yellow and Orange Peppers are also displayed along with Red one which were our desired peppers. But Now we are getting wrong objects detected as our desired objects.

RED and Green Color Segmentation

Now we are going to filter out RED color along with Green Color. The reason to choose Green color rather than blue is because of our unwanted Yellow and Orange Objects which holds Green in them. So We are actually building a foundation toward Eliminating those unwanted color objects. So here is the code to combine two colors and detected more than one color in a Image or Video frame.

>>green_x = x(:,:,2);  %get green matrix just like the red one
>>gsub_x = imsubtract(green_x,gray_x); %subtract gray from green 
>>figure,imshow(gsub_x);Code language: JavaScript (javascript)

Now if you notice that the image now contain very less information. Because of very low portion of green in it. So we need to reduce the threshold value for detecting green color in this Image. So just apply this value and see the results now.

>>gbin_x = im2bw(gsub_x,0.01);
Green Peppers after green color segmentation
Green Peppers with Green color filtration

Combine Red binary and Green binary with subtraction

Now It may seem confusing that although we are subtracting images but the resultant effect would be a combination of two colors. This is just because the resultant image will be a 1 if one color is present in one image but not in other. But if both of the Images contains 1 then the resultant image will get 0 at their.

%% Read Original Image

gray_x = rgb2gray(x);
%% Extract Red Space 
red_x = x(:,:,1);
rsub_x = imsubtract(red_x,gray_x);

%% Red to binary
rbin_x = im2bw(rsub_x,0.3);
rbmask = im2bw(imcomplement(rbin_x));
rbmask = cat(3,rbmask,rbmask,rbmask);
only_red_x = x;
figure,subplot(2,1,1),imshow(x),title('original Peppers Image');
subplot(2,1,2),imshow(only_red_x),title('red objects found');

%% display red_sub 
figure,imshow(x),title('Original Peppers Image')

green_x = x(:,:,2); 
gsub_x = imsubtract(green_x,gray_x);
figure,imshow(gsub_x),title('Green Matrix')
gbin_x = im2bw(gsub_x,0.01);
figure,imshow(gbin_x),title('green to binary with 0.01 threshold')

%% mask only green objects

only_green_x = x;
ogx_mask = im2bw(double(imcomplement(gbin_x)));
ogx_mask = cat(3,ogx_mask,ogx_mask,ogx_mask);
figure,imshow(only_green_x),title('Green Objects Subtracted');

%% subtract together

comb_x = x;
abs_comb_x = x;
combined_x = imsubtract(rbin_x,gbin_x);
abs_cx = abs(imsubtract(rbin_x,gbin_x));

%% apply masks
mk1 = im2bw(imcomplement(combined_x));
mk2 = im2bw(imcomplement(abs_cx));
mk1 = cat(3,mk1,mk1,mk1);mk2=cat(3,mk2,mk2,mk2);

%% plotting
figure,subplot(2,2,1),imshow(combined_x); title('without abs')
subplot(2,2,2),imshow(abs_cx),title('with abs')
subplot(2,2,3),imshow(comb_x),title('without abs x')
subplot(2,2,4),imshow(abs_comb_x),title('with abs x')Code language: JavaScript (javascript)

Here is the combined Effect of both colors

Red and Green Combined

Remove Unwanted Yellow and Orange Color from RED

Now here is the final code which will remove unwanted yellow and Orange Color from the Image and after that we presented the final resultant Image. This image could be enhanced by applying some filtration on binary image but we leave this on to user and may be to our next tutorial.

clear all; close all; clc;

x       = imread('peppers.png');
red_x   = x(:,:,1);
green_x = x(:,:,2);
gray_x  = rgb2gray(x);

sub_rg_gray = imsubtract(sub_rg,gray_x);
only_red_bin = im2bw(sub_rg_gray,0.18);

%% Prepare and Apply Mask
only_red_mask = im2bw(imcomplement( only_red_bin));
only_red_mask = cat(3,only_red_mask,only_red_mask,only_red_mask);
only_red_x = x;

%% display Results
figure,imshow(x),title('original image'),
figure,imshow(only_red_x),title('extracted objects');
Code language: JavaScript (javascript)
Red color Detected in image
Final Red Color Detection in MATLAB


[1]The Motivated Engineer, “Image Processing in MATLAB Tutorial 2 – Color Thresholding,” YouTube. 2020. [Accessed: 04-Apr-2020].

[2]“How to impose Binary mask on rgb color image –  MATLAB Answers – MATLAB Central,”, 2017. [Online]. Available: [Accessed: 04-Apr-2020].

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.