EmguCv提供的分水岭功能 [英] watershed function provided by EmguCv

查看:277
本文介绍了EmguCv提供的分水岭功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用emgucv提供的分水岭函数。我使用了以下代码,但我得到的只是一张白色的图片,请帮助我并纠正此代码。谢谢。

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


推荐答案

您需要更好地准备分水岭的遮罩文件(即imgr)。

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

然后,您至少应引入第二个类。因此,您可以绘制具有不同坐标的第二个圆(属于背景)。为了安全起见,对第一个线圈(例如 new Gray(100))使用不同于第二个灰度值(例如 new Gray)的灰度值(200))。

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

您将在掩码文件imgr的最后得到结果,这两个类以不同的灰度值显示。

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

我不确定您是否需要ROI位...

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

这篇关于EmguCv提供的分水岭功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆