Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code that uses MatType built under 4.9 but not under 4.10 #1685

Open
jmo-gelsight opened this issue Jul 16, 2024 · 4 comments
Open

Code that uses MatType built under 4.9 but not under 4.10 #1685

jmo-gelsight opened this issue Jul 16, 2024 · 4 comments

Comments

@jmo-gelsight
Copy link

jmo-gelsight commented Jul 16, 2024

Summary of your issues

I just updated to the latest version of OpenCvSharp. Suddenly my code no longer builds. I don't know what the proper way to fix it is. I cannot even find a discussion of the changes.

Under 4.9 (with .NET 8) this code built just fine (just the first part of a function listed

    public static unsafe Sdk.Image ToSdkImage(this Mat mat)
    {
        // Currently we're only handling a few pixel types.  Expand this later.

        switch (mat.Type())
        {
        case Cv.MatType.CV_32F:
            if (!mat.GetArray<float>(out var arr))
                throw new ArgumentException(nameof(mat));

However under 4.10, The compiler does not like the case clause; I get compiler error CS9135 "A constant value of type 'MatType' is expected"

Similarly under 4.9, this code builds

var surface = new Mat(hmHeightPx, hmWidthPx, MatType.CV_32FC1, hm.ImageData);

(where hmHeightPx and hmWidthPx are integers and hm.ImageData is of type float[])

But under 4.0, I get compiler error CS0122 "'Mat.Mat(int, int, MatType, Array, long)' is inaccessible due to its protection level"

Environment

Windows 10.
Visual Studio 2022
.NET 8
C# 12

I have no idea what to do. Do I make my switch statement switch on mat.Type().Depth instead? What is the new valid way to compare MatTypes?

And who do I construct a Mat from a width, height, mat-type and an array of raw data?

Example code:

pasted above 

Output:

22>C:\Users\joe\source\repos\Main\Mobile\Core\Core\CvMatExtensions.cs(27,14,27,31): error CS9135: A constant value of type 'MatType' is expected
22>C:\Users\joe\source\repos\Main\Mobile\Core\Core\CvMatExtensions.cs(33,14,33,30): error CS9135: A constant value of type 'MatType' is expected
22>C:\Users\joe\source\repos\Main\Mobile\Core\Core\Extensions\HeightMapExtensions.cs(132,30,132,33): error CS0122: 'Mat.Mat(int, int, MatType, Array, long)' is inaccessible due to its protection level
22>C:\Users\joe\source\repos\Main\Mobile\Core\Core\Extensions\ImageExtensions.cs(484,35,484,41): error CS0122: 'Mat.Mat(int, int, MatType, Array, long)' is inaccessible due to its protection level
22>C:\Users\joe\source\repos\Main\Mobile\Core\Core\Extensions\ImageExtensions.cs(485,35,485,41): error CS0122: 'Mat.Mat(int, int, MatType, Array, long)' is inaccessible due to its protection level
22>C:\Users\joe\source\repos\Main\Mobile\Core\Core\Extensions\ImageExtensions.cs(486,35,486,41): error CS0122: 'Mat.Mat(int, int, MatType, Array, long)' is inaccessible due to its protection level
22>C:\Users\joe\source\repos\Main\Mobile\Core\Core\Extensions\ImageExtensions.cs(487,35,487,41): error CS0122: 'Mat.Mat(int, int, MatType, Array, long)' is inaccessible due to its protection level
22>C:\Users\joe\source\repos\Main\Mobile\Core\Core\ScanAdvancedAlignment.cs(705,21,705,33): error CS0266: Cannot implicitly convert type 'OpenCvSharp.MatType' to 'int'. An explicit conversion exists (are you missing a cast?)
22>Done building project "GelSight.Core.csproj" -- FAILED.
StopOnFirstBuildError: Build cancelled because project "Core" failed to build.
Build has been canceled.

What did you intend to be?

No compiler errors :-)

@Turby001
Copy link

Turby001 commented Jul 24, 2024

I have the same issue when migrating from 4.9 to 4.10

"how do I construct a Mat from a width, height, mat-type and an array of raw data?"

@shimat
Copy link
Owner

shimat commented Jul 25, 2024

Please use Mat.FromPixelData instead.
https://github.com/shimat/opencvsharp/pull/1677/files#diff-4bfce067c7a4f3c0bd76d672e8b7c457350681fb3f89922045c2dcbc05a03fb3R273

With the introduction of nint in C# 9, IntPtr is implicitly replaced by nint. This made overloading the Mat constructors difficult to resolve, so I had no choice but to make this change.

@jmoleary
Copy link

jmoleary commented Jul 30, 2024

Thank you for this. However this only address the second problem I was having. When trying to compare a Mat.Type() value against, say Cv.MatType.CV_32F, I can no longer use a switch statement. Is it still meaningful to compare them with operator== ?

@Jean-PhilippeGoerke
Copy link

I am facing the same issue with switch statements over MatType as well as implicit conversions from int/double to Scalar are no longer possible.
Methods that take in a destination type can now no longer be called with mat.Type() but with mat.Type().ToInt32().

What is reasoning behind changing multiple implicit operators into an explicits (although MatType is only an int)?
(And I don't see the benefit right now when half the wrapper is using int and the other MatType.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants