程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何更快地二值化图片大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何更快地二值化图片?

开发过程中遇到如何更快地二值化图片的问题如何解决?下面主要结合日常开发的经验,给出你关于如何更快地二值化图片的解决方法建议,希望对你解决如何更快地二值化图片有所启发或帮助;

我目前正在尝试对图像进行二值化,以下是我使用的代码:

@H_403_3@private static Bitmap PBinary(Bitmap src,int v)
{
    int w = src.WIDth;
    int h = src.Height;

    Bitmap dstBitmap = new Bitmap(src.WIDth,src.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    System.Drawing.Imaging.bitmaPDAta srcdata = src.LockBits(new Rectangle(0,w,h),System.Drawing.Imaging.ImageLockmode.Readonly,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    System.Drawing.Imaging.bitmaPDAta dstData = dstBitmap.LockBits(new Rectangle(0,System.Drawing.Imaging.ImageLockmode.writeonly,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    unsafe
    {
        byte* pIn = (byte*)srcdata.Scan0.topointer();
        byte* pOut = (byte*)dstData.Scan0.topointer();
        byte* p;
        int StriDe = srcdata.StriDe;
        int r,g,b;
        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                p = pIn;
                r = p[2];
                g = p[1];
                b = p[0];
                pOut[0] = pOut[1] = pOut[2] = (bytE)(((bytE)(0.2125 * r + 0.7154 * g + 0.0721 * b) >= v) ? 255 : 0);
                pIn += 3;
                pOut += 3;
            }
            pIn += srcdata.StriDe - w * 3;
            pOut += srcdata.StriDe - w * 3;
        }
        src.UnlockBits(srcdata);
        dstBitmap.UnlockBits(dstData);
        return dstBitmap;
    }
}

我可以成功将图片二值化,但是当我添加滑块动态调整参数时,我发现图片显示时很卡。

@H_403_3@    private voID BinarySlIDer_ValueChanged(object sender,RoutedPropertyChangedEventArgs<double> E)
    {
        if (originalimg!=null)
        {
            BinaryPara = (int)e.NewValue;

            PrevIEwimg = PBinary(originalimg,BinaryPara);

            this.dispatcher.Invoke(() =>
            {
                img.source = null;

                img.source = BitmapToBitmapsource(PrevIEwimg);

            });

        }

    }

    [System.Runtime.Interopservices.Dllimport("gdi32.dll")]
    public static extern bool deleteObject(IntPtr hObject);

    public static Bitmapsource BitmapToBitmapsource(Bitmap bitmap)
    {
        IntPtr hBitmap = bitmap.GetHbitmap();

        Bitmapsource retval;

        try
        {
            retval = Imaging.CreateBitmapsourceFromHBitmap(
                         hBitmap,IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
        }
        finally
        {
            deleteObject(hBitmap);
        }

        return retval;

    }

再次尝试使用opencvsharp,发现使用opencvsharp动态调整二值化的参数,画面显示非常流畅,没有卡顿。

但目前我不想使用第三方库。有什么方法可以更快地对图像进行二值化?

谢谢大家!

解决方法

这可能不是完全您所需要的,但它应该让您了解如何从相同的、未修改的原始像素缓冲区创建不同的二值化位图。

在第一步中,它从原始 Bitmapsource 创建一个每像素 8 位的灰度 Bitmapsource,并将其缓冲区复制到一个字节数组。缓冲区中的每个字节都是 0 到 255 范围内的灰度值。诀窍是将其作为索引重新使用到具有 256 个条目的调色板中。

现在每次阈值(即范围为 0 到 255 的滑块的值)更改时,都会创建一个包含 256 个条目的调色板,但它仅包含 Black@H_616_27@ 用于低于阈值的索引,并且 { {1}}对于上述索引。

White@H_616_27@

大佬总结

以上是大佬教程为你收集整理的如何更快地二值化图片全部内容,希望文章能够帮你解决如何更快地二值化图片所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: