shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.37k stars 1.15k forks source link

Converting Mat to byte[] to send to nvidia triton server #1572

Open SCraenen opened 1 year ago

SCraenen commented 1 year ago

Summary of your issue

I want to convert OpenCvSharp Mat object to a byte[] that maintains it's size. To clarify what I mean is if I'd have an image of width and height 640, I want to receive a byte[] of size 640 640 3 4 (w h c 4bytesInFloat).

To clarify the purpose: I will write this byte[] in to system shared memory and transfer it to nvidia triton server to do model inference.

Environment

I'm working on windows, linux x64 and linux arm64.

Example code:

In python the equivalent code would look something like this:

image = cv2.imread(image_path, cv2.IMREAD_COLOR)
image = image.transpose((2, 0, 1))
image = image.astype(dtype=np.float32) / 255
image_bytes = image.tobytes()

Desired result:

I want to implement the same functionality as above with OpenCvSharp.

---- edit ---- it appears that the following code does the trick. I am confused because the bytes I manually inspected did not match my expectation. I created a 3 by 3 image with only full r g b white or black pixels however there were values like 4 in my byte array. It turns out that when I save the image with a bit depth of 8 I get the correct bytes while the original image with bit depth 24 gives invalid colours when loaded with Cv2.ImRead();

Marshal.Copy(image.Data, imageBytes, 0, (int)imageByteCount);

I still need to have the functionality of the transpose which I can't seem to figure out with OpenCvSharp

small-24depth small-8depth

stale[bot] commented 9 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.