当前位置: 代码迷 >> C# >> 图形分割有关问题
  详细解决方案

图形分割有关问题

热度:103   发布时间:2016-05-05 03:55:49.0
图形分割问题
我想从一张图片分割出感兴趣的部分应该怎么做呢具体比如这是一张医学图片我想分割出里面肝脏的部分,应该怎么做呢
如下图所示我想分割时我用红色勾勒出来那部分

------解决思路----------------------
抠图:

/// <summary>
        /// 获取经容差计算后的最终图,参考色彩点为源图坐标(1,1)
        /// </summary>
        /// <param name="SourceBitmap">源图</param>
        /// <param name="Tolerance">容差</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private Bitmap GetFinalBitmap_Forli_007(Bitmap SourceBitmap, int Tolerance)
        {
            try
            {
                //释放旧临时图内存
                Color SourceColor = SourceBitmap.GetPixel(1, 1);
                Bitmap _tmpB = new Bitmap(SourceBitmap);
                System.Drawing.Imaging.BitmapData bmpDATA = new System.Drawing.Imaging.BitmapData();
                bmpDATA = _tmpB.LockBits(new Rectangle(0, 0, _tmpB.Width, _tmpB.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                byte[] BTS = new byte[bmpDATA.Stride * bmpDATA.Height + 1];
                System.Runtime.InteropServices.Marshal.Copy(bmpDATA.Scan0, BTS, 0, BTS.Length - 1);
                for (int I = 0; I <= BTS.Length - 4; I += 4)
                {
                    if (IsNearValue(BTS[I], BTS[I + 1], BTS[I + 2], SourceColor, Tolerance) == true)
                    {
                        BTS[I + 3] = 0;
                    }
                }
                System.Runtime.InteropServices.Marshal.Copy(BTS, 0, bmpDATA.Scan0, BTS.Length - 1);
                _tmpB.UnlockBits(bmpDATA);
                return _tmpB;
            }
            catch
            {
                return null;
            }
        }

        private bool IsNearValue(int B, int G, int R, Color Cr, int Tol)
        {
            try
            {
                if (Math.Max(Cr.B, B) - Math.Min(Cr.B, B) <= Tol && Math.Max(Cr.G, G) - Math.Min(Cr.G, G) <= Tol && Math.Max(Cr.R, R) - Math.Min(Cr.R, R) <= Tol)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

------解决思路----------------------
#2 的代码已经足够了,你总不能让人家替你写个项目吧?
就算是替你写了,那也只是那一个方法对你有用

IsNearValue 方法是检查一个像素是否为你要的

容差 就是允许的误差
图中的每一个点不可能都完全一样,那么决定一个点的取舍时,就要看他是贴近需要的颜色多点,还是离开的多点
容差在这里就是判断的依据

比如,图中背景是黑的,那么深灰色的部分算是背景还是肝脏,这就要靠你设定容差来做了
  相关解决方案