SciSharp/Numpy.NET

How to use the repo to convert some Python code?

zydjohnHotmail opened this issue · 14 comments

Hello:
I have the following Python code to convert one RGB image to one YUV image, and use numpy to calculate an average of one column.

`
import cv2
import numpy as np

img_rgb = cv2.imread('C:/Images/1.PNG')
img_yuv = cv2.cvtColor(img_rgb,cv2.COLOR_BGR2YUV)
averageV = np.average(img_yuv[:,:,2])
print(averageV);
`

The Python code works well. Now, I want to change it to use C# code, as I have many other C# programs will need this averageV value.

I have done the following:

  1. I created one C# console project with Visual Studio 2019 (target .NET 5.0)
  2. I installed necessary NUGET packages:
    PM> Install-Package OpenCvSharp4 -Version 4.5.3.20210817
    PM> Install-Package Numpy -Version 3.7.1.25
  3. I have the following C# code:

`
using Numpy;
using OpenCvSharp;
using System;

namespace ConvertRGB2YUV
{
class Program
{
public const string Image1_File = @"C:\Images\1.PNG";

    static void Main(string[] args)
    {
        Mat img_rgb = Cv2.ImRead(Image1_File);
        Mat img_yuv = img_rgb.CvtColor(ColorConversionCodes.RGB2YUV);
    }
}

}
`

I can run my code, and I can see the image: img_rgb and img_yuv.
But I have no idea on how to write the python corresponding statement:
averageV = np.average(img_yuv[:,:,2])
How I can do this in Numpy?
Please advise,
Thanks,

henon commented

Read the Readme on the main page. There is info about the array slicing syntax

Hello:
I think I read the readme already. But the issue is: if Python and Numpy use the same way to store data about image, then I should be able to do this.
Unfortunately, I don't think here it is the case.
If you know how to do this, please share some C# code.
Thanks,

henon commented

@Oceania2018 do you know how to convert a CV image into a Numpy array?

Hello:
I have done the following:

  1. I created one C# console project using Visual Studio 2019 (Version 16.11.3), target .NET 5.0 (X64)
  2. I installed the following NUGET packages:
    PM> Install-Package SharpCV
    PM> Install-Package OpenCvSharp4.runtime.win
  3. I downloaded one French national flag, it has 3 different colors, so it is good for testing. It has width of 225 pixel with height of 150 pixel, I can see it with MS-Paint, so the picture is good in quality.
    The following is my C# code:

`
using SharpCV;
using static SharpCV.Binding;
using System;

namespace OpenFrenchFlag
{
class Program
{
public const string French_Flag = @"C:\Images\FrenchFlag.PNG";
static void Main()
{
var img = cv2.imread(French_Flag, IMREAD_COLOR.IMREAD_GRAYSCALE);
byte p = img[8, 8];
Console.WriteLine("Done!");
}
}
}
`

When I run it, I got the following error:
'OpenFrenchFlag.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Remote Debugger\x64\Runtime\Microsoft.VisualStudio.Debugger.Runtime.NetCoreApp.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'OpenFrenchFlag.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\5.0.10\System.Runtime.InteropServices.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The program '[38040] OpenFrenchFlag.exe: Program Trace' has exited with code 0 (0x0).
The program '[38040] OpenFrenchFlag.exe' has exited with code -1 (0xffffffff).

It seems I miss some other NUGET packages?
Let me know how to fix this first!
By the way, if you can find a French national flag, you can try your code and let me know what is the result if you can do something like in Python:

Mat img_rgb = Cv2.ImRead(Image1_File);
Mat img_yuv = img_rgb.CvtColor(ColorConversionCodes.RGB2YUV);
averageV = np.average(img_yuv[:,:,2])

Thanks,
PS: My OS is Windows 10 (Version 21H1)

henon commented

@Oceania2018 I think SharpCV's NDArray is not the same (and not compatible with) Numpy.NET's NDarray, right?
Can you extract the raw data from NDArray into a byte[] ? If so it could be used to create a NDarray from it.

Hello:
I have installed this NUGET package:
PM> Install-Package SciSharp.TensorFlow.Redist -Version 2.6.0
Then run my code (my code didn't change)
But I got the following error:

System.NotImplementedException
HResult=0x80004001
Message=
Source=TensorFlow.NET
StackTrace:
at Tensorflow.NumPy.NDArrayConverter.Scalar[T](NDArray nd)
at Tensorflow.NumPy.NDArray.op_Implicit(NDArray nd)
at OpenFrenchFlag.Program.Main() in C:\OpenCV\OpenFrenchFlag\OpenFrenchFlag\Program.cs:line 13

@zydjohnHotmail try var p = img[8, 8];

Hello:
I changed my code, so it looks like this:
public const string French_Flag = @"C:\Images\FrenchFlag.PNG";
static void Main()
{
var img = cv2.imread(French_Flag, IMREAD_COLOR.IMREAD_GRAYSCALE);
var p = img[8, 8];
}

My code can run now, but I saw in debug, the Results_View, there was one message:
Message "Index was outside the bounds of the array."
However, let me know how I can calculate some values as Python code can do.
I think the dimension of img is not correct!
But I have no idea on how to fix this.
Please advise!

@zydjohnHotmail Can you PR an UnitTest, so we can start to work quickly.

Hi,
Let me know how to raise a PR?
Thanks,

Please follow the github docs.

Hello:
I don't want to bother now, as EmguCV (C#) has already done a good job for this kind of thing.