绘制两位图的结果位图差异的问题 [英] Issue in plotting resultant bit map of two bit maps difference

查看:82
本文介绍了绘制两位图的结果位图差异的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想将一个位图与另一个位图(参考位图)进行比较,并在结果位图中绘制它的所有差异。使用下面的代码,我只能绘制差异区域,但不能绘制它的确切颜色。



这是我的代码


< pre class ="prettyprint"> Bitmap ResultantBitMap = new Bitmap(bitMap1.Height,bitMap2.Height);

BitmapData bitMap1Data = bitMap1.LockBits(new Rectangle(0,0,bitMap1.Width,bitMap1.Height),System.Drawing.Imaging.ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format32bppArgb );
BitmapData bitMap2Data = bitMap2.LockBits(new Rectangle(0,0,bitMap2.Width,bitMap2.Height),System.Drawing.Imaging.ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
BitmapData bitMapResultantData = ResultantBitMap.LockBits(new Rectangle(0,0,ResultantBitMap.Width,ResultantBitMap.Height),System.Drawing.Imaging.ImageLockMode.ReadWrite,System.Drawing.Imaging.PixelFormat.Format32bppArgb);

IntPtr scan0 = bitMap1Data.Scan0;
IntPtr scan02 = bitMap2Data.Scan0;
IntPtr scan0ResImg1 = bitMapResultantData.Scan0;


int bitMap1Stride = bitMap1Data.Stride;
int bitMap2Stride = bitMap2Data.Stride;
int ResultantImageStride = bitMapResultantData.Stride;

for(int y = 0; y< bitMap1.Height; y ++)
{
//定义第一个循环内的指针,用于并行
byte * p =(byte *)scan0.ToPointer();
p + = y * bitMap1Stride;

byte * p2 =(byte *)scan02.ToPointer();
p2 + = y * bitMap2Stride;

byte * pResImg1 =(byte *)scan0ResImg1.ToPointer();
pResImg1 + = y * ResultantImageStride;

for(int x = 0; x< bitMap1.Width; x ++)
{
//当找到差异时总是得到完整像素
if( Math.Abs​​(p [0] - p2 [0])> = 20 || Math.Abs​​(p [1] - p2 [1])> = 20 || Math.Abs​​(p [2] - p2 [2])> = 20)
{
pResImg1 [0] = p2 [0]; // B
pResImg1 [1] = p2 [1]; // R
pResImg1 [2] = p2 [2]; // G
pResImg1 [3] = p2 [3]; // A(不透明度)
}

p + = 4;
p2 + = 4;
pResImg1 + = 4;
}
}

bitMap1.UnlockBits(bitMap1Data);
bitMap2.UnlockBits(bitMap2Data);
ResultantBitMap.UnlockBits(bitMapResultantData);

ResultantBitMap.Save(@" c:\\abcd \abcd.jpeg");




< p style ="padding-right:0px; line-height:inherit; font-family:Arial,'Helvetica Neue',Helvetica,sans-serif; font-size:15px; vertical-align:baseline; clear:both; color :#242729">
我想要的是具有参考图像精确颜色的差异图像。

解决方案

< blockquote>

您好Abhilash Katkuri,


感谢您在此发帖。


如果您有任何问题,请尝试以下代码。

 private void Get_difference_between_two_images_Load(object sender,EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;
}

private void BtnGet_Click(object sender,EventArgs e)
{
//加载图像。
位图bm1 =(位图)pictureBox1.Image;
位图bm2 =(位图)pictureBox2.Image;

尺寸s1 = bm1.Size;
尺寸s2 = bm2.Size;
if(s1!= s2)MessageBox.Show(" Error!");

位图bm3 =新位图(s1.Width,s1.Height);
位图bm4 =新位图(s1.Width,s1.Height);

for(int y = 0; y< s1.Height; y ++)
for(int x = 0; x< s1.Width; x ++)
{
颜色c1 = bm1.GetPixel(x,y);
颜色c2 = bm2.GetPixel(x,y);
if(c1 == c2)
{
bm3.SetPixel(x,y,Color.Black);
bm4.SetPixel(x,y,Color.Black);
}
其他
{
bm3.SetPixel(x,y,c1);
bm4.SetPixel(x,y,c2);
}

}
pictureBox3.Image = bm3;
pictureBox4.Image = bm4;
}

private void BtnLoad_Click_1(对象发件人,EventArgs e)
{
pictureBox1.Load(@" 3.png");
pictureBox2.Load(@" 4.png");
}


最好的问候,


Wendy


I want to compare one bitmap with another bitmap (reference bitmap) and draw all the difference of it in resultant bit map. Using below code I am able to draw only difference area but not with exact color of it.

Here is my code

Bitmap ResultantBitMap = new Bitmap(bitMap1.Height, bitMap2.Height);

        BitmapData bitMap1Data = bitMap1.LockBits(new Rectangle(0, 0, bitMap1.Width, bitMap1.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        BitmapData bitMap2Data = bitMap2.LockBits(new Rectangle(0, 0, bitMap2.Width, bitMap2.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        BitmapData bitMapResultantData = ResultantBitMap.LockBits(new Rectangle(0, 0, ResultantBitMap.Width, ResultantBitMap.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        IntPtr scan0 = bitMap1Data.Scan0;
        IntPtr scan02 = bitMap2Data.Scan0;
        IntPtr scan0ResImg1 = bitMapResultantData.Scan0;


        int bitMap1Stride = bitMap1Data.Stride;
        int bitMap2Stride = bitMap2Data.Stride;
        int ResultantImageStride = bitMapResultantData.Stride;

        for (int y = 0; y < bitMap1.Height; y++)
        {
            //define the pointers inside the first loop for parallelizing
            byte* p = (byte*)scan0.ToPointer();
            p += y * bitMap1Stride;

            byte* p2 = (byte*)scan02.ToPointer();
            p2 += y * bitMap2Stride;

            byte* pResImg1 = (byte*)scan0ResImg1.ToPointer();
            pResImg1 += y * ResultantImageStride;

            for (int x = 0; x < bitMap1.Width; x++)
            {
                //always get the complete pixel when differences are found                   
                if (Math.Abs(p[0] - p2[0]) >= 20 || Math.Abs(p[1] - p2[1]) >= 20 || Math.Abs(p[2] - p2[2]) >= 20)
                {
                    pResImg1[0] = p2[0];// B
                    pResImg1[1] = p2[1];//R
                    pResImg1[2] = p2[2];//G
                    pResImg1[3] = p2[3];//A    (Opacity)
                }

                p += 4;
                p2 += 4;
                pResImg1 += 4;
            }
        }

        bitMap1.UnlockBits(bitMap1Data);
        bitMap2.UnlockBits(bitMap2Data);
        ResultantBitMap.UnlockBits(bitMapResultantData);

        ResultantBitMap.Save(@"c:\\abcd\abcd.jpeg");


What I want is the difference image with exact color of the reference image.

解决方案

Hi Abhilash Katkuri,

Thank you for posting here.

For your question, please try the code below.

   private void Get_difference_between_two_images_Load(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void BtnGet_Click(object sender, EventArgs e)
        {
            // Load the images.
            Bitmap bm1 = (Bitmap)pictureBox1.Image;
            Bitmap bm2 = (Bitmap)pictureBox2.Image;

            Size s1 = bm1.Size;
            Size s2 = bm2.Size;
            if (s1 != s2) MessageBox.Show("Error!");

            Bitmap bm3 = new Bitmap(s1.Width, s1.Height);
            Bitmap bm4 = new Bitmap(s1.Width, s1.Height);

            for (int y = 0; y < s1.Height; y++)
                for (int x = 0; x < s1.Width; x++)
                {
                    Color c1 = bm1.GetPixel(x, y);
                    Color c2 = bm2.GetPixel(x, y);
                    if (c1 == c2)
                    {
                        bm3.SetPixel(x, y, Color.Black);
                        bm4.SetPixel(x, y, Color.Black);
                    }
                    else
                    {
                        bm3.SetPixel(x, y, c1);
                        bm4.SetPixel(x, y, c2);
                    }

                }
            pictureBox3.Image = bm3;
            pictureBox4.Image = bm4;
        }

        private void BtnLoad_Click_1(object sender, EventArgs e)
        {
            pictureBox1.Load(@"3.png");
            pictureBox2.Load(@"4.png");
        }

Best Regards,

Wendy


这篇关于绘制两位图的结果位图差异的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆