I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 218, comments - 2281, trackbacks - 33

My Links

Advertisement

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer.

I also speak at local user group meetings and conferences like SQLBits and NT Conference
Welcome to my blog.
SQL Server MVP

My Books

SQL Server MVP Deep Dives 2
The Red Gate Guide to SQL Server Team based Development Free e-book

My Blog Feed via Email
Follow MladenPrajdic on Twitter


Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

Watershed Image Segmentation in C#

Lately I've been doing some image processing work using the most amazing AForge.NET open source library written in C#.

This library contains a whole bunch of useful filters you can play with but unfortunately it doesn't contain any filter for segmentation.

This is my implementation of Grayscale Immersion Watershed Segmentation in C# based on the

Vincent-Soille watershed algorithm which can be found in The Watershed Transform: Defnitions, Algorithms and Parallelization Strategies

paper by Jos B.T.M. Roerdink and Arnold Meijster on page 15. For watershed to work properly the areas you're trying to segment

have to be darker than the background. Blurring is also an important part of the image pre-processing since the algorithm

over segments non-blurred images. An example picture of watershed segmentation:

testPic testPic_Watershed
Original picture Segmented picture

 

using System;
using System.Collections.Generic;
using System.Text;

namespace Segmentation
{
    public class WatershedPixel
    {
        public int X;
        public int Y;
        public int Height;
        // labels the pixel as belonging to a unique basin or as a part of the watershed line
        public int Label;
        // Distance is a work image of distances
        public int Distance;

        public WatershedPixel()
        {
            this.X = -1;
            this.Y = -1;
            this.Height = -1000;
            this.Label = -1000;
            this.Distance = -1000;
        }

        public WatershedPixel(int x, int y)
        {
            this.X = x;
            this.Y = y;
            this.Height = -1000;
            this.Label = WatershedCommon.INIT;
            this.Distance = 0;
        }

        public WatershedPixel(int x, int y, int height)
        {
            this.X = x;
            this.Y = y;
            this.Height = height;
            this.Label = WatershedCommon.INIT;
            this.Distance = 0;
        }

        public override bool Equals(Object obj)
        {
            WatershedPixel p = (WatershedPixel)obj;
            return (X == p.X && X == p.Y);
        }

        public override int GetHashCode()
        {
            return X;
        }
        public override string ToString()
        {
            return "Height = " + Height + "; X = " + X.ToString() + ", Y = " + Y.ToString() + 
                   "; Label = " + Label.ToString() + "; Distance = " + Distance.ToString();
        }
    }
    
    public class WatershedGrayscale : FilterGrayToGray
    {
        #region Variables
        private WatershedPixel FictitiousPixel = new WatershedPixel();
        private int _currentLabel = 0;
        private int _currentDistance = 0;
        private FifoQueue _fifoQueue = new FifoQueue();
        // each pixel can be accesesd from 2 places: a dictionary for faster direct lookup of neighbouring pixels 
        // or from a height ordered list
        // sorted array of pixels according to height
        private List<List<WatershedPixel>> _heightSortedList;
        // string in the form "X,Y" is used as a key for the dictionary lookup of a pixel
        private Dictionary<string, WatershedPixel> _pixelMap;
        private int _watershedPixelCount = 0;
        private int _numberOfNeighbours = 8;
        private bool _borderInWhite;
        private int _pictureWidth = 0;
        private int _pictureHeight = 0;
        #endregion

        #region Constructors
        public WatershedGrayscale()
            : this(8)
        {}

        public WatershedGrayscale(int numberOfNeighbours)
        {
            if (numberOfNeighbours != 8 && numberOfNeighbours != 4)
                throw new Exception("Invalid number of neighbour pixels to check. Valid values are 4 and 8.");
            _borderInWhite = true;
            _numberOfNeighbours = numberOfNeighbours;
            _heightSortedList = new List<List<WatershedPixel>>(256);
            for (int i = 0; i < 256; i++)
                _heightSortedList.Add(new List<WatershedPixel>());
        }
        #endregion

        #region Properties
        /// <summary>
        /// number of neighbours to check for each pixel. valid values are 8 and 4
        /// </summary>
        public int NumberOfNeighbours
        {
            get { return _numberOfNeighbours; }
            set
            {
                if (value != 8 && value != 4)
                    throw new Exception("Invalid number of neighbour pixels to check. Valid values are 4 and 8.");
                _numberOfNeighbours = value;
            }
        }

        /// <summary>
        /// Number of labels/basins found
        /// </summary>
        public int LabelCount
        {
            get { return _currentLabel; }
            set { _currentLabel = value; }
        }

        /// <summary>
        /// True: border is drawn in white. False: border is drawn in black
        /// </summary>
        /// <value></value>
        public bool BorderInWhite
        {
            get { return _borderInWhite; }
            set { _borderInWhite = value; }
        }
        #endregion

        private void CreatePixelMapAndHeightSortedArray(BitmapData data)
        {
            _pictureWidth = data.Width;
            _pictureHeight = data.Height;
            // pixel map holds every pixel thus size of (_pictureWidth * _pictureHeight)
            _pixelMap = new Dictionary<string, WatershedPixel>(_pictureWidth * _pictureHeight);
            unsafe
            {                
                int offset = data.Stride - data.Width;
                byte* ptr = (byte*)(data.Scan0);

                // get histogram of all values in grey = height
                for (int y = 0; y < data.Height; y++)
                {
                    for (int x = 0; x < data.Width; x++, ptr++)
                    {
                        WatershedPixel p = new WatershedPixel(x, y, *ptr);
                        // add every pixel to the pixel map
                        _pixelMap.Add(p.X.ToString() + "," + p.Y.ToString(), p);
                        _heightSortedList[*ptr].Add(p);
                    }
                    ptr += offset;
                }
            }
            this._currentLabel = 0;
        }

        private void Segment()
        {            
            // Geodesic SKIZ (skeleton by influence zones) of each level height
            for (int h = 0; h < _heightSortedList.Count; h++)
            {
                // get all pixels for current height
                foreach (WatershedPixel heightSortedPixel in _heightSortedList[h])
                {
                    heightSortedPixel.Label = WatershedCommon.MASK;                    
                    // for each pixel on current height get neighbouring pixels
                    List<WatershedPixel> neighbouringPixels = GetNeighbouringPixels(heightSortedPixel);
                    // initialize queue with neighbours at level h of current basins or watersheds
                    foreach (WatershedPixel neighbouringPixel in neighbouringPixels)
                    {
                        if (neighbouringPixel.Label > 0 || neighbouringPixel.Label == WatershedCommon.WSHED)
                        {
                            heightSortedPixel.Distance = 1;
                            _fifoQueue.AddToEnd(heightSortedPixel);
                            break;
                        }                        
                    }
                }
                _currentDistance = 1;
                _fifoQueue.AddToEnd(FictitiousPixel);
                // extend basins
                while (true)
                {
                    WatershedPixel p = _fifoQueue.RemoveAtFront();
                    if (p.Equals(FictitiousPixel))
                    {
                        if (_fifoQueue.IsEmpty)
                            break;
                        else
                        {
                            _fifoQueue.AddToEnd(FictitiousPixel);
                            _currentDistance++;
                            p = _fifoQueue.RemoveAtFront();
                        }
                    }
                    List<WatershedPixel> neighbouringPixels = GetNeighbouringPixels(p);
                    // labelling p by inspecting neighbours
                    foreach (WatershedPixel neighbouringPixel in neighbouringPixels)
                    {
                        // neighbouringPixel belongs to an existing basin or to watersheds
                        // in the original algorithm the condition is:
                        //   if (neighbouringPixel.Distance < currentDistance && 
                        //      (neighbouringPixel.Label > 0 || neighbouringPixel.Label == WatershedCommon.WSHED))
                        //   but this returns incomplete borders so the this one is used                        
                        if (neighbouringPixel.Distance <= _currentDistance && 
                           (neighbouringPixel.Label > 0 || neighbouringPixel.Label == WatershedCommon.WSHED))
                        {
                            if (neighbouringPixel.Label > 0)
                            {
                                // the commented condition is also in the original algorithm 
                                // but it also gives incomplete borders
                                if (p.Label == WatershedCommon.MASK /*|| p.Label == WatershedCommon.WSHED*/) 
                                    p.Label = neighbouringPixel.Label;
                                else if (p.Label != neighbouringPixel.Label)
                                {
                                    p.Label = WatershedCommon.WSHED;
                                    _watershedPixelCount++;
                                }
                            }
                            else if (p.Label == WatershedCommon.MASK)
                            {
                                p.Label = WatershedCommon.WSHED;
                                _watershedPixelCount++;
                            }
                        }
                        // neighbouringPixel is plateau pixel
                        else if (neighbouringPixel.Label == WatershedCommon.MASK && neighbouringPixel.Distance == 0)
                        {
                            neighbouringPixel.Distance = _currentDistance + 1;
                            _fifoQueue.AddToEnd(neighbouringPixel);
                        }
                    }
                }
                // detect and process new minima at height level h
                foreach (WatershedPixel p in _heightSortedList[h])
                {
                    // reset distance to zero
                    p.Distance = 0;
                    // if true then p is inside a new minimum 
                    if (p.Label == WatershedCommon.MASK)
                    {
                        // create new label
                        _currentLabel++;
                        p.Label = _currentLabel;
                        _fifoQueue.AddToEnd(p);
                        while (!_fifoQueue.IsEmpty)
                        {
                            WatershedPixel q = _fifoQueue.RemoveAtFront();
                            // check neighbours of q
                            List<WatershedPixel> neighbouringPixels = GetNeighbouringPixels(q);
                            foreach (WatershedPixel neighbouringPixel in neighbouringPixels)
                            {
                                if (neighbouringPixel.Label == WatershedCommon.MASK)
                                {
                                    neighbouringPixel.Label = _currentLabel;
                                    _fifoQueue.AddToEnd(neighbouringPixel);
                                }
                            }
                        }
                    }
                }
            }
        }

        private List<WatershedPixel> GetNeighbouringPixels(WatershedPixel centerPixel)
        {
            List<WatershedPixel> temp = new List<WatershedPixel>();
            if (_numberOfNeighbours == 8)
            {
                /*
                CP = Center pixel
                (X,Y) -- get all 8 connected 
                |-1,-1|0,-1|1,-1|
                |-1, 0| CP |1, 0|
                |-1,+1|0,+1|1,+1|
                */                
                // -1, -1                
                if ((centerPixel.X - 1) >= 0 && (centerPixel.Y - 1) >= 0)
                    temp.Add(_pixelMap[(centerPixel.X - 1).ToString() + "," + (centerPixel.Y - 1).ToString()]);
                //  0, -1
                if ((centerPixel.Y - 1) >= 0)
                    temp.Add(_pixelMap[centerPixel.X.ToString() + "," + (centerPixel.Y - 1).ToString()]);
                //  1, -1
                if ((centerPixel.X + 1) < _pictureWidth && (centerPixel.Y - 1) >= 0)
                    temp.Add(_pixelMap[(centerPixel.X + 1).ToString() + "," + (centerPixel.Y - 1).ToString()]);
                // -1, 0
                if ((centerPixel.X - 1) >= 0)
                    temp.Add(_pixelMap[(centerPixel.X - 1).ToString() + "," + centerPixel.Y.ToString()]);
                //  1, 0
                if ((centerPixel.X + 1) < _pictureWidth)
                    temp.Add(_pixelMap[(centerPixel.X + 1).ToString() + "," + centerPixel.Y.ToString()]);
                // -1, 1
                if ((centerPixel.X - 1) >= 0 && (centerPixel.Y + 1) < _pictureHeight)
                    temp.Add(_pixelMap[(centerPixel.X - 1).ToString() + "," + (centerPixel.Y + 1).ToString()]);
                //  0, 1
                if ((centerPixel.Y + 1) < _pictureHeight)
                    temp.Add(_pixelMap[centerPixel.X.ToString() + "," + (centerPixel.Y + 1).ToString()]);
                //  1, 1
                if ((centerPixel.X + 1) < _pictureWidth && (centerPixel.Y + 1) < _pictureHeight)
                    temp.Add(_pixelMap[(centerPixel.X + 1).ToString() + "," + (centerPixel.Y + 1).ToString()]);
            }
            else
            {
                /*
                CP = Center pixel, N/A = not used
                (X,Y) -- get only 4 connected 
                | N/A |0,-1| N/A |
                |-1, 0| CP |+1, 0|
                | N/A |0,+1| N/A |
                */
                //  -1, 0
                if ((centerPixel.X - 1) >= 0)
                    temp.Add(_pixelMap[(centerPixel.X - 1).ToString() + "," + centerPixel.Y.ToString()]);
                //  0, -1
                if ((centerPixel.Y - 1) >= 0)
                    temp.Add(_pixelMap[centerPixel.X.ToString() + "," + (centerPixel.Y - 1).ToString()]);
                //  1, 0
                if ((centerPixel.X + 1) < _pictureWidth)
                    temp.Add(_pixelMap[(centerPixel.X + 1).ToString() + "," + centerPixel.Y.ToString()]);
                //  0, 1
                if ((centerPixel.Y + 1) < _pictureHeight)
                    temp.Add(_pixelMap[centerPixel.X.ToString() + "," + (centerPixel.Y + 1).ToString()]);
            }
            return temp;
        }

        private void DrawWatershedLines(BitmapData data)
        {
            if (_watershedPixelCount == 0)
                return;

            byte watershedColor = 255;
            if (!_borderInWhite)
                watershedColor = 0;
            unsafe
            {
                int offset = data.Stride - data.Width;
                byte* ptr = (byte*)(data.Scan0);

                for (int y = 0; y < data.Height; y++)
                {
                    for (int x = 0; x < data.Width; x++, ptr++)
                    {
                        // if the pixel in our map is watershed pixel then draw it
                        if (_pixelMap[x.ToString() + "," + y.ToString()].Label == WatershedCommon.WSHED)
                            *ptr = watershedColor;
                    }
                    ptr += offset;
                }
            }
        }

        protected override void ProcessFilter(BitmapData imageData)
        {
            CreatePixelMapAndHeightSortedArray(imageData);
            Segment();
            DrawWatershedLines(imageData);
        }
    }
    
    public class FifoQueue
    {
        List<WatershedPixel> queue = new List<WatershedPixel>();

        public int Count
        {
            get { return queue.Count; }
        }

        public void AddToEnd(WatershedPixel p)
        {
            queue.Add(p);
        }

        public WatershedPixel RemoveAtFront()
        {
            WatershedPixel temp = queue[0];
            queue.RemoveAt(0);
            return temp;
        }

        public bool IsEmpty
        {
            get { return (queue.Count == 0); }
        }

        public override string ToString()
        {
            return base.ToString() + " Count = " + queue.Count.ToString();
        }
    }
    
    public class WatershedCommon
    {
        #region Constants
        public const int INIT = -1;
        public const int MASK = -2;
        public const int WSHED = 0;
        #endregion
    }
}

 

This filter will also become the part of the AForge.NET library in the future so to use this algorithm you have to have it installed.

The performance can be improved as I've gone for the readability factor since performance wasn't very important on my project.

 

kick it on DotNetKicks.com
 

Print | posted on Monday, February 11, 2008 6:46 PM | Filed Under [ .Net ]

Feedback

Gravatar

# re: Watershed Image Segmentation in C#

Please send me full codeproject. I need it. Thanks you very much
8/23/2008 4:38 PM | To Quyen
Gravatar

# re: Watershed Image Segmentation in C#

can you explain how to run your coding...i got problem to run this code...tq
10/14/2008 6:19 AM | zol
Gravatar

# re: Watershed Image Segmentation in C#

you need to install the aforge.net library and that's about it.
10/14/2008 10:46 AM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

Hi, Im new to the area of image processing especially MRI image.I have some image
and want to classify them in different categories. Please guide me how i can achieve this. Hope you will guide me..
10/27/2008 2:03 PM | zol
Gravatar

# re: Watershed Image Segmentation in C#

hehe that's a huge area and i'm not really working with it anymore. i guess there are better resources out there for it.
10/27/2008 2:05 PM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

Can you share this project?

Thanks.
11/18/2008 2:09 PM | ruaxauxi
Gravatar

# re: Watershed Image Segmentation in C#

which project?
11/18/2008 2:38 PM | Mlade
Gravatar

# re: Watershed Image Segmentation in C#

I have some problems when complie this! Can you share this project ???Thanks a lot!
11/30/2008 6:09 AM | Nhan
Gravatar

# re: Watershed Image Segmentation in BORLAND C++

can u send me a source code for watershed segmented include the preprocessing process such as histogram equalization, median and gaussian filter and then followed by watershed process.
11/30/2008 9:49 AM | NOR HIDAYAH
Gravatar

# re: Watershed Image Segmentation in C#

@Nhan: what kind of problems?

@NOR HIDAYAH: i'm sure you can find all this on the net.

11/30/2008 2:39 PM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

I got it to work. You need to use Aforce version 1.6.2 since the newer versions are not backwards compatible. The source does not have a main method, so you will have to make your own, and run ProcessFilter(BitmapData imageData) in that method. You have to feed it a BitmapData file, which you can make out of a bitmap file like this:

Bitmap bmp = new Bitmap("image.jpg");
WatershedGrayscale b = new WatershedGrayscale(8);

// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);

after the image has been segmented, you have to unlock the bits of the bitmapdata varable using:

bmp.UnlockBits(bmpData);

then you can display the bmp file.

Also make sure that the image you feed it is in single channel, 8-bit grayscale depth. Also, to get the results as advertised above, you need to sufficiently blur the image. Notice that the segmented result is far blurrier than the original.

Good luck.
12/3/2008 4:29 AM | don omar
Gravatar

# re: Watershed Image Segmentation in C#

require a full code for segmentation oef an image
12/19/2008 12:07 PM | wasif
Gravatar

# re: Watershed Image Segmentation in C#

good for you.
12/19/2008 12:24 PM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

can u send ur code to me?? and guide me how to run this code
12/25/2008 11:16 AM | pavan
Gravatar

# re: Watershed Image Segmentation in C#

just follow the instructions in the article.
12/29/2008 11:22 AM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

did we've to install Aforge.NET in the same folder of .NET or we've to specify any path in the program because i'm getting four errors while compiling the program
1/6/2009 11:37 PM | pavan
Gravatar

# re: Watershed Image Segmentation in C#

no you don't. you have to install the aforge and then include it's imaging dll into your piojects
1/7/2009 10:19 AM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

i solve all the compile time error. Now, its giving run time error on Bitmap bmp = new Bitmap("image.jpg"); line. I've included main function in the same code and written the same code given above. Can u help me.
1/14/2009 11:25 PM | pavan
Gravatar

# re: Watershed Image Segmentation in C#

well you have to specify the full path to your image...
1/15/2009 10:44 AM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

AForge.Net is a great free open source imaging library for .Net. However, you can still improve performance using multithreading.
If you are interested in learning how to exploit multicore processors to speed up image processing using AForge.Net, you will find a great example using AForge.Net and multithreading to improve the performance in working with images and with multicore CPUs, in the book " C# 2008 and 2005 Threaded Programming: Beginners Guide", by Gaston Hillar, Packt Publishing - www.packtpub.com
It includes many exercises related to image management with multicore support. Highly recommended if you want to improve performance and UI responsiveness.
You can download the code from Packt's website. http://www.packtpub.com/beginners-guide-for-C-sharp-2008-and-2005-threaded-programming/book

There is also an article in Packt's website: http://www.packtpub.com/article/simplifying-parallelism-complexity-c-sharp

I bought the book last week and it helped me a lot in my image processing needs. Now, I can split an image in many parts using the code from the book and I can make it exploit my Core 2 Quad. My boss is impressed!
2/5/2009 2:56 AM | Diego Salinas
Gravatar

# re: Watershed Image Segmentation in C#

hi i ve gone through ur progrm.......... actually m working in matrox software can u help me doing some codding in this platform for watersheding.......
2/5/2009 7:42 AM | jyotirmoy pathak
Gravatar

# re: Watershed Image Segmentation in C#

@rmoy pathak: I have no clue what matrox software is or how to code anything in it,

@Diego Salinas: thanx for the links.
2/5/2009 11:04 AM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

ok..... thanks 4 ur reply.....
2/6/2009 5:35 AM | jyotirmoy
Gravatar

# re: Watershed Image Segmentation in C#

HI thanks for the code ..could u tell me how to invoke the processfilter function and display the sgmented image. I am new to C#..thanks a lot!

3/28/2009 3:45 AM | barry
Gravatar

# re: Watershed Image Segmentation in C#

you have to use the free aforge.net library
3/30/2009 3:31 PM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

Thank you for the code!!! As other programmers I am having four errors when I try to run the example.
First I installed AForge.NET 2.0, then I create a new console project in VisualStudio 2008, then copy&paste and finally I added AForge references which are in the Realese folder and I put some using AForge; using AForge.Imaging

When I try to run these errors happen:

Error 1 The type or namespace name 'FilterGrayToGray' could not be found (are you missing a using directive or an assembly reference?)

Error 2 The type or namespace name 'BitmapData' could not be found (are you missing a using directive or an assembly reference?)

Error 3 The type or namespace name 'BitmapData' could not be found (are you missing a using directive or an assembly reference?)

Error 4 The type or namespace name 'BitmapData' could not be found (are you missing a using directive or an assembly reference?)

Thank you very much,
5/21/2009 1:40 AM | armor
Gravatar

# re: Watershed Image Segmentation in C#

you also have to add the reference to the system.drawing assembly that holds BitmapData type.
same thing for Aforge's FilterGrayToGray.
5/21/2009 7:38 AM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

Thank you,

BitmapData was solved, but for FilterGrayToGray Iadded the references AForge; AForge.Imaging; AForge.Imagine.Format;

And

using AForge;
using AForge.Imaging;
using AForge.Fuzzy;
using AForge.Imaging.Filters;

But the error continue,
5/21/2009 4:32 PM | armor
Gravatar

# re: Watershed Image Segmentation in C#

I was looking for some information and when i open the search object in Visual Studioi start to search FilterGrayToGray but it does not appear it is not in the AForge.Imaging.Filters.

How can i add the FilterGrayToGray.cs because now it said to me "unsafe code" and if i put unsafe class it does not work.

Thank you,
6/3/2009 1:41 AM | aarmor
Gravatar

# re: Watershed Image Segmentation in C#

Thanks a lot!! It's very helpful to me. ~(^(oo)^)~
6/4/2009 9:59 AM | Piggy
Gravatar

# re: Watershed Image Segmentation in C#

Hi, I tried to implement your code, but I got the following error :

Error 1 'Segmentation.Form1.Dispose(bool)': no suitable method found to override C:\Users\pashok\Documents\Visual Studio 2008\Projects\watershed\watershed\Form1.Designer.cs 14 33 watershed

what am i supposed to do? Kindly help me
11/25/2009 3:22 PM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

my guess is that either your namspaces or class names are wrong somewhere.
11/25/2009 3:24 PM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

Hi, I tried to implement your code, but this is the warning I get:

Accessing a member on 'watershed1.Form1.X' may cause a runtime exception because it is a field of a marshal-by-reference class.

What does this mean?

Moreover, as suggested in a previous comment, I have created a bitmapdata object and called processfilter function etc. But how do I display the segmented image? Or is it saved anywhere? Kindly expl that
11/26/2009 10:57 AM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

maybe you should first get familiar with how the Aforge.NET works.
how Process works is all explained in Aforge documantation.
i haven't done anything differently.

as for your 'watershed1.Form1.X' error it's part of your code so i wouldn't know what it means. it looks like you have MarshalByRefObject on your class which probably isn't needed. google it up.
11/26/2009 11:20 AM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

hi, this is the code i have written. can you tell me if it is correct

Bitmap bmp = (Bitmap)pictureBox1.Image;
WatershedGrayscale wg = new WatershedGrayscale(8);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpdata = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
wg.execute(bmpdata);
bmp.UnlockBits(bmpdata);
pictureBox2.Image = (System.Drawing.Image)bmp;
pictureBox1.Visible = true;

I have added a function called execute, because I am not able to access the filtertogray function directly.

I am sorry but I am entirely new to AForge.NET
11/26/2009 11:28 AM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

ok, i am sorry i did a very stupid mistake. instead of picturebox2.visible = true, I had given picbox1. sorry.

now, the program is running fine, but even if I give ur image as test input, only half the image is getting segmented. The other half is not.

any suggestions??
11/26/2009 12:42 PM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

your image probably has the wrong PixelFormat for GrayToGray Filter.
11/26/2009 12:46 PM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

working perfectly.. thanks a lot
11/26/2009 2:06 PM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

Hi, I know that watershed algo has one problem - over segmentation. That is what I am facing. Are there any possibilities to reduce it
11/30/2009 10:47 AM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

yes you have to blur the image before you apply the segmentation.
then put the segmented lines on your original image.
11/30/2009 11:25 AM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

can you just guide me as to how the image can be blurred. sorry to ask.. but i am new to image processing
11/30/2009 12:07 PM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

are there any possibilities to convert this code to the HSL color model instead of the RGB
12/2/2009 1:19 PM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

i'm sure there is. you're welcome to do it.
12/2/2009 1:35 PM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

any idea how to do it?
12/3/2009 12:53 PM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

none. if i had any i'd tell you :)
12/3/2009 1:30 PM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

:) thanks!! if u can give me your mail id, i will send you the code I have written. if possible, can you go through it and tell me if the changes i have made are correct?
12/3/2009 2:21 PM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

This code goes thru even white pixels rite?? how can we avoid tat in order to reduce running time
12/15/2009 3:20 PM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

Just wanted to say thanks once again for this great code.. It helped me out greatly
12/18/2009 11:56 AM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

sir i am working on hand gesture recognition
can i use ur code for segmenting a human hand in a frame?
if it can be used then can u kindly dictate me how to use it?

thanking you in anticipation.
12/27/2009 8:14 PM | Innayat
Gravatar

# re: Watershed Image Segmentation in C#

i have no idea but it probably can in some way...
12/29/2009 12:02 PM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

Is there a possibility to get a documentation for this code?
1/21/2010 1:08 PM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

what documentation?
that's it what you see there.
1/21/2010 1:48 PM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

Yes I am sorry. Did not relize that.
1/21/2010 3:15 PM | Prashanth
Gravatar

# re: Watershed Image Segmentation in C#

Please send this code along with description. thnx
2/9/2010 5:29 PM | Anil Rathod
Gravatar

# re: Watershed Image Segmentation in C#

thank you for ur code
I worked with this code in new console application. but I have a following errors
Error 1 The type or namespace name 'FilterGrayToGray' could not be found (are you missing a using directive or an assembly reference?) E:\ConsoleApplication1\ConsoleApplication1\watershedPixel.cs 62 39 ConsoleApplication1
Error 2 The type or namespace name 'BitmapData' could not be found (are you missing a using directive or an assembly reference?) E:\ConsoleApplication1\ConsoleApplication1\watershedPixel.cs 134 57 ConsoleApplication1
Error 3 The type or namespace name 'BitmapData' could not be found (are you missing a using directive or an assembly reference?) E:\ConsoleApplication1\ConsoleApplication1\watershedPixel.cs 331 41 ConsoleApplication1
Error 4 The type or namespace name 'BitmapData' could not be found (are you missing a using directive or an assembly reference?) E:\ConsoleApplication1\ConsoleApplication1\watershedPixel.cs 357 47 ConsoleApplication1
Error 5 Inconsistent accessibility: parameter type 'ConsoleApplication1.watershedPixel' is less accessible than method 'ConsoleApplication1.FifoQueue.AddToEnd(ConsoleApplication1.watershedPixel)' E:\ConsoleApplication1\ConsoleApplication1\watershedPixel.cs 374 21 ConsoleApplication1
Error 6 Inconsistent accessibility: return type 'ConsoleApplication1.watershedPixel' is less accessible than method 'ConsoleApplication1.FifoQueue.RemoveAtFront()' E:\ConsoleApplication1\ConsoleApplication1\watershedPixel.cs 379 31 ConsoleApplication1


I am very new to AForge.net. Please can u help me
2/16/2010 8:11 AM | chitra
Gravatar

# re: Watershed Image Segmentation in C#

you've most like forgotten to include the AForge Dll's to your app.
2/16/2010 11:05 AM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

I created a WatershedGrayscale object and used its Apply method with a BitmapData. But there was an error: "The filter can be applied to graysclae (8bpp indexed) image only". How to solve this (I tried to use your sample picture)?
2/17/2010 11:20 AM | Rongu Sakan
Gravatar

# re: Watershed Image Segmentation in C#

there's a method in AForge that converts the image to 8bpp. But i don't remember it's name.
2/17/2010 11:22 AM | Mladen
Gravatar

# FilterGrayToGray ???

I included all of the AForge.NET dll's for the latest AForge.NET 2.1.1. But I get the FilterGrayToGray error that other comments mentioned. I opened the AForge.NET solution source code, and did a search for the string FilterGrayToGray, and nothing came up. I tried modifying your code from "public class WatershedGrayscale : FilterGrayToGray" to "public class WatershedGrayscale : Grayscale", but this doesn't work either. Maybe FilterGrayToGray is from an older version of AForge.NET? How to update this code sample?
2/21/2010 12:42 AM | Pem
Gravatar

# re: Watershed Image Segmentation in C#

there were probably some changet with the classes in new aforge.
look at their change log to see to what they replaced the class with and derive from that.
2/22/2010 11:03 AM | Mladen
Gravatar

# re: Watershed Image Segmentation in C#

Please send me full codeproject. I really need it for my final task. Thank you very much
5/14/2010 3:50 AM | renny
Gravatar

# re: Watershed Image Segmentation in C#

please send me full codeproject. i really need it for my final task. Thank you very much
7/21/2010 11:09 AM | shekhar
Gravatar

# re: Watershed Image Segmentation in C#

Hi i have problem with this project. Program segments the image only in part.I transformed code to visual applications, And i want to ask that only i have this problem, and how to solve it. I attach the picture where you can see my problem
8/20/2010 7:11 PM | cartus
Gravatar

# re: Watershed Image Segmentation in C#

http://www.image-share.com/ijpg-328-22.html sry i forgot add link.
8/20/2010 7:13 PM | cartus
Gravatar

# re: Watershed Image Segmentation in C#

i need code for uterine cervix image segmentation...can u help me????
8/24/2010 6:17 PM | reshma
Gravatar

# re: Watershed Image Segmentation in C#

hi

can i get code for compressing an using fractal image compression technique. where we use segmentation of an image.

Thank u
8/27/2010 9:48 AM | gokul
Gravatar

# re: Watershed Image Segmentation in C#

Hi

i need full code..in this coding am getting error as "Error 1 'ConsoleApplication1.WatershedGrayscale.ProcessFilter(System.Drawing.Imaging.BitmapData)': no suitable method found to override"
8/30/2010 7:37 AM | Gomathi
Gravatar

# re: Watershed Image Segmentation in C#

please sent me a demo to godjing1985@yahoo.com,thanks,i am a new in c#
10/12/2010 4:13 PM | Begger
Gravatar

# re: Watershed Image Segmentation in C#

Hello!

I tried to work with your code, but the algoortihm is not working correctly. I have the same problem as Karthus!

What is he problem??

Really thx 4 the your answer!

Adam
10/17/2010 5:20 PM | Adam
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET