marshalling - Marshal.Copy 导致暗恋
问题描述
我想在一个线程中翻转一张“tiff”的图片,但使用“Marshal.Copy”时可能会导致粉碎
我的代码:
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;
}
}
params:image是一个arrary int[2048*2048],高宽是2048,mirrorNum是flip的类型。通常是1,出现概率大约0.01%
事件查看器显示:
System.AccessViolationException at System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr, System.Object, Int32, Int32) 在 Utility.Function.ImageMirrorCv(Int32[] ByRef, Int32, Int32, Int32) 在 DataInterface.ImageProcess.Mirror(System .Object) 在 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 在 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback , System.Object, Boolean) 在 System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 在 System.Threading.ThreadPoolWorkQueue.Dispatch()
解决方案
推荐阅读
- javascript - 协议指定 void 参数类型时未调用 gRPC node-js 方法
- python - CPython 在执行标记扫描算法后如何知道哪些对象没有被标记?
- flutter - 如何在颤动中删除选项卡之间的额外空间?
- swift - 如何防止 NSToolbar 重叠 NSViewController 内容?
- botframework - Teams 客户端更新 - 访问此应用时出现问题
- google-sheets - Google 文档遇到错误。请尝试重新加载此页面,或在几分钟后返回此页面
- sql - Oracle plsql检查两侧之间的日期
- google-cloud-platform - 运行terraform申请GCP时连接被拒绝
- javascript - 保存本地存储 JavaScript 检查
- python - 如何部署训练有素的 OpenAI 健身房模型以将股票交易作为应用程序或部署到 backtrader 等回测框架中以预测买入或卖出?