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# 嵌套并行中的同步。For循环_C#_Image Processing_Parallel Processing_Task Parallel Library - Fatal编程技术网

C# 嵌套并行中的同步。For循环

C# 嵌套并行中的同步。For循环,c#,image-processing,parallel-processing,task-parallel-library,C#,Image Processing,Parallel Processing,Task Parallel Library,我想把一个普通的for循环变成一个并行的for循环 这个- 它以消息形式失败- 对象当前正在其他地方使用 由于多个线程试图访问非线程安全的资源,因此位于第行下方。你知道我该怎么做吗 System.Drawing.Color oc = bitmapImage.GetPixel(i, x); 这不是一个干净的解决方案,看看你想实现什么。最好在一次拍摄中获得所有像素,然后并行处理这些像素,以获得最佳效果 我个人使用的另一种方法是使用不安全的函数来输出灰度图像,这一方法大大提高了性能 public s

我想把一个普通的for循环变成一个并行的for循环

这个-

它以消息形式失败-

对象当前正在其他地方使用

由于多个线程试图访问非线程安全的资源,因此位于第行下方。你知道我该怎么做吗

System.Drawing.Color oc = bitmapImage.GetPixel(i, x);

这不是一个干净的解决方案,看看你想实现什么。最好在一次拍摄中获得所有像素,然后并行处理这些像素,以获得最佳效果

我个人使用的另一种方法是使用不安全的函数来输出灰度图像,这一方法大大提高了性能

public static byte[] MakeGrayScaleRev(byte[] source, ref Bitmap bmp,int Hei,int Wid)
        {            
            int bytesPerPixel = 4;   

            byte[] bytesBig = new byte[Wid * Hei]; //create array to contain bitmap data with padding

            unsafe
            {

                int ic = 0, oc = 0, x = 0;
                //Convert the pixel to it's luminance using the formula:
                // L = .299*R + .587*G + .114*B
                //Note that ic is the input column and oc is the output column                  
                for (int ind = 0, i = 0; ind < 4 * Hei * Wid; ind += 4, i++)
                {                        
                    int g = (int)
                            ((source[ind] / 255.0f) *
                            (0.301f * source[ind + 1] +
                             0.587f * source[ind + 2] +
                            0.114f * source[ind + 3]));
                    bytesBig[i] = (byte)g;
                }    
            }

            try
            {

                bmp = new Bitmap(Wid, Hei, PixelFormat.Format8bppIndexed);

                bmp.Palette = GetGrayScalePalette();

                Rectangle dimension = new Rectangle(0, 0, Wid, Hei);
                BitmapData picData = bmp.LockBits(dimension, ImageLockMode.ReadWrite, bmp.PixelFormat);


                IntPtr pixelStartAddress = picData.Scan0;

                Marshal.Copy(forpictures, 0, pixelStartAddress, forpictures.Length);

                bmp.UnlockBits(picData);

                return bytesBig;

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);

                return null;

            }

        }
public static byte[]MakeGrayScaleRev(byte[]源,ref位图bmp,int Hei,int Wid)
{            
int字节/像素=4;
byte[]bytesBig=新字节[Wid*Hei];//创建数组以包含带填充的位图数据
不安全的
{
int ic=0,oc=0,x=0;
//使用以下公式将像素转换为其亮度:
//L=.299*R+.587*G+.114*B
//请注意,ic是输入列,oc是输出列
对于(int ind=0,i=0;ind<4*Hei*Wid;ind+=4,i++)
{                        
int g=(int)
((源[ind]/255.0f)*
(0.301f*源[ind+1]+
0.587f*源[ind+2]+
0.114f*源[ind+3]);
bytesBig[i]=(字节)g;
}    
}
尝试
{
bmp=新位图(Wid、Hei、PixelFormat.Format8bppIndexed);
bmp.palete=GetGrayScalePalette();
矩形尺寸=新矩形(0,0,宽,高);
BitmapData picData=bmp.LockBits(维度,ImageLockMode.ReadWrite,bmp.PixelFormat);
IntPtr pixelStartAddress=picData.Scan0;
Marshal.Copy(用于图片,0,像素StartAddress,用于图片,长度);
bmp.UnlockBits(picData);
返回bytesBig;
}
捕获(例外情况除外)
{
控制台写入线(例如StackTrace);
返回null;
}
}

它获取输入图像所有像素的字节数组、高度和宽度,并输出计算的灰度数组,在ref Bitmap bmp中获取输出灰度位图。

您不能同时读取或更改资源。您的第一个版本是唯一可以工作的版本,添加锁会增加开销并使其比第一个版本慢。@Igor谢谢。我也这么认为。因为图像是GUI相关的类,所以它是为单线程访问使用而制作的。您可以尝试在一个独立的矩阵上进行计算,然后在单for循环中更新图像。+1谢谢。这可能对某人有帮助,但由于其他原因,我现在可能不会使用这个。如果您需要任何帮助,请告诉我。该代码用于从RTSP摄像机获取流并将其转换为灰度,因此性能至关重要。
System.Drawing.Color oc = bitmapImage.GetPixel(i, x);
public static byte[] MakeGrayScaleRev(byte[] source, ref Bitmap bmp,int Hei,int Wid)
        {            
            int bytesPerPixel = 4;   

            byte[] bytesBig = new byte[Wid * Hei]; //create array to contain bitmap data with padding

            unsafe
            {

                int ic = 0, oc = 0, x = 0;
                //Convert the pixel to it's luminance using the formula:
                // L = .299*R + .587*G + .114*B
                //Note that ic is the input column and oc is the output column                  
                for (int ind = 0, i = 0; ind < 4 * Hei * Wid; ind += 4, i++)
                {                        
                    int g = (int)
                            ((source[ind] / 255.0f) *
                            (0.301f * source[ind + 1] +
                             0.587f * source[ind + 2] +
                            0.114f * source[ind + 3]));
                    bytesBig[i] = (byte)g;
                }    
            }

            try
            {

                bmp = new Bitmap(Wid, Hei, PixelFormat.Format8bppIndexed);

                bmp.Palette = GetGrayScalePalette();

                Rectangle dimension = new Rectangle(0, 0, Wid, Hei);
                BitmapData picData = bmp.LockBits(dimension, ImageLockMode.ReadWrite, bmp.PixelFormat);


                IntPtr pixelStartAddress = picData.Scan0;

                Marshal.Copy(forpictures, 0, pixelStartAddress, forpictures.Length);

                bmp.UnlockBits(picData);

                return bytesBig;

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);

                return null;

            }

        }