Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 898469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:02:53+00:00 2026-05-15T15:02:53+00:00

Given the image below, what algorithm might I use to detect whether regions one

  • 0

Given the image below, what algorithm might I use to detect whether regions one and two (identified by color) have a border?

http://img823.imageshack.us/img823/4477/borders.png

If there’s a C# example out there, that would be awesome, but I’m really just looking for any example code.

Edit: Using Jaro’s advice, I came up with the following…

public class Shape
{
    private const int MAX_BORDER_DISTANCE = 15;

    public List<Point> Pixels { get; set; }

    public Shape()
    {
        Pixels = new List<Point>();
    }

    public bool SharesBorder(Shape other)
    {
        var shape1 = this;
        var shape2 = other;

        foreach (var pixel1 in shape1.Pixels)
        {
            foreach (var pixel2 in shape2.Pixels)
            {
                var xDistance = Math.Abs(pixel1.X - pixel2.X);
                var yDistance = Math.Abs(pixel1.Y - pixel2.Y);

                if (xDistance > 1 && yDistance > 1)
                {
                    if (xDistance * yDistance < MAX_BORDER_DISTANCE)
                        return true;
                }
                else
                {
                    if (xDistance < Math.Sqrt(MAX_BORDER_DISTANCE) &&
                        yDistance < Math.Sqrt(MAX_BORDER_DISTANCE))
                        return true;
                }
            }
        }

        return false;
    }

    // ...
}

Clicking on two shapes that do share a border returns fairly quickly, but very distance shapes or shapes with a large number of pixels take 3+ seconds at times. What options do I have for optimizing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:02:54+00:00Added an answer on May 15, 2026 at 3:02 pm

    2 regions having border means that within a certain small area there should be 3 colors present: red, black and green.

    So a very ineffective solution presents itself:
    using Color pixelColor = myBitmap.GetPixel(x, y); you could scan an area for those 3 colors. The area must be larger than the width of the border of course.

    There is of course plenty room for optimizations (like going in 50 pixels steps and decreasing the precision continually).
    Since black is the least used color, you would search around black areas first.

    This should explain what I have written in various comments in this topic:

    namespace Phobos.Graphics
    {
        public class BorderDetector
        {
            private Color region1Color = Color.FromArgb(222, 22, 46);
            private Color region2Color = Color.FromArgb(11, 189, 63);
            private Color borderColor = Color.FromArgb(11, 189, 63);
    
            private List<Point> region1Points = new List<Point>();
            private List<Point> region2Points = new List<Point>();
            private List<Point> borderPoints = new List<Point>();
    
            private Bitmap b;
    
            private const int precision = 10;
            private const int distanceTreshold = 25;
    
            public long Miliseconds1 { get; set; }
            public long Miliseconds2 { get; set; }
    
            public BorderDetector(Bitmap b)
            {
                if (b == null) throw new ArgumentNullException("b");
    
                this.b = b;
            }
    
            private void ScanBitmap()
            {
                Color c;
    
                for (int x = precision; x < this.b.Width; x += BorderDetector.precision)
                {
                    for (int y = precision; y < this.b.Height; y += BorderDetector.precision)
                    {
                        c = this.b.GetPixel(x, y);
    
                        if (c == region1Color) region1Points.Add(new Point(x, y));
                        else if (c == region2Color) region2Points.Add(new Point(x, y));
                        else if (c == borderColor) borderPoints.Add(new Point(x, y));
                    }
                }
            }
    
            /// <summary>Returns a distance of two points (inaccurate but very fast).</summary>
            private int GetDistance(Point p1, Point p2)
            {
                return Math.Abs(p1.X - p2.X) + Math.Abs(p1.Y - p2.Y);
            }
    
            /// <summary>Finds the closests 2 points among the points in the 2 sets.</summary>
            private int FindClosestPoints(List<Point> r1Points, List<Point> r2Points, out Point foundR1, out Point foundR2)
            {
                int minDistance = Int32.MaxValue;
                int distance = 0;
    
                foundR1 = Point.Empty;
                foundR2 = Point.Empty;
    
                foreach (Point r1 in r1Points)
                    foreach (Point r2 in r2Points)
                    {
                        distance = this.GetDistance(r1, r2);
    
                        if (distance < minDistance)
                        {
                            foundR1 = r1;
                            foundR2 = r2;
                            minDistance = distance;
                        }
                    }
    
                return minDistance;
            }
    
            public bool FindBorder()
            {
                Point r1;
                Point r2;
    
                Stopwatch watch = new Stopwatch();
    
                watch.Start();
                this.ScanBitmap();
                watch.Stop();
                this.Miliseconds1 = watch.ElapsedMilliseconds;
    
                watch.Start();
                int distance = this.FindClosestPoints(this.region1Points, this.region2Points, out r1, out r2);
                watch.Stop();
                this.Miliseconds2 = watch.ElapsedMilliseconds;
    
                this.b.SetPixel(r1.X, r1.Y, Color.Green);
                this.b.SetPixel(r2.X, r2.Y, Color.Red);
    
                return (distance <= BorderDetector.distanceTreshold);
            }
        }
    }
    

    It is very simple. Searching this way only takes about 2 + 4 ms (scanning and finding the closest points).

    You could also do the search recursively: first with precision = 1000, then precision = 100 and finally precision = 10 for large images.
    FindClosestPoints will practically give you an estimated rectangual area where the border should be situated (usually borders are like that).

    Then you could use the vector approach I have described in other comments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the two images below. They are the same image, with one having
I have one static page displaying result of an event as given below. <StackPanel
I need to upload a given image using Amazon S3 I have this PHP:
I have grid of images, when the mouse is over any given image a
I have given bg image for radio button. It is working in chrome but
I have a symmetric matrix like shown in the image attached below. I've made
I have been successfully working with the Haar algorithm in OpenCV-2.1.0 (cvHaarDetectObjects) to detect
Please see the image below. Assume that these are all divs with the given
I have the XML given below. From this XML, in an ASPX page with
In the image below you see in (1) a triangle and a circle. Given

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.