Thursday, May 9, 2013

Find differences between images C#

when i run your code to get diff & combine image then i got the error and the error message was


Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


error is throwing for this line of code if (p[0] != p2[0] || p[1] != p2[1] || pRes[2] != p2[2])


so here i am uploading two image which i was comparing. so u just save this two image and run your code to compare my given image.


two image given first one name was pic1.png & pic2.png. also here again i am pasting my code then way i use your code. please have look and fix the error.



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace PictureCompare
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Init();
}

private void button1_Click(object sender, EventArgs e)
{
//Bitmap image1 = new Bitmap(@"c:\users\tridip\documents\visual studio 2010\Projects\PictureCompare\PictureCompare\pic1.png");
//Bitmap image2 = new Bitmap(@"c:\users\tridip\documents\visual studio 2010\Projects\PictureCompare\PictureCompare\pic2.png");

//Bitmap diff = ImageTool.GetDifferenceImage(image1, image2, Color.Pink);
//diff.MakeTransparent(Color.Pink);
//diff.Save(@"d:\test-diff.png", System.Drawing.Imaging.ImageFormat.Png);



}

private void Form1_Load(object sender, EventArgs e)
{
SetUpBitmaps();

Bitmap bmp3 = GetDiffBitmap((Bitmap)this.pictureBox1.Image, (Bitmap)this.pictureBox2.Image);

this.pictureBox3.Image = bmp3;

Bitmap bComb = new Bitmap(bmp3.Width, bmp3.Height);

using (Graphics g = Graphics.FromImage(bComb))
{
g.DrawImage(this.pictureBox1.Image, 0, 0, bComb.Width, bComb.Height);
g.DrawImage(bmp3, 0, 0, bComb.Width, bComb.Height);
}

this.pictureBox4.Image = bComb;
}

private unsafe Bitmap GetDiffBitmap(Bitmap bmp, Bitmap bmp2)
{
if (bmp.Width != bmp2.Width || bmp.Height != bmp2.Height)
throw new Exception("Sizes must be equal.");

Bitmap bmpRes = null;

System.Drawing.Imaging.BitmapData bmData = null;
System.Drawing.Imaging.BitmapData bmData2 = null;
System.Drawing.Imaging.BitmapData bmDataRes = null;

try
{
bmpRes = new Bitmap(bmp.Width, bmp.Height);

bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
bmData2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
bmDataRes = bmpRes.LockBits(new Rectangle(0, 0, bmpRes.Width, bmpRes.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

IntPtr scan0 = bmData.Scan0;
IntPtr scan02 = bmData2.Scan0;
IntPtr scan0Res = bmDataRes.Scan0;

int stride = bmData.Stride;
int stride2 = bmData2.Stride;
int strideRes = bmDataRes.Stride;

int nWidth = bmp.Width;
int nHeight = bmp.Height;

//for(int y = 0; y < nHeight; y++)
System.Threading.Tasks.Parallel.For(0, nWidth, y =>
{
//define the pointers inside the first loop for parallelizing
byte* p = (byte*)scan0.ToPointer();
p += y * stride;
byte* p2 = (byte*)scan02.ToPointer();
p2 += y * stride2;
byte* pRes = (byte*)scan0Res.ToPointer();
pRes += y * strideRes;

for (int x = 0; x < nWidth; x++)
{
//always get the complete pixel when differences are found
// the below line giving error & error is "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
if (p[0] != p2[0] || p[1] != p2[1] || pRes[2] != p2[2])
{
pRes[0] = p2[0];
pRes[1] = p2[1];
pRes[2] = p2[2];

//alpha (opacity)
pRes[3] = p2[3];
}

p += 4;
p2 += 4;
pRes += 4;
}
});

bmp.UnlockBits(bmData);
bmp2.UnlockBits(bmData2);
bmpRes.UnlockBits(bmDataRes);
}
catch
{
if (bmData != null)
{
try
{
bmp.UnlockBits(bmData);
}
catch
{

}
}

if (bmData2 != null)
{
try
{
bmp2.UnlockBits(bmData2);
}
catch
{

}
}

if (bmDataRes != null)
{
try
{
bmpRes.UnlockBits(bmDataRes);
}
catch
{

}
}

if (bmpRes != null)
{
bmpRes.Dispose();
bmpRes = null;
}
}

return bmpRes;
}

private void SetUpBitmaps()
{
Bitmap bmp1 = new Bitmap(@"c:\users\tridip\documents\visual studio 2010\Projects\PictureCompare\PictureCompare\pic1.png");
Bitmap bmp2 = new Bitmap(@"c:\users\tridip\documents\visual studio 2010\Projects\PictureCompare\PictureCompare\pic2.png");

//Bitmap bmp1 = new Bitmap(this.pictureBox1.ClientSize.Width, this.pictureBox1.ClientSize.Height);

//using (Graphics g = Graphics.FromImage(bmp1))
//{
// g.Clear(Color.Green);

// g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// g.FillEllipse(Brushes.Red, new Rectangle(0, 0, bmp1.Width, bmp1.Height));
//}

//Bitmap bmp2 = new Bitmap(this.pictureBox2.ClientSize.Width, this.pictureBox2.ClientSize.Height);

//using (Graphics g = Graphics.FromImage(bmp2))
//{
// g.Clear(Color.Green);

// g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

// using (System.Drawing.Drawing2D.LinearGradientBrush l = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, bmp2.Width, bmp2.Height), Color.Red, Color.Yellow, 0F))
// g.FillEllipse(l, new Rectangle(0, 0, bmp2.Width, bmp2.Height));
//}

this.pictureBox1.Image = bmp1;
this.pictureBox2.Image = bmp2;
}
private void Init()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.pictureBox4 = new System.Windows.Forms.PictureBox();

this.pictureBox1.Location = new System.Drawing.Point(13, 28);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(150, 150);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;

this.pictureBox2.Location = new System.Drawing.Point(196, 28);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(150, 150);
this.pictureBox2.TabIndex = 0;
this.pictureBox2.TabStop = false;

this.pictureBox3.Location = new System.Drawing.Point(376, 28);
this.pictureBox3.Name = "pictureBox3";
this.pictureBox3.Size = new System.Drawing.Size(150, 150);
this.pictureBox3.TabIndex = 0;
this.pictureBox3.TabStop = false;

this.pictureBox4.Location = new System.Drawing.Point(553, 28);
this.pictureBox4.Name = "pictureBox4";
this.pictureBox4.Size = new System.Drawing.Size(150, 150);
this.pictureBox4.TabIndex = 0;
this.pictureBox4.TabStop = false;

this.ClientSize = new System.Drawing.Size(740, 214);
this.Controls.Add(this.pictureBox4);
this.Controls.Add(this.pictureBox3);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}
}
}





No comments:

Post a Comment