C#螺纹图像处理 [英] C# Threaded image processing

查看:173
本文介绍了C#螺纹图像处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

                    for (int x = 0; x < blockCountX; x++)
                    {
                        for (int y = 0; y < blockCountY; y++)
                        {
                            //get blocks from image to new image and send to threaded processor
                            imageBlocks[x, y] = image.Clone(clipRectangle, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ThreadedFromHeightMap));
                            t.Start(imageBlocks[x,y]);
                            clipRectangle.Offset(0, IMAGEBLOCKSIZE);
                        }
                        clipRectangle.Offset(IMAGEBLOCKSIZE, clipRectangle.Location.Y * -1);
                    }
                    break;
            }
        }
        private void ThreadedFromHeightMap(object Image)
        {
            Bitmap image = (Bitmap)Image;
            int width = image.Width;
            int height = image.Height;

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    map.Hexes.Add(new Point(x, y), new Hex(image.GetPixel(x, y).B));
                    //tempHexes.Enqueue(new Hex(image.GetPixel(x, y).B));
                }
            }
        }



我想取从2048×2048 8bpp灰度高度图,建设一个十六进制地图相应的高度值的像素数据。我存储在不吉利的东西字典集合。总而言之有〜400万不吉利的东西收藏。

I'm trying to take pixel data from a 2048 x 2048 8bpp grayscale heightmap and building a hex map with corresponding height values. I'm storing the hexes in a Dictionary collection. All told there's ~4 million hexes in the collection.

要做到这一点有效,我打破了图像成256×256块和图像传递到另一个线程其将解析它和不吉利的东西添加到集合。这是这一切都失控。相反,用(0,0)一个图像作为其左上角的,我现在已经有(0,0)64块图像作为其左上角。不过我使用的像素位置作为词典的索引。这崩溃当第二个线程试图增加与指数(0,0)的另一个值。

To do this efficiently I'm breaking the image up into 256 x 256 chunks and passing that image to another thread which will parse it and add the hexes to the collection. This is were it all goes haywire. Instead of one image with (0,0) as its top left corner, I now have 64 image chunks that have (0,0) as their top left corner. However I'm using the pixel location as an index for the dictionary. This crashes when the second thread tries to add another value with index (0,0).

我如何缓解这一问题?我想过建设一个只是有一个图像部件和块号成员,并传递给线程,这样我可以调整取决于什么块线程工作的像素位置,但似乎达不到最佳的类。

How do I mitigate this problem? I've thought about building a class that just has an image member and a chunk number member and pass that to the thread so I can adjust the pixel location depending on what chunk the thread is working on but that seems less than optimal.

(我知道我使用的是不是线程安全的字典。因为我已经固定的。)

(I realize the Dictionary I'm using isn't thread safe. I've since fixed that.)

推荐答案

我可以推荐几件事?


  1. 忘记image.GetPixel() ,这是可怕的慢;直接与位图数据的工作,你的算法的性能会被如此多的改进,你不会需要运行并行线程,以提高其效率。请参阅MSDN: http://msdn.microsoft.com/ EN-US /库/ system.drawing.imaging.bitmapdata.aspx

如果你坚持并行线程,使用线程池的,而不是产卵64个线程。 (参见MSDN: http://msdn.microsoft。 COM / EN-US /库/ 3dasc8as(v = VS.80)的.aspx

If you insist on parallel threads, make use of the threadpool, instead of spawning 64 threads. (See MSDN: http://msdn.microsoft.com/en-us/library/3dasc8as(v=vs.80).aspx)

如果你坚持产卵多线程,做不生成比你的CPU的内核更多的线程。我不认为你有你的机器上的64个内核,你呢?​​

If you insist on spawning many threads, do NOT spawn more threads than the cores of your CPU. I do not suppose you have 64 cores on your machine, do you?

如果你坚持产卵多线程,你会的,当然,需要通过每瓦线,位置,让你确切地知道那瓦应放在当你重建大局。这不是比最佳少,这是必要的。

If you insist on spawning many threads, you will, of course, need to pass the location of each tile to the thread, so that you know exactly where that tile should be placed when you reconstruct the big picture. That's not less than optimal, it is necessary.

这篇关于C#螺纹图像处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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