我取了多张tiff图像,读到内存中为int[] 数组,而后通过线程池控制每张图像 尽心翻转操作。翻转使用的opencv,目前有一定的概率,在执行Marshal.Copy的时候,崩溃,使用try catch完全捕获不到,进程直接退出了。代码如下:
public static bool ImageMirrorCv(ref int[] image, int width, int height, int mirrorNum)
{
try
{
//以X轴,垂直翻转
if (mirrorNum == 0)
{
Mat mt = new Mat(height, width, MatType.CV_32F);
if ((mt.Height * mt.Width) != image.Length)
return false;
Marshal.Copy(image, 0, mt.Data, image.Count());
Cv2.Flip(mt, mt, FlipMode.X);
if ((mt.Height * mt.Width) != image.Length)
return false;
Marshal.Copy(mt.Data, image, 0, image.Count());
}
//以Y轴,水平翻转
else if (mirrorNum == 1)
{
Mat mt = new Mat(height, width, MatType.CV_32F);
if ((mt.Height * mt.Width) != image.Length)
return false;
Marshal.Copy(image, 0, mt.Data, image.Count());
Cv2.Flip(mt, mt, FlipMode.Y);
if ((mt.Height * mt.Width) != image.Length)
return false;
Marshal.Copy(mt.Data, image, 0, image.Count());
}
//XY翻转
else if (mirrorNum == -1)
{
Mat mt = new Mat(height, width, MatType.CV_32F);
if ((mt.Height * mt.Width) != image.Length)
return false;
Marshal.Copy(image, 0, mt.Data, image.Count());
Cv2.Flip(mt, mt, FlipMode.XY);
if ((mt.Height * mt.Width) != image.Length)
return false;
Marshal.Copy(mt.Data, image, 0, image.Count());
}
else
{
}
return true;
}
catch(Exception ex)
{
Utility.LogControl.Error(ex.ToString());
return false;
}
}
其中,输入输出,均为image,其为2048*2048的数据,height和width均为2048,mirrorNum控制翻转方向
报错后,系统事件查看器中显示:
目前使用频率大概在一天执行几万次,报错频率大概在一个月一次。
望大神不吝赐教