![](/img/trans.png)
[英]System.AccessViolationException while construct Mat using EmguCV
[英]System.AccessViolationException on VideoCapture.Retrieve() - EmguCV / OpenCV
我是剛打開簡歷的新人,我正在嘗試使用面部識別功能,但有一次遇到麻煩。 在以下三種情況之一中,在VideoCapture-Object上調用Retrieve()-方法會引發System.AccessViolationException。 我發現了有關此問題的許多主題,但沒有解決方案。
這是我得到的StackTrace:
bei Emgu.CV.CvInvoke.cveVideoCaptureRetrieve(IntPtr捕獲,IntPtr圖像,Int32標志)
bei Emgu.CV.VideoCapture.Retrieve(IOutputArray圖像,Int32通道)
bei OpenCVGenericAssembly.OpenCVGenericAssembly.Capture(字符串sensorSerialNo,FeatureType功能,布爾壓縮,Int32超時)在C:\\ Users \\ sl \\ Documents \\ Source \\ Share \\ OpenCVGenericAssembly \\ OpenCVGenericAssembly \\ OpenCVGenericAssembly.cs:Ze
bei OpenCVGenericAssembly.OpenCVGenericAssembly.Enroll(String sensorSerialNo,字符串firstName,字符串lastName,字符串公司,FeatureType功能,Int32 templateDestination,布爾壓縮,Int32超時,字符串connectionString,字符串templateQuery)在C:\\ Users \\ sl \\ Documents \\ Source \\ Share \\ OpenCVGenericAssembly \\ OpenCVGenericAssembly \\ OpenCVGenericAssembly.cs:Zeile 125。
bei Testing.Program.Main(String [] args)在C:\\ Users \\ sl \\ Documents \\ Source \\ Share \\ OpenCVGenericAssembly \\ Testing \\ Program.cs:Zeile 20中。
我正在調用一個Enroll方法,該方法除了調用Capture-Method並等待其響應外,什么也不做。 捕獲方法將運行,直到准確檢測到一張臉為止,然后將其返回。 這就是Capture-Method的樣子:
public DResponse Capture(string sensorSerialNo, FeatureType feature, bool compressed = false, int timeOut = 0)
{
capture = new VideoCapture();
DResponse rsp = DResponse();
while(string.IsNullOrWhiteSpace(rsp.templateData))
{
using (Mat mat = new Mat())
{
capture.Retrieve(mat);
Image<Bgr, Byte> currentFrame = mat.ToImage<Bgr, Byte>();
if (currentFrame != null)
{
Image<Gray, Byte> grayFrame = currentFrame.Convert<Gray, Byte>();
Rectangle[] detectedFaces = cascadeClassifier.DetectMultiScale(grayFrame, DMS_SCALE_FACTORS, DMS_MIN_NEIGHBORS);
if (detectedFaces.Length == 1)
{
Image<Gray, byte> result = currentFrame.Copy(detectedFaces[0]).Convert<Gray, byte>().Resize(IMG_WIDTH, IMG_HEIGHT, Emgu.CV.CvEnum.Inter.Cubic);
result._EqualizeHist();
rsp.templateData = Convert.ToBase64String(result.Bytes);
break;
}
Thread.Sleep(100);
}
}
}
return rsp;
}
我首先嘗試了一個教程。 這是一個wpf應用程序,顯示視頻流和檢測到的面部周圍的框架(如果識別出人,還加上名稱)。 這非常相似,但是他們在本教程中使用了DispatcherTimer,我不能使用它,因為我的代碼應作為程序集使用。 無論如何,此代碼不會引發此錯誤,因此也許這將有助於某人在上面的源中捕獲問題。
private void Window_Loaded(object sender, RoutedEventArgs e)
{
capture = new VideoCapture();
haarCascade = new CascadeClassifier(System.AppDomain.CurrentDomain.BaseDirectory + "haarcascade_frontalface_alt_tree.xml");
timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
Mat mat = new Mat();
capture.Retrieve(mat);
Image<Bgr, Byte> currentFrame = mat.ToImage<Bgr, Byte>();
if (currentFrame != null)
{
Image<Gray, Byte> grayFrame = currentFrame.Convert<Gray, Byte>();
Rectangle[] detectedFaces = haarCascade.DetectMultiScale(grayFrame, 1.1, 1);
for (int i = 0; i < detectedFaces.Length; i++)
{
result = currentFrame.Copy(detectedFaces[i]).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic);
result._EqualizeHist();
currentFrame.Draw(detectedFaces[i], new Bgr(System.Drawing.Color.Green), 3);
if (eigenRecog.IsTrained)
{
// do some stuff
}
}
image1.Source = ToBitmapSource(currentFrame);
}
}
有什么提示嗎? 任何問題? 感謝您的每一次輸入! STL
必須捕獲VideoCapture-Object(手動或將其放入using塊中)。 那解決了問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.