Click here to Skip to main content
16,016,925 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have the code that works fine for the x64 bit OS while x32bit OS generates exception : "The type initializer for 'Emgu.CV.CvInvoke' threw an exception."

i have followed almost solutions from the internet still didn't get rid of the problem.

i have tried to put unmanaged dlls to the execution file directory.
i have build in target framework x86.
i have also tried the dlls to put into c:\windows\system32.

almost every solutions.
also tested demo projects still getting same problems.
it has run perfect on the x64bit OS but gives runtime exception "The type initializer for 'Emgu.CV.CvInvoke' threw an exception.<"

What I have tried:

i am using emgucv library to do some image processing. i have earlier used emgu cv library but never encountered this error. almost tried every solutions from the internet solutions and sources.
here is my sample code from the demo project.

C#
private void Application_Idle(object sender, EventArgs e)
        {
            String FileName = @"E:\Demo\EmguCv-Histogram-01-master\S\ResizedImages\dblbig.jpg";
            Image<Bgr, Byte> InputImage;
            InputImage = new Image<Bgr, byte>(FileName);
            mainImage = InputImage; //webcamCapture.QueryFrame().ToImage<Bgr, Byte>();
            if (mainImage == null) return;
            processedImage = mainImage.InRange(new Bgr(120, 0, 0), new Bgr(256, 70, 70));
            processedImage = processedImage.SmoothGaussian(9);
            CircleF[] circles = processedImage.HoughCircles(new Gray(100), new Gray(50), 2, processedImage.Height / 4, 10, 400).SelectMany(u => u.Select(s => s)).ToArray();
            foreach (var c in circles)
            {
                if (txtLog.Text != "") txtLog.AppendText(Environment.NewLine);


                CvInvoke.Circle(mainImage, new Point((int)c.Center.X, (int)c.Center.Y), 3, new MCvScalar(0, 255, 0), -1, Emgu.CV.CvEnum.LineType.AntiAlias, 0);
                mainImage.Draw(c, new Bgr(Color.Red), 3);
            }
            imageBox1.Image = mainImage;
            imageBox2.Image = processedImage;

        }


Update 1

Update : i don't know why but when i deleted Emgu.CV library from nuget pkg and added dlls from the reference
1.Emgu.CV
2.Emgu.Util
3.Emgu.CV.UI

i was working.

but now the problem is i was using nuget pkg Emgu.CV library of version 3.3.0.2824 it was using Emgu.cv.World.dll and now i am using these three dlls from reference and it uses Emgu.CV.dll so the functions have been changed.
i was using :
C#
var rect = CvInvoke.MinAreaRect(points);

now its giving me error because it not in reference dll
so now which function i can use instead of the :
C#
var rect = CvInvoke.MinAreaRect(points);

i am updating my question and posting the code where i want to change the function.


here is my some of my source code:

C#
private LineSegment2D[] objectLines = new LineSegment2D[0];
int lineCount = 0;


foreach (var line in objectLines)
{


    var points = new PointF[] { line.P1, line.P2 };
    var rect = CvInvoke.MinAreaRect(points);  //here i am getting compile time error and now want to change my function

    angle = rect.Angle;
    var center = rect.Center;

    startPoint = new Point(Convert.ToInt32(center.X - 100 * Math.Cos(angle * Math.PI / 180.0)),
                           Convert.ToInt32(center.Y - 100 * Math.Sin(angle * Math.PI / 180.0)));

    endPoint = new Point(Convert.ToInt32(center.X + 100 * Math.Cos(angle * Math.PI / 180.0)),
                         Convert.ToInt32(center.Y + 100 * Math.Sin(angle * Math.PI / 180.0)));

    if (lineCount == 0 && isTopLineClicked && upbuttobClick)
    {
        Display_diamond_checkbox.Checked = false;
        Thread.Sleep(1000);
        this.ob.DrawLine(Color.Black, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
        double distanceToTopLine = CalculateDistance(startPoint.X, startPoint.Y, x21, y21, x22, y22);
        double distanceToBottomLine = CalculateDistance(startPoint.X, startPoint.Y, x19, y19, x20, y20);
        double distanceThreshold = 3;

        if (distanceToTopLine <= distanceThreshold || distanceToBottomLine <= distanceThreshold)
        {
            MessageBox.Show("Line matches!", "Detect Line", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else
        {
            MessageBox.Show("Does not match!", "Detect Line", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        upbuttobClick = false;

    }
Posted
Updated 27-Jul-23 1:15am
v2

OpenCV builds as either 64bit or 32bit. If you are trying to run your application on a 32bit processor, you will need to add the 32bit DLLs. This[^] details how to build the 32bit version.
 
Share this answer
 
Comments
sahil ajmeri 2022 18-Jul-23 3:02am    
hey @Pete O'Hanlon
i have tried all above solutions built x32bit dlls
still getting same issue
Pete O'Hanlon 19-Jul-23 8:25am    
Did you copy the 32 bit DLLs into the build output directory and try that?
sahil ajmeri 2022 20-Jul-23 1:58am    
yes !!!
Pete O'Hanlon 20-Jul-23 2:00am    
And have you used a dependency checker to see if there are other missing dependencies? https://www.dependencywalker.com/
sahil ajmeri 2022 20-Jul-23 5:15am    
yess i have checked missing dependencies too.
i get rid of the error by uninstlling the emgu.cv nuget package and i have added reference dlls from the version 2.3.0.1416
1) Emgu.CV.dll
2) Emgu.CV.UI.dll
3) Emgu.Util.dll
4) opencv_core231.dll
5) opencv_imgproc231.dll

but when you will use this old version you will have to modify the code and the functions that you are using there are many functions are not available in old version so this will be a two sided coin.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900