通过图像减法的概念来拼接图像 [英] Stitching images by using the concept of image substraction

查看:89
本文介绍了通过图像减法的概念来拼接图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用C#编程语言,并且当前正在尝试通过使用图像减法的概念在两个图像之间进行拼接.我选择两个图像作为输入,它们的图像大小相同,并产生第三个图像作为输出.图像是灰度图像.基本上,我的结果图像应该是全景图像.通过使用该概念可以拼接图像吗?感谢您的评论和建议.

Hi everyone,

I am using C# programming language and I am currently trying to stitch between two images by using the concept of image subtraction . I choose two images as input which identically sized images and produces as output a third image. The images are the gray scale images. Basically my result images should be the panorama images. Is it possible by using that concept to stitch the images? Thanks for your comments and advises

推荐答案

是的.
我假设通过减法,您想要让其面积大于0的区域为粘滞区域.

您将需要一些阈值之类的东西,但实际上并没有那么复杂.如果性能很重要,那么您可能需要考虑一些事情,例如以本机代码进行处理或使用第三方库.使用GetPixel和SetPixel的速度极慢,此过程将需要您触摸很多像素.如果您想留在C#中并且不想使用第三者,我建议您使用不安全的代码块.

AForge [ ^ ]是一个非常不错的开源库,并且还有很多其他库(只是在Google周围搜索图像库).

如果您不需要很多其他图像处理技术,尽管您自己构建它就不会太困难,您将学到很多东西.如果您遇到困难,请随时发布一些更具体的问题,我相信您会得到一些帮助.
Yes it is possible.

I assume by subtraction you are wanting to have the area of which is more 0ed be the stictched area.

You will need some thresholding and such, but it is really not that complicated. If performance matters though, you may want to consider a few things such as processing in native code or using 3rd party libraries. Using the GetPixel and SetPixel is extremly slow and this process will require you to touch a lot of pixels. If you want to stay in C# and don''t want to use 3rd party I recomend using unsafe code chuncks.

AForge[^] is a pretty good open source library and there are also numerous others (just google around for Image Libraries).

If you do not need many other image processing techniques though building it yourself would not be too hard and you would learn quite a bit. If you get stuck feel free to post some more specific questions and I am sure you will get some help.


这是我到目前为止编写的源代码.但是此源代码仍然存在一些错误.我仍然无法拼接图像,但是我的源代码可以将两个图像相减并将它们合并在一起.

问题是:
1.图像仅合并"在一起,但没有缝合在一起.
2.输出图像的尺寸仍然相同.我需要根据缝合图像的尺寸获取输出图像的尺寸.因此换句话说,应用我的结果后,图像的大小应该改变.

And this the source code which I have written so far. But this source code still has some mistakes. I am still unable to stitch the image but my source code can subtract between the two images and merge them together.

The problems are :
1. The images are only "merged" together but they are not stitched together.
2. The size of the output images are still same. I need to get the size of the output image base on the size of the stitched image. So in other words the size of the image should change after I apply the result.

public Bitmap Substraction(Bitmap bmpA, Bitmap bmpB)
 {

     BitmapData bmpData_A = bmpA.LockBits(new Rectangle(0, 0, bmpA.Width, bmpA.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
     BitmapData bmpData_B = bmpB.LockBits(new Rectangle(0, 0, bmpB.Width, bmpB.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

     int width_A = bmpData_A.Width;
     int width_B = bmpData_B.Width;

     int height_A = bmpData_A.Height;
     int height_B = bmpData_B.Height;

     bmpA.UnlockBits(bmpData_A);
     bmpB.UnlockBits(bmpData_B);


     Bitmap input_img1 = new Bitmap(bmpA, width_A, height_A);
     Bitmap input_img2 = new Bitmap(bmpB, width_B, height_B);

     int width_output = width_A + width_B;
     int height_output = height_A + width_B;

     Bitmap output_image = new Bitmap(width_A, height_B);

     BitmapData bmpdata1 = input_img1.LockBits(new Rectangle(0, 0, input_img1.Width, input_img1.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
     BitmapData bmpdata2 = input_img2.LockBits(new Rectangle(0, 0, input_img2.Width, input_img2.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
     BitmapData bmpoutput = output_image.LockBits(new Rectangle(0, 0, output_image.Width, output_image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);


     unsafe
     {
         int ptr1 = bmpdata1.Stride - bmpdata1.Width * 3;
         int ptr2 = bmpdata2.Stride - bmpdata2.Width * 3;
         int ptr3 = bmpoutput.Stride - bmpoutput.Width * 3;

         //Pointing towards the 1st byte in Array
         byte* p1 = (byte*)bmpdata1.Scan0;
         byte* p2 = (byte*)bmpdata2.Scan0;
         byte* p3 = (byte*)bmpoutput.Scan0;


         for (int y = 0; y < height_A; y++)
         {
             for (int x = 0; x < width_A * 3; x++)
             {

                     p3[0] = (byte)(Math.Abs(p2[0] - p1[0]));

                     p1++;
                     p2++;
                     p3++;


             }

             p1 += ptr1;
             p2 += ptr2;
             p3 += ptr3;

         }

     }

     input_img1.UnlockBits(bmpdata1);
     input_img2.UnlockBits(bmpdata2);
     output_image.UnlockBits(bmpoutput);

     return output_image;

 }


与往常一样,非常感谢您的评论和建议.非常感谢.


As always, Thank you so much for your comments and advices. I appreciate it a lot.


这篇关于通过图像减法的概念来拼接图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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