TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ankush belorkar
NA
8
0
image comparison
Mar 16 2010 8:24 AM
hello,
i want to compare two image and difference should be saved in another image file. i am using c#.net.
i have write the following code to compare two bitmap file but when i try to compare images from my harddisk it does not show correct output.
i use following statement to use my image stored on harddisk.
public
partial
class
Form1
:
Form
{
public
Form1()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender,
EventArgs
e)
{
ServiceReference1.
Service1SoapClient
f =
new
ServiceReference1.
Service1SoapClient
();
label1.Text= f.HelloWorld();
Bitmap
image1 =
new
Bitmap
(400, 400);
using
(
Graphics
g =
Graphics
.FromImage(image1))
{
g.DrawRectangle(
Pens
.Blue,
new
Rectangle
(0, 0, 60, 60));
g.DrawRectangle(
Pens
.Red,
new
Rectangle
(0, 0, 100, 100));
}
image1.Save(
"D:\\test-1.Jpeg"
,
ImageFormat
.Jpeg);
Bitmap
image2 = (
Bitmap
)image1.Clone();
using
(
Graphics
g =
Graphics
.FromImage(image2))
{
g.DrawRectangle(
Pens
.Purple,
new
Rectangle
(0, 0, 100, 100));
}
image2.Save(
"D:\\test-2.Jpeg"
,
ImageFormat
.Jpeg);
Bitmap
diff =
ImageTool
.GetDifferenceImage(image1, image2,
Color
.Pink);
diff.MakeTransparent(
Color
.Pink);
diff.Save(
"D:\\test-diff.bmp"
,
ImageFormat
.Bmp);
}
public
class
ImageTool
{
public
static
unsafe
Bitmap
GetDifferenceImage(
Bitmap
image1,
Bitmap
image2,
Color
matchColor)
{
if
(image1 ==
null
| image2 ==
null
)
return
null
;
if
(image1.Height != image2.Height || image1.Width != image2.Width)
return
null
;
Bitmap
diffImage = image2.Clone()
as
Bitmap
;
int
height = image1.Height;
int
width = image1.Width;
BitmapData
data1 = image1.LockBits(
new
Rectangle
(0, 0, width, height),
ImageLockMode
.ReadOnly,
PixelFormat
.Format24bppRgb);
BitmapData
data2 = image2.LockBits(
new
Rectangle
(0, 0, width, height),
ImageLockMode
.ReadOnly,
PixelFormat
.Format24bppRgb);
BitmapData
diffData = diffImage.LockBits(
new
Rectangle
(0, 0, width, height),
ImageLockMode
.WriteOnly,
PixelFormat
.Format24bppRgb);
byte
* data1Ptr = (
byte
*)data1.Scan0;
byte
* data2Ptr = (
byte
*)data2.Scan0;
byte
* diffPtr = (
byte
*)diffData.Scan0;
byte
[] swapColor =
new
byte
[3];
swapColor[0] = matchColor.B;
swapColor[1] = matchColor.G;
swapColor[2] = matchColor.R;
int
rowPadding = data1.Stride - (image1.Width * 3);
// iterate over height (rows)
for
(
int
i = 0; i < height; i++)
{
// iterate over width (columns)
for
(
int
j = 0; j < width; j++)
{
int
same = 0;
byte
[] tmp =
new
byte
[3];
// compare pixels and copy new values into temporary array
for
(
int
x = 0; x < 3; x++)
{
tmp[x] = data2Ptr[0];
if
(data1Ptr[0] == data2Ptr[0])
{
same++;
}
data1Ptr++;
// advance image1 ptr
data2Ptr++;
// advance image2 ptr
}
// swap color or add new values
for
(
int
x = 0; x < 3; x++)
{
diffPtr[0] = (same == 3) ? matchColor.G : tmp[x];
//swapColor[x] : tmp[x];
diffPtr++;
// advance diff image ptr
}
}
// at the end of each column, skip extra padding
if
(rowPadding > 0)
{
data1Ptr += rowPadding;
data2Ptr += rowPadding;
diffPtr += rowPadding;
}
}
image1.UnlockBits(data1);
image2.UnlockBits(data2);
diffImage.UnlockBits(diffData);
return
diffImage;
}
}
}
would anyone help me out.
thanks in advance
Reply
Answers (
0
)
email sending code using .net code
MS 2008 c# SQL remote server