Click here to Skip to main content
11,572,389 members (48,573 online)
Click here to Skip to main content

Masking(Multiplication) of Two images using C#

, 10 Mar 2009 CPOL 28K 1.9K 18
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kunal Pawar
Software Developer (Senior)
India India
I am Software developer, working on web and Windows technology. Willing to accept challengces and try to complete them.

You may also be interested in...

Comments and Discussions

 
GeneralGreat!!! Pin
tasco2-May-11 17:26
membertasco2-May-11 17:26 
GeneralSuggested enhancements Pin
robertjb2016-Mar-10 5:53
memberrobertjb2016-Mar-10 5:53 
Sorry for the long post. It is intended as constructive critism only.

Please include sample bitmaps SrcBitmap1 and SrcBitmap2 as part of your demo.
It makes it much easier to see the results when we run the program.

You mention that we can make our selection using circles, ellipses, rectangles or polygons.
Please include sample bitmaps allowing us to make these kinds of selections. Also, the source code
to generate these types of selections would be very nice also.

Replace:
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);
with:
bitmap = new Bitmap( Math.Min( SrcBitmap1.Width , SrcBitmap2.Width ),
Math.Min( SrcBitmap1.Height, SrcBitmap2.Height ) );

Next: It is not a very good practice to dispose of bitmaps passed as paramaters to a method. The method
which created them should perform the disposal.

Next: In the next two for loop declarations, you have reversed the 'row' and 'col' variables.
You will also not process the last row and last column because of the height - 1 and width - 1.

Replace:
for (int col = 0; col < bitmap.Height - 1; col++)
With:
for (int row = 0; row < bitmap.height; row++)

Replace:
for (int row = 0; row < bitmap.Width - 1; row++)
With:
for (int col = 0; col < bitmap.Width; col++)

Next: You re-calculate Src1Ptr, Src2Ptr, DestPtr inside the first for loop. It should be moved
before the first for loop.

Next: You calculate row*xOffset : 15 times, row*xOffset + 1 : 5 times,
row*xOffset + 2 : 5 times.

It is bad programming practice to do this and will slow down your code unnecessairily especially
for larger bitmaps.

Next: The byte conversions for 0 in the following 3 lines are unnecessary since C# handles it for you.
DestPtr[row * xOffset] = (byte)(0);
DestPtr[row * xOffset + 1] = (byte)(0);
DestPtr[row * xOffset + 2] = (byte)(0);

Next: The byte conversions for the next 3 lines are unnecessary since Src2[ position ] is of type byte.
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]);

Lastly: Please see the note just before your multiplication test to see if you have masked out the
color or not.

I have re-written the unsafe{ ... } portion of your code as follows. It is mostly untested because
of the sample bitmaps not being provided with the demo program. If there are any problems, please
let me know. I have added comments so that you can follow my changes.

I have not looked at the other parts of your library yet.

unsafe { int xOffset = 3;
// ----------------------------------------------------------
// - Calculate the starting positions of each bitmap's data -
// - block. -
// - Moved from inside the loops so that it is calculated -
// - only once. RJB -
// ----------------------------------------------------------
byte* Src1Ptr = (byte*) Src1Data.Scan0.ToPointer();
byte* Src2Ptr = (byte*) Src2Data.Scan0.ToPointer();
byte* DestPtr = (byte*) DestData.Scan0.ToPointer();

// ----------------------------------------------------------
// - Calculate the number of bytes we must move each pointer-
// - at the end of each bitmap row. -
// - ( I added the following 4 lines. ) RJB. -
// ----------------------------------------------------------
int DestDataOffset = DestData.Width * xOffset;

int Src1LineEndOffset = Src1Data.Stride - DestDataOffset;
int Src2LineEndOffset = Src2Data.Stride - DestDataOffset;
int DestLineEndOffset = DestData.Stride - DestDataOffset;

// ----------------------------------------------------------
// - For each row and all the columns in each row, do the -
// - following: RJB -
// - (Note: I reversed the order of the row and col -
// - variables because the author had the col variable -
// - going between 0 and height - 1 and the row variable -
// - going between 0 and width - 1 ) -
// - -
// - (Note: the author was losing the last row and column -
// - because of col < bitmap.Height - 1 and -
// - row < bitmap.Width - 1. : Fixed. ) -
// ----------------------------------------------------------
for ( int row = 0; row < bitmap.Height; row++ ) {
for ( int col = 0; col < bitmap.Width; col++ ) {
// -----------------------------------------------------
// - Eliminated row * xOffset being re-calculated 15 -
// - times within this loop. Also eliminated the 5 -
// - times each row * xOffset + 1 and -
// - row * xOffset + 2 -
// - were being re-calculated inside this loop. RJB -
// -----------------------------------------------------
clr1 = ( Src1Ptr[ 0 ] + Src1Ptr[ 1 ] + Src1Ptr[ 2 ] ) / 3;
clr2 = ( Src2Ptr[ 0 ] + Src2Ptr[ 1 ] + Src2Ptr[ 2 ] ) / 3;

// ------------------------------------------------------
// - The following color patterns for either clr1 or -
// - clr2 will give 0 when these two averages are -
// - multiplied together ( interger division ): -
// - -
// - ( 0 + 0 + 0 )/3 = 0, ( 0 + 0 + 1 )/3 = 0, -
// - ( 0 + 1 + 0 )/3 = 0, ( 0 + 1 + 1 )/3 = 0, -
// - ( 1 + 0 + 0 )/3 = 0, ( 1 + 0 + 1 )/3 = 0, -
// - ( 1 + 1 + 0 )/3 = 0, ( 1 + 0 + 1 )/3 = 0 -
// - -
// - I am not familiar enough with this method to say if-
// - this is what the author intended or not. RJB -
// ------------------------------------------------------
if ( ( clr1 * clr2 ) == 0 ) {
DestPtr[ 0 ] = 0;
DestPtr[ 1 ] = 0;
DestPtr[ 2 ] = 0;
} else {
DestPtr[ 0 ] = Src2Ptr[ 0 ];
DestPtr[ 1 ] = Src2Ptr[ 1 ];
DestPtr[ 2 ] = Src2Ptr[ 2 ];
}
}

// -----------------------------------------------------------
// - Move onto the next pixel in the 3 bitmaps. -
// - ( I added the following 3 lines. ) RJB. -
// -----------------------------------------------------------
Src1Ptr += xOffset;
Src2Ptr += xOffset;
DestPtr += xOffset;
}

// ---------------------------------------------------------------
// - Move to the beginning of the next row in the 3 bitmaps. -
// - ( I added the following 3 lines. ) RJB. -
// ---------------------------------------------------------------
Src1Ptr += Src1LineEndOffset;
Src2Ptr += Src2LineEndOffset;
DestPtr += DestLineEndOffset;
}

bitmap.UnlockBits( DestData );
SrcBitmap1.UnlockBits( Src1Data );
SrcBitmap2.UnlockBits( Src2Data );
// -------------------------------------------------------------------
// - Never dispose of anything passed as parameters to a function. -
// - Always dispose of them in the calling method that created them. -
// -------------------------------------------------------------------
//SrcBitmap1.Dispose();
//SrcBitmap2.Dispose();
}

Otherwise, I like the idea of performing this type of operation on bitmaps.

Thank you.

Robert

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 10 Mar 2009
Article Copyright 2009 by Kunal Pawar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid