Quansight-Labs / numpy.net

A port of NumPy to .Net
BSD 3-Clause "New" or "Revised" License
131 stars 14 forks source link

How to transfer Mat(opencvsharp) or bitmap into ndarray efficiently? #2

Closed BackT0TheFuture closed 3 years ago

BackT0TheFuture commented 4 years ago

Hi @KevinBaselinesw So excited to find numpy.net, It's great work! I want to use numpy.net right now, but I have no idea how to transfer Mat into ndarray . Is there a plan for implenting Mat (opencvsharp) to ndarray (numpy.net)? thx!

KevinBaselinesw commented 4 years ago

Hi goodtogood!

My email address is kmckenna@baselinesw.com mailto:kmckenna@baselinesw.com for easier communications.

Thank you for your interest in numpy.net. My library is a pure C# (.net standard 2.0/core xx) library that replicates the functionality of the python NumPy tool.

I developed it to port a large python application to C#.

I am 99% finished with this tool. I have a small number of functions to implement (i.e. np.gradient and np.einsum). I hope to get those working over the next couple of weeks. I work on it as time permits.

Currently numpy.net works with all of the common C# data types (double, float, Int64, Int32, Int16, etc…). Future support may include decimals, BigInts and Complex numbers.

Here are two unit tests that display how to create an ndarray (there are probably more than 100 different ways in total). If you can provide a more detailed question I can try to help you better.

   [TestMethod]

    public void test_arange_reshape_33()

    {

        var a = np.arange(2, 11).reshape(new shape(3,3));

        print(a);

        print(a.shape);

        print(a.strides);

        AssertArray(a, new Int32[3,3] { { 2, 3, 4 },{ 5, 6, 7 },{ 8, 9, 10 } });

        AssertShape(a, 3,3);

        AssertStrides(a, sizeof(Int32) * 3, sizeof(Int32));

    }

   [TestMethod]

    public void test_concatenate_2()

    {

        var a = np.array(new int[,,,] { { { { 1, 2 }, { 3, 4 }, { 5, 6 } } } });

        var c = np.concatenate(a, axis : 0);

        AssertArray(c, new int[,,] { { { 1, 2 }, { 3, 4 }, { 5, 6 } } });

        print(c);

        var d = np.concatenate(a, axis: 1);

        AssertArray(d, new int[,,] { { { 1, 2 }, { 3, 4 }, { 5, 6 } } });

        print(d);

        var e = np.concatenate(a, axis : null);

        AssertArray(e, new int[] { 1, 2, 3, 4, 5, 6 });

        print(e);

    }

From: goodtogood notifications@github.com Sent: Monday, December 16, 2019 9:50 AM To: Quansight-Labs/numpy.net numpy.net@noreply.github.com Cc: KevinBaselinesw kmckenna@baselinesw.com; Mention mention@noreply.github.com Subject: [Quansight-Labs/numpy.net] How to transfer Mat(opencvsharp) or bitmap into ndarray efficiently? (#2)

Hi @KevinBaselinesw https://github.com/KevinBaselinesw So excited to find numpy.net, It's great work! I want to use numpy.net right now, but I have no idea how to transfer Mat into ndarray . Is there a plan for implenting Mat (opencvsharp https://github.com/shimat/opencvsharp/blob/master/src/OpenCvSharp/Modules/core/Mat/Mat.cs#L24 ) to ndarray (numpy.net)? thx!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Quansight-Labs/numpy.net/issues/2?email_source=notifications&email_token=ACP4GWRZBKDRKHXFMJVKJMLQY6IRFA5CNFSM4J3LCAX2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IAYHVNQ , or unsubscribe https://github.com/notifications/unsubscribe-auth/ACP4GWUGCIMHP5DABG7JFCLQY6IRFANCNFSM4J3LCAXQ . https://github.com/notifications/beacon/ACP4GWVMMKTBGCWUNDTNBE3QY6IRFA5CNFSM4J3LCAX2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IAYHVNQ.gif

KevinBaselinesw commented 4 years ago

I just put NumpyDotNet as nuget package: https://www.nuget.org/packages/NumpyDotNet/

Search nuget for NumpyDotNet to get the released mode libraries and also unit tests to demonstrate all of the functions.

From: goodtogood notifications@github.com Sent: Monday, December 16, 2019 9:50 AM To: Quansight-Labs/numpy.net numpy.net@noreply.github.com Cc: KevinBaselinesw kmckenna@baselinesw.com; Mention mention@noreply.github.com Subject: [Quansight-Labs/numpy.net] How to transfer Mat(opencvsharp) or bitmap into ndarray efficiently? (#2)

Hi @KevinBaselinesw https://github.com/KevinBaselinesw So excited to find numpy.net, It's great work! I want to use numpy.net right now, but I have no idea how to transfer Mat into ndarray . Is there a plan for implenting Mat (opencvsharp https://github.com/shimat/opencvsharp/blob/master/src/OpenCvSharp/Modules/core/Mat/Mat.cs#L24 ) to ndarray (numpy.net)? thx!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Quansight-Labs/numpy.net/issues/2?email_source=notifications&email_token=ACP4GWRZBKDRKHXFMJVKJMLQY6IRFA5CNFSM4J3LCAX2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IAYHVNQ , or unsubscribe https://github.com/notifications/unsubscribe-auth/ACP4GWUGCIMHP5DABG7JFCLQY6IRFANCNFSM4J3LCAXQ . https://github.com/notifications/beacon/ACP4GWVMMKTBGCWUNDTNBE3QY6IRFA5CNFSM4J3LCAX2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IAYHVNQ.gif

BackT0TheFuture commented 4 years ago

Hi, @KevinBaselinesw Sorry for my late response. what I want to do is something like below,

python code

import cv2
import numpy as np

img = cv2.imread('./test.jpg')
print(type(img))
# <class 'numpy.ndarray'>
img = cv2.resize(img, (320, 320))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.transpose(2, 0, 1)
print(img.shape)
# (3, 320, 320)
data = img.ravel()

......(more process using numpy)

c# and ndarray in numpy.net Mat (class similar to ndarray ) in OpenCvSharp

using System;
using OpenCvSharp;
using NumpyDotNet;
using NumpyLib;

namespace Samples
{
    class Sample
    {
        public static void Test()
        {
            var img = Cv2.ImRead("./test.jpg");
            Cv2.Resize(img, img, new Size(320, 320));
            Cv2.CvtColor(img, img, ColorConversionCodes.RGB2BGR);

            // Mat to ndarray in numpy.net

            .......(more process using numpy.net)            

        }

    }
}

In brief, how to transfer Mat object into ndarray in numpy.net and vice versa. thanks!

KevinBaselinesw commented 4 years ago

If your data is a standard .NET data type (i.e. System.bool, System.Int32, System.Int64, etc…) that it likely just a matter of making a call like this:

    public static void Test()
    {
        var img = Cv2.ImRead("./test.jpg");
        Cv2.Resize(img, img, new Size(320, 320));
        Cv2.CvtColor(img, img, ColorConversionCodes.RGB2BGR);

       var imgnd = np.array(img, copy: false); // copy = false if want to share data with your application.
       imgnd = imgnd.transpose(2, 0, 1);
       print(imgnd.shape);  #(3, 320, 320)
       Var data = img.ravel();

This should work really well if your input data is a single dimension array. If your data is a multi-dimensional array, we have no choice but to create a copy.

        // Mat to ndarray in numpy.net

        .......(more process using numpy.net)            

    }

From: goodtogood notifications@github.com Sent: Saturday, January 4, 2020 11:30 AM To: Quansight-Labs/numpy.net numpy.net@noreply.github.com Cc: KevinBaselinesw kmckenna@baselinesw.com; Mention mention@noreply.github.com Subject: Re: [Quansight-Labs/numpy.net] How to transfer Mat(opencvsharp) or bitmap into ndarray efficiently? (#2)

Hi, @KevinBaselinesw https://github.com/KevinBaselinesw Sorry for my late response. what I want to do is something like below,

python code

import cv2 import numpy as np

img = cv2.imread('./test.jpg') print(type(img))

<class 'numpy.ndarray'>

img = cv2.resize(img, (320, 320)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = img.transpose(2, 0, 1) print(img.shape)

(3, 320, 320)

data = img.ravel()

......(more process using numpy)

c# and Mat https://github.com/shimat/opencvsharp/blob/master/src/OpenCvSharp/Modules/core/Mat/Mat.cs#L27 (class similar to ndarray ) in OpenCvSharp

using System; using OpenCvSharp; using NumpyDotNet; using NumpyLib;

namespace Samples { class Sample { public static void Test() { var img = Cv2.ImRead("./test.jpg"); Cv2.Resize(img, img, new Size(320, 320)); Cv2.CvtColor(img, img, ColorConversionCodes.RGB2BGR);

        // Mat to ndarray in numpy.net

        .......(more process using numpy.net)            

    }

}

}

thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Quansight-Labs/numpy.net/issues/2?email_source=notifications&email_token=ACP4GWUTQPGRJ5JNVQXDMFDQ4C2PPA5CNFSM4J3LCAX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIC3GYI#issuecomment-570798945 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ACP4GWSBJD56QSM6TYYBLSDQ4C2PPANCNFSM4J3LCAXQ . https://github.com/notifications/beacon/ACP4GWVQWB2X7RZPWN5WOUDQ4C2PPA5CNFSM4J3LCAX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIC3GYI.gif

KevinBaselinesw commented 4 years ago

Actually this is the required format for transpose to specify the axis to transpose around.

//imgnd = imgnd.transpose(2, 0, 1);

imgnd = imgnd.transpose(new new long[] { 2,0,1 });

From: goodtogood notifications@github.com Sent: Saturday, January 4, 2020 11:30 AM To: Quansight-Labs/numpy.net numpy.net@noreply.github.com Cc: KevinBaselinesw kmckenna@baselinesw.com; Mention mention@noreply.github.com Subject: Re: [Quansight-Labs/numpy.net] How to transfer Mat(opencvsharp) or bitmap into ndarray efficiently? (#2)

Hi, @KevinBaselinesw https://github.com/KevinBaselinesw Sorry for my late response. what I want to do is something like below,

python code

import cv2 import numpy as np

img = cv2.imread('./test.jpg') print(type(img))

<class 'numpy.ndarray'>

img = cv2.resize(img, (320, 320)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = img.transpose(2, 0, 1) print(img.shape)

(3, 320, 320)

data = img.ravel()

......(more process using numpy)

c# and Mat https://github.com/shimat/opencvsharp/blob/master/src/OpenCvSharp/Modules/core/Mat/Mat.cs#L27 (class similar to ndarray ) in OpenCvSharp

using System; using OpenCvSharp; using NumpyDotNet; using NumpyLib;

namespace Samples { class Sample { public static void Test() { var img = Cv2.ImRead("./test.jpg"); Cv2.Resize(img, img, new Size(320, 320)); Cv2.CvtColor(img, img, ColorConversionCodes.RGB2BGR);

        // Mat to ndarray in numpy.net

        .......(more process using numpy.net)            

    }

}

}

thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Quansight-Labs/numpy.net/issues/2?email_source=notifications&email_token=ACP4GWUTQPGRJ5JNVQXDMFDQ4C2PPA5CNFSM4J3LCAX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIC3GYI#issuecomment-570798945 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ACP4GWSBJD56QSM6TYYBLSDQ4C2PPANCNFSM4J3LCAXQ . https://github.com/notifications/beacon/ACP4GWVQWB2X7RZPWN5WOUDQ4C2PPA5CNFSM4J3LCAX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIC3GYI.gif