简体   繁体   English

读取多个线程中的图像

[英]Reading images in multiple threads

I have some C# code for reading images from disk and then crop them(optionally resize) and then save them. 我有一些C#代码,用于从磁盘读取图像,然后裁剪它们(可选地调整大小),然后保存它们。 But it looks like only 30% of CPU is used during operation, can I use multithreading to improve speed or disk read speed is bottleneck? 但是看来运行期间只使用了30%的CPU,我可以使用多线程来提高速度还是磁盘读取速度成为瓶颈? How to properly load cpu and disk for max performance? 如何正确加载cpu和磁盘以获得最佳性能?

    string path = args[0];

    int counter = 0;
    foreach (string dir in Directory.GetDirectories(path))
    {
        Console.WriteLine("processing: " + dir);

        //create folder for faces
        string dir_path = dir + "\\face";
        System.IO.Directory.CreateDirectory(dir_path);

        try
        {
            string ini_path = dir + "\\.picasa.ini";
            if (File.Exists(ini_path))
            {
                FileIniDataParser parser = new FileIniDataParser();
                IniData data = parser.LoadFile(ini_path);
                foreach (SectionData section in data.Sections)
                {
                    if (section.SectionName.Contains(".jpg"))
                    {
                        string rects = data[section.SectionName]["faces"];

                        string[] str_rects = GetRectStrings(rects);

                        for (int i = 0; i < str_rects.Length; ++i)
                        {
                            Bitmap img = (Bitmap)Image.FromFile(dir + "\\" + section.SectionName, true);

                            RectangleF rectF = GetRectangle(str_rects[i]);

                            int im_w = img.Width;
                            int im_h = img.Height;

                            rectF.X = rectF.X * im_w;
                            rectF.Y = rectF.Y * im_h;
                            rectF.Width = rectF.Width * im_w;
                            rectF.Height = rectF.Height * im_h;

                            Bitmap bmpCrop = img.Clone(rectF, img.PixelFormat);

                            string text_path = Directory.GetParent(path).FullName + "\\db.txt";
                            string crop_path = dir + "\\face\\" +
                                Path.GetFileNameWithoutExtension(dir + "\\" + section.SectionName) + "_" + i.ToString() + "_crop.png";

                            bool resize = true;
                            if (resize)
                            {
                                Bitmap resized = new Bitmap(bmpCrop, new Size(24, 32));//вынести в параметры
                                resized.Save(crop_path,
                                    System.Drawing.Imaging.ImageFormat.Png);

                                Bitmap gr = ConvertGray(resized);

                                AppendToTxtFile(gr, text_path);
                            }
                            else
                            {
                                bmpCrop.Save(crop_path,
                                    System.Drawing.Imaging.ImageFormat.Png);

                                Bitmap gr = ConvertGray(bmpCrop);

                                AppendToTxtFile(gr, text_path);
                            }

                            counter++;
                        }
                    }
                }
            }

        }
        catch
        {
            Console.WriteLine("problem in: " + dir);
        }

        Console.WriteLine("rects: " + counter.ToString());
    }

    Console.WriteLine("all done");
    Console.ReadLine();

You have both the elements of reading from disk, which is probably consuming some time depending on what your disk quality is, but you are still limited by IO work in general and doing CPU bound work of resizing the images. 您同时具有从磁盘读取的两个元素,这可能会花费一些时间,具体取决于磁盘的质量,但是总体上,您仍然受到IO工作和调整图像大小的CPU约束工作的限制。

If you're bound to .NET 3.5, i'd suggest using FileStreams.BeginRead (which uses the older asynchronous pattern) for loading the images asynchrnously and then firing up background workers to take care of resizing your image to maximize CPU usage. 如果您绑定到.NET 3.5,则建议使用FileStreams.BeginRead(使用旧的异步模式)异步加载图像,然后启动后台工作程序以调整图像大小,以最大程度地利用CPU。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM