watershed function provided by EmguCv

2019-08-27 01:07发布

问题:

I want to use watershed function provided by emgucv.I used the following code but all I get is a white picture.Please help me and correct this code.Thanks.

Image im;
    Bitmap bm;
    Bitmap bmF;
    private void button1_Click(object sender, EventArgs e)//setting the background image
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            im = Image.FromFile(openFileDialog1.FileName);
           bm = new Bitmap(im);

        }
        panel1.BackgroundImage = im;
        panel1.Width = im.Width;
        panel1.Height = im.Height;
        panel1.Visible = true;


    }

    private void button2_Click(object sender, EventArgs e)
    {
        watershed(bm);
    }

    private void watershed(Bitmap bm)
    {
        Image<Bgr, Byte> imWa = new Image<Bgr, byte>(bm);
        Image<Gray, Int32> imgr = new Image<Gray, int>(imWa.Width, imWa.Height);
        Rectangle rec = imWa.ROI;
        imgr.Draw(new CircleF(new PointF(rec.Left + rec.Width / 2.0f, rec.Top + rec.Height / 2.0f), (float)(Math.Min(imWa.Width, imWa.Height) / 4.0f)), new Gray(255), 0);
        CvInvoke.cvWatershed(imWa, imgr);
        bmF=new Bitmap(bm.Width,bm.Height);
        bmF= imgr.ToBitmap();
        panel1.BackgroundImage = (Image)bmF;
        panel1.Invalidate();
    }

回答1:

You need to better prepare your mask file for the watershed (i.e. imgr).

For this purpose you need to set all to zero first. You can do that by calling:

CvInvoke.cvZero(imgr);

Then you should introduce at least a second "class". Hence you could draw a second circle with different coordinates (something belonging to the background). To be on the safe side use a different greyvalue for the first cirle (e.g. new Gray(100)) than for the second one (e.g. new Gray(200)).

You will get your result in your mask file imgr at the end, with the two classes showing in different greyvalues.

I am not really sure you need the ROI bit...



回答2:

Instead of:

bmF= imgr.ToBitmap();

Try this:

bmF= imgr.Convert<Gray,byte>().ToBitmap();