当前位置: 首页 > 编程语言 > C#

EmguCv.CV.CvInvoke引发的EmguCvTypeInitializationException分享

时间:2023-04-10 10:47:47 C#

EmguCv.CV.CvInvoke引发的EmguCvTypeInitializationException现在,仔细听,因为非常奇怪的部分来了。我首先要说我的问题有三个“级别”,但是,所有“级别”中的代码完全相同,即使没有任何变化。这自然会指向引用或链接问题,但我再次尝试使用不同的教程无济于事。级别1(此级别产生TypeInitializationException)我创建一个新项目,正确引用所有内容等,然后在这个新项目中编写我的代码。调试时,我抛出异常并且我的程序退出。这是问题图片的链接:http://prntscr.com/uychc第2级(这个级别工作得很好,不会抛出任何异常)在这个级别,我几乎找到了EmguCv的示例项目(在这种情况下在VideoSurveilance中),然后删除默认代码并将我的所有代码复制并粘贴到那里。添加我需要的更多参考后,程序运行良好。我不能发布超过3个链接,但你必须相信我,视频图像显示正确。第3级(这个级别不会抛出异常,但会警告我“第一次机会”)在这个级别,我将整个第2级项目复制并粘贴到另一个目录中。找到并重新链接丢失的文件/引用后,我能够运行该程序,但图片没有显示,我收到“Emgu.CV.dll警告中出现类型为“System.TypeInitializationException”的第一次机会异常”.http://prntscr.com/uycmn我目前正在运行Windows7x64(是的,我将构建选项更改为x64和x64.dll)并运行EmguCv2.4.9和2.4.2(在两者上测试)和VisualStudios2010和2012(两者都经过测试)。这是可能值得的代码:usingSystem;使用System.Collections.Generic;使用System.ComponentModel;使用系统数据;使用系统绘图;使用System.Linq;使用系统文本;//使用System.Threading.Tasks;使用System.Windows.Forms;使用System.Windows;使用System.Windows.Controls;使用System.Windows.Data;使用System.Windows.Documents;使用System.Windows.Input;使用System.Windows.Media;使用System.Windows.Media.Imaging;使用System.Windows.Navigation;使用System.Windows.Shapes;使用Microsoft.Kinect;使用Emgu.CV;使用Emgu.CV.CvEnum;使用Emgu.CV.Structure;使用Emgu.CV.UI;使用System.IO;namespaceVideoSurveilance{publicpartialclassVideoSurveilance:Form{KinectSensor传感器;可写位图深度位图;可写位图颜色位图;DepthImagePixel[]depthPixels;byte[]颜色像素;intblobCount=0;publicVideoSurveilance(){InitializeComponent();}privatevoidVideoSurveilance_Load(objectsender,System.EventArgse){foreach(varpotentialSensorinKinectSensor.KinectSensors){if(potentialSensor.Status==KinectStatus.Connected){this.sensor=potentialSensor;休息;}}if(null!=this.sensor){this.sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);this.colorPixels=newbyte[this.sensor.ColorStream.FramePixelDataLength];this.depthPixels=newDepthImagePixel[this.sensor.DepthStream.FramePixelDataLength];this.colorBitmap=newWriteableBitmap(this.sensor.ColorStream.FrameWidth,this.sensor.ColorStream.FrameHeight,96.0,96.0,PixelFormats.Bgr32,null);this.depthBitmap=newWriteableBitmap(this.sensor.DepthStream.FrameWidth,this.sensor.DepthStream.FrameHeight,96.0,96.0,PixelFormats.Bgr32,null);WriteableBitmap位图;bitmap=newWriteableBitmap(this.sensor.DepthStream.FrameWidth,this.sensor.DepthStream.FrameHeight,96.0,96.0,PixelFormats.Bgr32,无效);byte[]retVal=newbyte[bitmap.PixelWidth*bitmap.PixelHeight*4];bitmap.CopyPixels(newInt32Rect(0,0,bitmap.PixelWidth,bitmap.PixelHeight),retVal,bitmap.PixelWidth*4,0);位图b=newBitmap(bitmap.PixelWidth,bitmap.PixelHeight);诠释k=0;字节红色、绿色、蓝色、alpha;for(inti=0;iDepthImageFrame.PlayerIndexBitmaskWidth;//将距离映射到可以用RGB表示的强度varintensity=CalculateIntensityFromDistance(depth);if(depth>min&&depth0)return(byte)(255-(255*newMax/(MaxDepthDistanceOffset)));elsereturn(byte)255;}publicstaticSystem.Drawing.BitmapToBitmap(thisBitmapSourcebitmapsource){System.Drawing.Bitmap位图;使用(varoutStream=newMemoryStream()){//从System.Media.BitmapImage到System.Drawing.BitmapBitmapEncoderenc=newBmpBitmapEncoder();enc.Frames.Add(BitmapFrame.Create(bitmapsource));enc.保存(outStream);位图=newSystem.Drawing.Bitmap(outStream);返回位图;}}[DllImport(“gdi32”)]privatestaticexternintDeleteObject(IntPtro);//////将IImage转换为WPFBitmapSource。结果可以在Image.Source的Set属性中使用//////TheEmguCVImage///等效的BitmapSourcepublicstaticBitmapSourceToBitmapSource(IImageimage){using(System.Drawing.Bitmapsource=image.Bitmap){IntPtrptr=source.GetHbitmap();//获取HbitmapBitmapSourcebs=System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ptr,IntPtr.Zero,Int32Rect.Empty,System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());删除对象(指针);//释放HBitmapreturnbs;我真诚地感谢那些甚至试图帮助我的人,并希望任何有类似问题的人都能从找到解决这个长期存在的问题的“蛮力”解决方案中受益:你必须手动放置“cvextern.dll”文件进入您的构建目录(x64、调试、发布或其他)。做过。我不敢相信我花了一整天的时间试图解决这个问题。正如Oliver所说,您的问题非常类似于:EmguCVTypeInitializationExceptionExceptionthrowedduetoinabilitytoaccesscertaindllsasopencv_highgui220.dll。在CvInvoke中你会发现DLLImports,也许你应该插入一个断点并观察它来自哪里。我想您已经从emgu网站上阅读了这篇文章。以上就是C#学习教程:EmguCvTypeInitializationExceptioncausedbyEmguCv.CV.CvInvoke分享的全部内容,如果对大家有用,需要详细了解C#学习教程,希望大家多多关注—本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢