Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#快速img合并_C#_Image Processing_Combiners - Fatal编程技术网

C#快速img合并

C#快速img合并,c#,image-processing,combiners,C#,Image Processing,Combiners,今天我想尝试一下C语言中图像处理的新方法 Szenario如下:我有两张黑白照片。现在我想从两幅图像中获取所有白色像素(x,y),并将它们放入返回图像中。最后,我的image3包含了image1和image2中的所有白色像素。 我使用不安全的指针,因为它们更快 因此,在代码中,我检查image1in(x,y)==image2in(x,y),因为两张图片不太可能在同一点上有一个白色像素 我现在的做法是: private unsafe static Bitmap combine_img(Bitmap

今天我想尝试一下C语言中图像处理的新方法

Szenario如下:我有两张黑白照片。现在我想从两幅图像中获取所有白色像素(x,y),并将它们放入返回图像中。最后,我的image3包含了image1和image2中的所有白色像素。 我使用不安全的指针,因为它们更快

因此,在代码中,我检查image1in(x,y)==image2in(x,y),因为两张图片不太可能在同一点上有一个白色像素

我现在的做法是:

private unsafe static Bitmap combine_img(Bitmap img1, Bitmap img2)
    {
        Bitmap retBitmap = img1;

        int width = img1.Width;
        int height = img1.Height;
        BitmapData image1 = retBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
        BitmapData image2 = img2.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); // here an error

        byte* scan1 = (byte*)image1.Scan0.ToPointer();
        int stride1 = image1.Stride;

        byte* scan2 = (byte*)image2.Scan0.ToPointer();
        int stride2 = image2.Stride;

        for (int y = 0; y < height; y++)
        {
            byte* row1 = scan1 + (y * stride1);
            byte* row2 = scan2 + (y * stride2);

            for (int x = 0; x < width; x++)
            {
                if (row1[x] == row2[x])
                    row1[x] = 255;
            }
        }

        img1.UnlockBits(image1);
        img2.UnlockBits(image2);

        return retBitmap;
    }
private不安全静态位图组合\u img(位图img1、位图img2)
{
位图retBitmap=img1;
int width=img1.宽度;
内部高度=img1.高度;
BitmapData image1=retBitmap.LockBits(新矩形(0,0,宽度,高度),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
BitmapData image2=img2.LockBits(新矩形(0,0,宽度,高度),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);//这里有一个错误
字节*scan1=(字节*)image1.Scan0.ToPointer();
int-stripe1=image1.stripe;
字节*scan2=(字节*)image2.Scan0.ToPointer();
int-stripe2=image2.stripe;
对于(int y=0;y

不幸的是,它在尝试锁定第二个图像时返回一个错误,表示该图像已被锁定

问题是,奇怪的是,我确实传递了相同的图像,这里是更正后的代码:

private unsafe static void combine_img(Bitmap img1, Bitmap img2)
    {
        BitmapData image1 = img1.LockBits(new Rectangle(0, 0, img1.Width, img1.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
        BitmapData image2 = img2.LockBits(new Rectangle(0, 0, img2.Width, img2.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

        int bytesPerPixel = 3;

        byte* scan1 = (byte*)image1.Scan0.ToPointer();
        int stride1 = image1.Stride;

        byte* scan2 = (byte*)image2.Scan0.ToPointer();
        int stride2 = image2.Stride;

        for (int y = 0; y < img1.Height; y++)
        {
            byte* row1 = scan1 + (y * stride1);
            byte* row2 = scan2 + (y * stride2);

            for (int x = 0; x < img1.Width; x++)
            {
                if (row2[x * bytesPerPixel] == 255)
                    row1[x * bytesPerPixel] = row1[x * bytesPerPixel - 1] = row1[x * bytesPerPixel-2] = 255;
            }
        }
        img1.UnlockBits(image1);
        img2.UnlockBits(image2);
    }
private不安全静态无效组合\u img(位图img1、位图img2)
{
BitmapData image1=img1.LockBits(新矩形(0,0,img1.Width,img1.Height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
BitmapData image2=img2.LockBits(新矩形(0,0,img2.Width,img2.Height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
int字节/像素=3;
字节*scan1=(字节*)image1.Scan0.ToPointer();
int-stripe1=image1.stripe;
字节*scan2=(字节*)image2.Scan0.ToPointer();
int-stripe2=image2.stripe;
对于(int y=0;y
问题是,奇怪的是,我确实传递了相同的图像,这里是更正后的代码:

private unsafe static void combine_img(Bitmap img1, Bitmap img2)
    {
        BitmapData image1 = img1.LockBits(new Rectangle(0, 0, img1.Width, img1.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
        BitmapData image2 = img2.LockBits(new Rectangle(0, 0, img2.Width, img2.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

        int bytesPerPixel = 3;

        byte* scan1 = (byte*)image1.Scan0.ToPointer();
        int stride1 = image1.Stride;

        byte* scan2 = (byte*)image2.Scan0.ToPointer();
        int stride2 = image2.Stride;

        for (int y = 0; y < img1.Height; y++)
        {
            byte* row1 = scan1 + (y * stride1);
            byte* row2 = scan2 + (y * stride2);

            for (int x = 0; x < img1.Width; x++)
            {
                if (row2[x * bytesPerPixel] == 255)
                    row1[x * bytesPerPixel] = row1[x * bytesPerPixel - 1] = row1[x * bytesPerPixel-2] = 255;
            }
        }
        img1.UnlockBits(image1);
        img2.UnlockBits(image2);
    }
private不安全静态无效组合\u img(位图img1、位图img2)
{
BitmapData image1=img1.LockBits(新矩形(0,0,img1.Width,img1.Height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
BitmapData image2=img2.LockBits(新矩形(0,0,img2.Width,img2.Height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
int字节/像素=3;
字节*scan1=(字节*)image1.Scan0.ToPointer();
int-stripe1=image1.stripe;
字节*scan2=(字节*)image2.Scan0.ToPointer();
int-stripe2=image2.stripe;
对于(int y=0;y
我猜您的第二个图像不是24位图像。Mayber尝试以下方法:

BitmapData image2 = img2.LockBits(new Rectangle(0, 0, img2.Width, img2.Height), ImageLockMode.ReadWrite, img2.PixelFormat);

在这种情况下,您将始终通过这一行(我假设),但问题是您不知道您实际上正在处理24位图像或32位图像。

我猜您的第二个图像不是24位图像。Mayber尝试以下方法:

BitmapData image2 = img2.LockBits(new Rectangle(0, 0, img2.Width, img2.Height), ImageLockMode.ReadWrite, img2.PixelFormat);

在这种情况下,您将始终通过这一行(我假设),但问题是您不知道您实际上正在处理24位图像或32位图像。

如果您在两个参数上都通过相同的图像,这也就不足为奇了。即使不是这样,您也应该添加验证图像是否相同的代码,如果它们正确处理该情况;否则照常继续。只需交换两个LockBits语句。如果它现在抱怨retBitmap被锁定,那么它们确实引用了相同的位图数据。如果没有,则它被锁定在其他位置。UnlockBits()属于finally{block}。如果在两个参数上传递相同的图像,这并不奇怪。即使不是这样,您也应该添加验证图像是否相同的代码,如果它们正确处理该情况;否则照常继续。只需交换两个LockBits语句。如果它现在抱怨retBitmap被锁定,那么它们确实引用了相同的位图数据。如果没有,则它被锁定在其他位置。UnlockBits()属于finally{block}。