Friday, June 28, 2013

EmguCV binary image ( Thresholding )

For a grayscale image this is simple: 


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));
       //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.


    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:
   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
    }

Restricting the Opening File Type in C# in OpenFileDialog

 // Calling the OpenFileDialog           
                  OpenFileDialog Openfile = new OpenFileDialog(); 
// Defining the Allowed File Types
            Openfile.Filter = "Image Files ( *.jpg; *.jpeg)| *.jpg; *.jpeg; "; 

Resizing a Image in ENGU CV

// To get orginal image from the OpenFileDialog
Image<Bgr, Byte> captureImage = new Image<Bgr, byte>(openImageFileDialog.FileName);
// To resize the image 
Image<Bgr, byte> resizedImage = captureImage.Resize(width, height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
/////////////////////OR///////////////////////
//open the image using open file dialog
OpenFileDialog Openfile = new OpenFileDialog();
            if (Openfile.ShowDialog() == DialogResult.OK)
            {
                Image<Bgr, Byte> imgOriginal = new Image<Bgr, byte>(Openfile.FileName); //Open
                Image<Bgr, byte> resizedImage = imgCurrent.Resize(width, height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); //Resize
            }

Source : http://stackoverflow.com/questions/15052419/how-to-resize-image-with-imagebox-emgucv