Question

Use Hashing to optimize your algorithm.(use java) Write an algorithm to find and complete the lab...

Use Hashing to optimize your algorithm.(use java)

Write an algorithm to find and complete the labeling of connected components in an image.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Image

|

scan pixel By pixel

|

pixel is not background

|

check neighbour

|

neighbour already labelled neighbour already labelled

|

assign to parent labelled assign new labelled

second to find aggregation

Image

|

scan pixel by pixel

|

get parent labelled

|

add to existing list add to new list

public interface IConnectedComponentLabeling
{
    IDictionary<int, Bitmap> Process(Bitmap input);
}

IConnectedComponentLabeling target = new CCL();

Bitmap input = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + @"\Test.bmp");

var images= target.Process(input);

foreach (var image in images) {

image.Value.Save(savePath + image.Key + ".bmp");

}

private Dictionary<int, List<Pixel>> AggregatePatterns(Dictionary<int, 
          Label> allLabels, int width, int height)
{
    var patterns = new Dictionary<int, List<Pixel>>();

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int patternNumber = _board[j, i];

            if (patternNumber != 0)
            {
                patternNumber = allLabels[patternNumber].GetRoot().Name;

                if (!patterns.ContainsKey(patternNumber))
                {
                    patterns.Add(patternNumber, new List<Pixel>());
                }

                patterns[patternNumber].Add(new Pixel(new Point(j, i), Color.Black));
            }
        }
    }

    return patterns;
}
using System;
using System.Collections.Generic;
using System.Text;

namespace ConnectedComponentLabeling
{
    internal class Label
    {

        public int Name { get; set; }

        public Label Root { get; set; }

        public int Rank { get; set; }
        public Label(int Name)
        {
            this.Name = Name;
            this.Root = this;
            this.Rank = 0;
        }
        internal Label GetRoot()
        {
            if (this.Root != this)
            {
                this.Root = this.Root.GetRoot();//Compact tree
            }

            return this.Root;
        }

        internal void Join(Label root2)
        {
            if (root2.Rank < this.Rank)//is the rank of Root2 less than that of Root1 ?
            {
                root2.Root = this;//yes! then Root1 is the parent of Root2 (since it has the higher rank)
            }
            else //rank of Root2 is greater than or equal to that of Root1
            {
                this.Root = root2;//make Root2 the parent

                if (this.Rank == root2.Rank)//both ranks are equal ?
                {
                    root2.Rank++;//increment Root2, we need to reach a single root for the whole tree
                }
            }
        }

}
}
Add a comment
Know the answer?
Add Answer to:
Use Hashing to optimize your algorithm.(use java) Write an algorithm to find and complete the lab...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT