V
V
vad1mi2015-12-20 16:17:52
Programming
vad1mi, 2015-12-20 16:17:52

How to quickly iterate through all the pixels of a large number of images?

There are 150-200 images with a resolution of 10,000x10,000 pixels.
From them it is necessary to build something like a photoplan. I decided to fold this photo plan from pixels, so I need to get all non-black pixels from all images. I tried to implement a lot of threading through the Parallel class, there is no big acceleration in processing time, what can be done?
Call:

var progress = new Progress<string>(s => richTextBox1.AppendText(s + Environment.NewLine));
                string way = string.Format(@fbD.SelectedPath);
                string searchPattern = string.Format(@"*.tif");
                string[] Names = System.IO.Directory.GetFiles(way, searchPattern);
                //int count = 0;
                DateTime start = DateTime.Now;
                textBox2.Text += "FilesCount = " + Names.Length + Environment.NewLine;
                await Task.Factory.StartNew(
                                            () => Worker.Read(progress,Names),
                                            TaskCreationOptions.None);
                DateTime end = DateTime.Now;
                MessageBox.Show(string.Format(" Time work[ {0}:{1} ]", (end - start).Minutes, (end - start).Seconds));

Implementation:
{
           //Parallel.For(0, Names.Length, i =>
           for(int i =0;i<Names.Length;i=i+10)
                {
                    if (i + 10 < Names.Length)
                    {
                        Parallel.ForEach(Enumerable.Range(i, 10), j =>
                        {
                            byte[, ,] Im = Form1.BitmapToByteRgb(new Bitmap(Names[j]));
                            progress.Report(j.ToString());
                        });
                    }
                    else
                    {
                        Parallel.ForEach(Enumerable.Range(i, Names.Length), j =>
                        {
                            byte[, ,] Im = Form1.BitmapToByteRgb(new Bitmap(Names[j]));
                            progress.Report(j.ToString());
                        });
                    }
                }
        }
static unsafe public byte[, ,] BitmapToByteRgb(Bitmap bmp)
        {
            int width = bmp.Width,
                height = bmp.Height;
            byte[, ,] res = new byte[3, height, width];
            BitmapData bd = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly,
                PixelFormat.Format24bppRgb);
            try
            {
                byte* curpos;
                for (int h = 0; h < height; h++)
                {
                    curpos = ((byte*)bd.Scan0) + h * bd.Stride;
                    for (int w = 0; w < width; w++)
                    {
                        res[2, h, w] = *(curpos++);
                        res[1, h, w] = *(curpos++);
                        res[0, h, w] = *(curpos++);
                    }
                }
            }
            finally
            {
                bmp.UnlockBits(bd);
            }
            return res;
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question