使用C#进行区域增长图像分割 [英] Region Growing Image Segmentation with C#

查看:89
本文介绍了使用C#进行区域增长图像分割的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这个代码有什么问题,我希望你能解释一下或者让它变得更好......因为我不知道这个代码还有什么...请帮助..



i dont know what is wrong with this code, i hope you can explain it to me or make it better.. because I dont know What else to do with this code.. please help..

public Bitmap Bitmap{
              get{
                 return bitmap;
              }
            set{
            Bitmap bitmap = new Bitmap (CitraAsli.Image);
            mask = new bool[bitmap.Height, bitmap.Width];
            maskBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb);
            for (int i = 0; i < bitmap.Height; i++)
            for (int j = 0; j < bitmap.Width; j++)
            mask[i, j] = false;
            BitmapData data = bitmap.LockBits(new Rectangle(0, 0,bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            pixels = new Color[bitmap.Height, bitmap.Width];
            for (int i = 0; i < bitmap.Height; i++)
            {
                unsafe
                {
                    int* pix = (int*)((int)data.Scan0 + data.Stride * i);
                    for (int j = 0; j < bitmap.Width; j++)
                    {
                        pixels[i, j] = Color.FromArgb(pix[j]);
                    }
                }
            }
            bitmap.UnlockBits(data);
            
            }
        }

        int GetColorDistance(Color c1, Color c2){
     
            return Math.Max(Math.Max(Math.Abs(c1.R - c2.R), Math.Abs(c1.G - c2.G)), Math.Abs(c1.B - c2.B));
 
        }

            private delegate void IncludeToRegion(int x, int y);


            void PushMask()
            {
                maskStack.Add(mask);
                bool[,] orimask = mask;
                mask = new bool[bitmap.Height, bitmap.Width];
                for (int i = 0; i < bitmap.Height; i++)
                {
                    for (int j = 0; j < bitmap.Width; j++)
                    {
                        mask[i, j] = orimask[i, j];
                    }
                }
                if (maskStack.Count > MaxUndoCount)
                {
                    maskStack.RemoveAt(0);
                }
            }

        private void UpdateMaskBitmap(){
            BitmapData data = maskBitmap.LockBits(new Rectangle(0, 0, maskBitmap.Width, maskBitmap.Height),
            ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
            for (int i = 0; i < maskBitmap.Height; i++){
                unsafe
                {
                    int* pix = (int*)((int)data.Scan0 + data.Stride * i);
                    for (int j = 0; j < maskBitmap.Width; j++){
                        pix[j] = Color.FromArgb(mask[i, j] ? 128 : 0,Color.Blue).ToArgb();
                    }
                }
            }
                maskBitmap.UnlockBits(data);
            }




            void GrowRegion(){
                PushMask();
                int y = startPoint.Y;
                int x = startPoint.X;
                int threshold = 10;
                mask[y, x] = true;
                bool[,] scanned = new bool[bitmap.Height, bitmap.Width];
                for (int i = 0; i < bitmap.Height; i++)
                for (int j = 0; j < bitmap.Width; j++)
                scanned[i, j] = false;
                scanned[y, x] = true;

                Queue<Point> queue = new Queue<Point>();
                Point curPoint = startPoint;
                //double stdev = 0.0;
                //double sn = 0.0;
                double meanR = pixels[y,x].R;
                double meanG = pixels[y,x].G;
                double meanB = pixels[y,x].B;
                int n = 1;

                IncludeToRegion include = delegate(int X, int Y)
                {
                    double diff = Math.Max(Math.Abs(pixels[Y, X].R - meanR) / meanR, Math.Max(Math.Abs(pixels[Y, X].B - meanB) / meanB, Math.Abs(pixels[Y, X].G - meanG) / meanG));

                    if (diff < threshold)
                    {
                        mask[Y, X] = true;
                        queue.Enqueue(new Point(X, Y));
                        meanR = (meanR * n + pixels[Y, X].R) / (n + 1);
                        meanG = (meanG * n + pixels[Y, X].G) / (n + 1);
                        meanB = (meanB * n + pixels[Y, X].B) / (n + 1);
                        //sn = sn + (val ‐ mean)*(val‐newMean);
                        //stdev = Math.Sqrt(sn/(n));
                        n++;
                    }

                    while (true)
                    {
                        Point p1, p2, p3, p4;
                        p1 = new Point(curPoint.X - 1, curPoint.Y);
                        p2 = new Point(curPoint.X + 1, curPoint.Y);
                        p3 = new Point(curPoint.X, curPoint.Y - 1);
                        p4 = new Point(curPoint.X, curPoint.Y + 1);
                        if (p1.X >= 0 && !scanned[p1.Y, p1.X])
                        {
                            scanned[p1.Y, p1.X] = true;
                            include(p1.X, p1.Y);
                        }
                        if (p2.X < bitmap.Width && !scanned[p2.Y, p2.X])
                        {
                            scanned[p2.Y, p2.X] = true;
                            include(p2.X, p2.Y);
                        }
                        if (p3.Y >= 0 && !scanned[p3.Y, p3.X])
                        {
                            scanned[p3.Y, p3.X] = true;
                            include(p3.X, p3.Y);
                        }
                        if (p4.Y < bitmap.Height && !scanned[p4.Y, p4.X])
                        {
                            scanned[p4.Y, p4.X] = true;
                            include(p4.X, p4.Y);
                        }
                        if (queue.Count() != 0)
                            curPoint = queue.Dequeue();

                        else
                            break;

                        UpdateMaskBitmap();
                       
                    
                    }
                };
            }

推荐答案

您能否在此写下您需要达到的目标?



查看代码,我发现从不使用Bitmap属性setter。该初始化代码应该放在getter中。



Could you please write here what you need to achieve?

Looking at your code, I see that Bitmap property setter is never used. The initalization code is supposed to be placed in the getter instead.

public Bitmap Bitmap
        {
            get
            {
                Bitmap bitmap = new Bitmap(CitraAsli.Image);
                mask = new bool[bitmap.Height, bitmap.Width];
                maskBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb);
                for (int i = 0; i < bitmap.Height; i++)
                    for (int j = 0; j < bitmap.Width; j++)
                        mask[i, j] = false;
                BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                pixels = new Color[bitmap.Height, bitmap.Width];
                for (int i = 0; i < bitmap.Height; i++)
                {
                    unsafe
                    {
                        int* pix = (int*)((int)data.Scan0 + data.Stride * i);
                        for (int j = 0; j < bitmap.Width; j++)
                        {
                            pixels[i, j] = Color.FromArgb(pix[j]);
                        }
                    }
                }
                bitmap.UnlockBits(data);
                return bitmap;
            }
        }





GrowRegion()功能没完成。你想在这个函数中做什么?





GrowRegion() function is not completed. What you want to do in this function?

void GrowRegion()
        {
            PushMask();
            int y = startPoint.Y;
            int x = startPoint.X;
            int threshold = 10;
            mask[y, x] = true;
            bool[,] scanned = new bool[bitmap.Height, bitmap.Width];
            for (int i = 0; i < bitmap.Height; i++)
                for (int j = 0; j < bitmap.Width; j++)
                    scanned[i, j] = false;
            scanned[y, x] = true;

            Queue<point> queue = new Queue<point>();
            Point curPoint = startPoint;
            //double stdev = 0.0;
            //double sn = 0.0;
            double meanR = pixels[y, x].R;
            double meanG = pixels[y, x].G;
            double meanB = pixels[y, x].B;
            int n = 1;

            IncludeToRegion include = null;

            // .... what's next? what you want to achieve? where is the code?
        }</point></point>





提供有关您的目标的更多详细信息,我可能会帮助您。



在缩放图像时,您必须记住两件重要的事情:



宽高比



Antialiasing



以下讨论应该帮助您了解图像调整大小的技巧:



http://stackoverflow.com/questions/10442269/scaling-a-system-drawing-bitmap-to-a-given-size-while-保持纵横比 [ ^ ]



http://stackoverflow.com/questions/333589/image-resizing-in-net-with-antialiasing [ ^ ]



希望这有帮助。



provide more details about your goal and I might help you.

There are two important things, that you have to keep in mind when scaling images:

Aspect ratio

Antialiasing

Following discussions should help you to understand images resizing techniques:

http://stackoverflow.com/questions/10442269/scaling-a-system-drawing-bitmap-to-a-given-size-while-maintaining-aspect-ratio[^]

http://stackoverflow.com/questions/333589/image-resizing-in-net-with-antialiasing[^]

Hope this helps.


这篇关于使用C#进行区域增长图像分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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