Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Masking(Multiplication) of Two images using C#

0.00/5 (No votes)
9 Mar 2009 1  
This article helps to mask(multiply) second image on first image using C#.

Introduction

This is my second article. In Matlab there is functionality to mask one image on other image or in words we can multiply images. Same functionality we can view in photoshop also. Using .Net we can not mask(multiply) images directly so I want to discuss code.

Purpose

I am from imaging specially for analysis. In imaging it required to show image in specific shapes, and not only for display but also for analysis. For example analyze only selected part of image. Selection may be using circle, ellipse, rectangle or polygon that time we have to mask image with black and white image. This article helps developer, who work on imageing project.

Using the Code

Any body can download code use ImageProcessingLib as like plug and play to mask image and perform Dilation operation on image.

This function accepts two bitmap one for backgrond and second for foreground and function returns masked Bitmap of two images. GetPixel and SetPixel functions have several drawbacks so we decided to use Pointer. We access and modify a pixel value using Pointer. The next example utilizes the “unsafe” block in C#. Inside unsafe blocks, we have access to pointers from C#. The conclusion is that pointers in unsafe blocks are faster than GetPixel and SetPixel functions.

//
// This functon used to mask(multiply) two images bitmap.
//
public Bitmap MaskImagePtr(Bitmap SrcBitmap1, Bitmap SrcBitmap2, out string Message)
{
	int width;
	int height;

	Message = "";

	if (SrcBitmap1.Width < SrcBitmap2.Width)
		width = SrcBitmap1.Width;
	else
		width = SrcBitmap2.Width;

	if (SrcBitmap1.Height < SrcBitmap2.Height)
		height = SrcBitmap1.Height;
	else
		height = SrcBitmap2.Height;

	bitmap = new Bitmap(width, height);
	int clr1, clr2;

	try
	{
		BitmapData Src1Data = SrcBitmap1.LockBits(new Rectangle(0, 0, SrcBitmap1.Width, SrcBitmap1.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
		
		BitmapData Src2Data = SrcBitmap2.LockBits(new Rectangle(0, 0, SrcBitmap2.Width, SrcBitmap2.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
		
		BitmapData DestData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

		unsafe
		{
			int xOffset = 3;

			for (int col = 0; col < bitmap.Height - 1; col++)
			{
				byte* Src1Ptr = (byte*)Src1Data.Scan0 + col * Src1Data.Stride;
				byte* Src2Ptr = (byte*)Src2Data.Scan0 + col * Src2Data.Stride;
				byte* DestPtr = (byte*)DestData.Scan0 + col * DestData.Stride;

				for (int row = 0; row < bitmap.Width - 1; row++)
				{
					clr1 = (Src1Ptr[row * xOffset] + Src1Ptr[row * xOffset + 1] + Src1Ptr[row * xOffset + 2]) / 3;
					clr2 = (Src2Ptr[row * xOffset] + Src2Ptr[row * xOffset + 1] + Src2Ptr[row * xOffset + 2]) / 3;

					clr1 *= clr2;

					if (clr1 == 0)
					{
						DestPtr[row * xOffset] = (byte)(0);
						DestPtr[row * xOffset + 1] = (byte)(0);
						DestPtr[row * xOffset + 2] = (byte)(0);
					}
					else
					{
						DestPtr[row * xOffset] = (byte)(Src2Ptr[row * xOffset]);
						DestPtr[row * xOffset + 1] = (byte)(Src2Ptr[row * xOffset + 1]);
						DestPtr[row * xOffset + 2] = (byte)(Src2Ptr[row * xOffset + 2]);
					}
				}
			}
		}

		bitmap.UnlockBits(DestData);
		SrcBitmap1.UnlockBits(Src1Data);
		SrcBitmap2.UnlockBits(Src2Data);

		SrcBitmap1.Dispose();
		SrcBitmap2.Dispose();
	}
	catch (Exception ex)
	{
		Message = ex.Message;
	}

	return bitmap;
}

In this function, first we calclate minimum width and height of SrcBitmap1 and SrcBitmap2. Then create new destination bitmap for same height & width. We calculate the start and end addresses of the image structure, thinking that it is a 1D linear array. We increment the pointer from start to end addresses and multiply values both images, if multiplication is zero then store black value in destination pointer otherwise store second image pixel value in destination pointer.xOffset is used to go to next row of bits for 24 bit pixel image.

Following is list of offset for different bit images

  • 8 bit : 1
  • 16 bit : 2
  • 24 bit : 3 and
  • 32 bit : 4

Points of Interest

In this project Dilation algorithm (mathematical morphology operation) is also implemented. Also zooming operations are perform using context menu.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here