Click here to Skip to main content
Click here to Skip to main content

Image Region Recoloring

By , 2 May 2013
 

Introduction

Fill with color in photo editors change specific area color with selected color. This job may be needed in other image processing tasks. The current tip shows one simple way to implement fill with color.

Background

Steps:

  1. Select and load source image
  2. Select position and get its color in image
    Pictures have specific size that is divided in width and height, that each pixel has a certain position X and Y, for easy pixel selection picture size after load time must be equal to the actual size (for scale, avoid any easy pixel selection)
  3. Select new color
  4. Fill Color

    Changing pixels color continue until color has been changed, on the other hand, region borders reach when it finds pixels with different color. The algorithm that is used in this approach is:

    1. Change Color of first selected pixel and add that position to queue
    2. While queue is not empty, do the following:
      • Dequeue one position
      • Find the neighborhood pixels in left, right, top, down direction positions of the current position
      • Check the color of that position pixel if same with the first color:
      • Change pixel color to fill color
      • Add to queue
    3. Save image

Using the Code

I implemented the Fill method:

private void btnFill_Click(object sender, EventArgs e)
{
    if (SColor == true && isload == true)
    {
        Queue q = new Queue();
        ArrayList PreList = new ArrayList();
        int x1 = int.Parse(txtX.Text);
        int y1 = int.Parse(txtY.Text);
        Point p = new Point(x1, y1);
        q.Enqueue(p);
        img.SetPixel(x1, y1, pic3.BackColor);
        while (q.Count > 0)
        {
            p = (Point)q.Dequeue();
            x1 = p.X;
            y1 = p.Y;
            for (int i = 1; i <= 4; i++)
            {
                int x2 = x1;
                int y2 = y1;
                switch (i)
                {
                    case 1://left
                        x2--;
                        break;
                    case 2://Right
                        x2++;
                        break;
                    case 3://down
                        y2++;
                        break;
                    case 4://up
                        y2--;
                        break;
                }
                if (x2 < img.Width && x2 >= 0 && y2 < img.Height && y2 >= 0)
                {
                    if (img.GetPixel(x2, y2) == pic2.BackColor)
                    {
                        img.SetPixel(x2, y2, pic3.BackColor);
                        Point p2 = new Point(x2, y2);
                        q.Enqueue(p2);
                    }
                }
            }
        }
        pic1.Image = img;
    }
}

Input image (that isn't original photos, because of reducing web page size):

Output image after two fills:

Form view:

Points of Interest

Fill any type of image in color range region.

History

  • Implemented in April 2013

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mojtaba Mahdavi Eng
Software Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Member
software developer and supportor and teacher of CEH (Certifacte ethical hacker), Network+ , C# , PHP , SQL and , Mysql , Matlab and... in university and other centers.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130513.1 | Last Updated 2 May 2013
Article Copyright 2013 by Mojtaba Mahdavi Eng
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid