Skip to content

How to pass Mat to Tensor #96

@Assassyn

Description

@Assassyn

Hi,

I am trying to connect FastOpenCV with onnxruntime-react-native, but I am unsure how to do it in JavaScript with FastOpenCV. I have similar functionality implemented in F#, and it looks like that:

`let private normalizeAndResize (image: Mat) =
let targetWidth = 1024
let targetHeight = 1024
let mean = [| 0.485f; 0.456f; 0.406f |]
let std = [| 1.0f; 1.0f; 1.0f |]

// Resize the image
let workingImage = image.Resize(new Size(targetWidth, targetHeight))

// Create tensor for image data
let input = DenseTensor<float32>([| 1; 3; targetHeight; targetWidth |])

for y in 0 .. targetHeight - 1 do
    for x in 0 .. targetWidth - 1 do
        let pixel = workingImage.Get<Vec3b>(y, x)

        // Normalize pixel values
        input.[0, 0, y, x] <- (float32 pixel.Item2 / 255.0f - mean.[0]) / std.[0] // R channel
        input.[0, 1, y, x] <- (float32 pixel.Item1 / 255.0f - mean.[1]) / std.[1] // G channel
        input.[0, 2, y, x] <- (float32 pixel.Item0 / 255.0f - mean.[2]) / std.[2] // B channel

input`

My main problem is that I do not know how to extract pixels from Mat. I have tried to use toJSValue, but for an image 1024x1024 it produces only around 300k of bytes instead of the desired ~1M

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions