Click here to Skip to main content
15,884,986 members
Articles / Desktop Programming / Win32
Tip/Trick

Binarizing image (Thresholding) using EmguCV

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
6 Jul 2013CPOL 34K   7   4
Step by step method of thresholding a image using the EmguCV API in C#

For a grayscale image this is simple:

C#
OpenFileDialog open = new OpenFileDialog();
C#
open.Filter = "Image Files (*.tif; *.dcm; *.jpg; *.jpeg; *.bmp)|*.tif; *.dcm; *.jpg; *.jpeg; *.bmp";
int threshold_value = 50; //0-255
if (open.ShowDialog() == DialogResult.OK)
{
    Image<Gray,Byte> img = new Image<Gray,Byte>(open.FileName);
    pictureBox1.Image = img.ToBitmap(); //Display if you want
    img = img.ThresholdBinary(new Gray(threshold_value), new Gray(255));
   //Image<Gray,Byte> Binary_Image = img.ThresholdBinary(new Gray(threshold_value),
   //   new Gray(255)); // to get it saved in seperate variable 
    pictureBox2.Image = img .ToBitmap(); //display results in different picturebox
}

If you want to play with things a little you can add a trackbar to your form, put the minimum to 0 and maximum to 255, and use this code.

C#
private void trackBar1_Scroll(object sender, EventArgs e)
{
    int trackbar = trackBar1.Value;
    label1.Text = trackbar.ToString(); //use a label to display trackbar value
    if (img != null)
    {
        using (Image<Gray,Byte> Gray = img.ThresholdBinary(new Gray(trackbar), new Gray(255)))
        {
            pictureBox2.Image = Gray.ToBitmap();
        }
    }
}

For a colour image things are a little different:

C#
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.tif; *.dcm; *.jpg; *.jpeg; *.bmp)|*.tif; *.dcm; *.jpg; *.jpeg; *.bmp";
int Blue_threshold = 50; //0-255
int Green_threshold = 50; //0-255
int Red_threshold = 50; //0-255
if (open.ShowDialog() == DialogResult.OK)
{
    Image<bgr,Byte> img_colour = new Image<bgr,Byte>(open.FileName);
    pictureBox1.Image = img_colour.ToBitmap(); //Display if you want
    img_colour = img_colour.ThresholdBinary(
      new Bgr(Blue_threshold, Green_threshold, Red_threshold), new Bgr(255, 255, 255));
    pictureBox2.Image = img_colour.ToBitmap();//display results in different picturebox
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Amir Mohammad Nasrollahi27-Jul-13 22:18
professionalAmir Mohammad Nasrollahi27-Jul-13 22:18 
QuestionThank you! Pin
Ahmad Davudi21-Jul-13 2:50
Ahmad Davudi21-Jul-13 2:50 
GeneralMy vote of 3 Pin
Amin Esmaeily6-Jul-13 22:00
professionalAmin Esmaeily6-Jul-13 22:00 
GeneralRe: My vote of 3 Pin
hirosht7-Jul-13 1:17
hirosht7-Jul-13 1:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.