Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How to use EmguCV to make Black and White image
from colored or gray image ?
Posted

Hi,

You asked a similar question however it was the opposite way round if you want to do this let me know and I can help. Anyway to threshold to a binary image in EMGU you can simply use .ThresholdBinary syntax.

For a grayscale image this is simple:
C#
OpenFileDialog open = new OpenFileDialog();
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));
    pictureBox2.Image = img .ToBitmap(); //display results in different picturebox
}


You can always do the following if you wish to keep the original image

C#
Image<Gray,Byte> Binary_Image = img.ThresholdBinary(new Gray(threshold_value), new Gray(255));


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();
        }
    }
}


Although this is not technically a binary image as that would be 1 or 0 it will produce the expected black and white effect.

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
}


I hope this helps you out if you need any help feel free to ask,

Cheers,
Chris
 
Share this answer
 
v2
Comments
lalit4u 21-Jan-14 8:45am    
Is this possible Automated guided vehicle using white path in threshold if yes then please explain?
Member 10741352 10-May-14 0:09am    
can i have your code
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900