SIGN UP MEMBER LOGIN:    
ARTICLE

C# Image Proccessing Techniques: Part III (Substitution of Pixels)

Posted by Tamer Khalil Articles | Algorithms in C# June 21, 2010
In the series of article we will build together our general use image processing class in C#.
Reader Level:

A Digital image is simply a multidimensional array; this array contains numbers which represents the color of points in the image. Therefore, we could easily amend this array easily and do whatever we want with this array. This could include entering this array (Matrix) into a mathematical function for making any amendments in it, or for encrypting the image. Or in this article I will show you a method used in substituting two pixels from two images, for instance you could take a photo for you and place a background of Evil tower behind you. The same technique used in movies, and in the media.

What do we need?

We need to have two images, an image for Evil tower, and a personal image for you with a plain color background (for simplicity). Make sure that the two images have reasonable sizes to substitute them together. In the next articles I will show you different methods of scaling of images.

Now let us move to the steps to write our program.

First you should determine the color components of the background of your picture, please refer to my previous article to see how to get the color component of a point. After that make a nested for loop that traverses over the two images and replace the selected color components of the background, with the color components of the corresponding pixel in the Evil tower image.

Here is the code

Bitmap Pixelssubstitutions(Bitmap PersonalImage, Bitmap EvilImage, Color PlainColor)
        {
            Bitmap NewImage=PersonalImage;
            for(int i=0;i<NewImage.Height;i++)
                for (int j = 0; j < NewImage.Width; j++)
                {
                    Color Pixel=NewImage.GetPixel(j,i);
                    if(Pixel==PlainColor)
                    {
                        NewImage.SetPixel(j,i,EvilImage.GetPixel(j,i));
                    }
                }
 
            return NewImage;
        }

The Output

fig.gif

You can see that some pixels around my body is not replaced with the corresponding Evil tower image, that is because not all plain background pixels have the same exact color components there may be a slight difference, therefore it is better to put a range in the color components selected pixel.

In the next article we will discuss several techniques of image scaling using C#. Please feel free to contact me if you need anything concerning this topic.

 

Login to add your contents and source code to this article
share this article :
post comment
 

In that parameter argument what is the value for plaincolor.

Posted by baskaran chellasamy May 10, 2012

Hi,

I used 2 ways in capturing images. I used WIA( Windows Image acquisition), and Directx.
The best articles you could find is in codeproject.com.  These are two articles that uses both ways.

http://www.codeproject.com/KB/directx/DirXVidStrm.aspx
http://www.codeproject.com/KB/cs/WebCamService.aspx

Posted by Tamer Khalil Oct 01, 2010

how can i take a picture from my camera with c#?
thanks for sharing.it's great.

Posted by phan lam Oct 01, 2010

Thanks mahesh, I hope that I could finish the series ASAP.

Posted by Tamer Khalil Jun 27, 2010

Thanks Tamer for sharing a good series.

Posted by Mahesh Chand Jun 26, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor