Issue with Unwrapping Blob Data into Float Array After Using blobFromImage

Hello OpenCV experts!

I’m currently working on an Android Kotlin project where I’m dealing with a CV Mat that I’ve converted into a blob using the blobFromImage function. The purpose of this is to create a linear array by unwrapping in the H,C,W format, as required by a subsequent block of code. The process involves permuting the dimensions and then unwrapping them.

Here’s a summary of my approach:

  1. Convert a CV Mat into a blob using blobFromImage.
  2. Permute the dimensions to obtain the H,C,W format.
  3. Attempt to unwrap the blob data using the get function into a float array.

The issue I’m facing is that even though the blob seems to contain non-zero values (as I can see in the original CV Mat), when I use the get function to unwrap the data into a float array, all the elements in the float array remain as 0.

I’ve considered an alternative approach of looping through the blob values and unwrapping them one by one, but I’m unsure about how to access the individual values within a blob using OpenCV functions.

Could anyone shed some light on why the get operation is not working as expected? Additionally, any guidance on how to correctly access and unwrap the values within a blob would be greatly appreciated. Here’s a snippet of my code for reference:

// Convert CV Mat into blob
val input = FloatArray(1*256*256*3)
var temp = Dnn.blobFromImage(imageMat)
Core.transposeND(temp, MatOfInt(0,2,1,3), temp)
temp.get(0, 0, input)

Thank you in advance for your help and insights!

can you please explain, what kind of network this is, what it does, and why it expects this weird input format ?

what is happening ?

i would understand, if this is for the output, but you should not need to access the blob’s values for the input, why you need that ?

I appreciate your response and would like to address your questions and concerns:

can you please explain, what kind of network this is, what it does, and why it expects this weird input format ?

Indeed, the chosen input format is unconventional. The specific model in use is shared across our team, and modifying the model’s input dimensions wasn’t considered to avoid disrupting other workflows.

what is happening ?

The issue I’m facing is that even though the blob seems to contain non-zero values (as I can see in the original CV Mat), when I use the get function to unwrap the data into a float array, all the elements in the float array remain as 0.

i would understand, if this is for the output, but you should not need to access the blob’s values for the input, why you need that ?

My initial thought was to use the get operation, which didn’t yield the expected results. As a potential workaround, I think about accessing blob values to create a customized “get” operation.

I am getting the same problem for unwarpping the blob and getting array full of 0s,
my code to get it is

        val a = Math.toIntExact( blob.total())
        val rbuffer = FloatArray( a )
        blob.get(0, 0, rbuffer)

I tried printing stats of the Mat and it returns reasonable values

        val mean = MatOfDouble()
        val stdDev = MatOfDouble()
        Core.meanStdDev(blob, mean, stdDev);
        val meanValue = mean[0, 0][0]
        val stdDevValue = stdDev[0, 0][0]
        Log.d("retina","blob channels "+ blob.channels()+ " type "+ blob.type() + " size "+blob.size()+ " dim "+blob.dims())
        Log.d("retina","blob size "+ blob.size(0)+ "  "+ blob.size(1) + "  "+blob.size(2)+ "  "+blob.size(3))
        Log.d("retina","blob sum "+ Core.sumElems(blob))
        Log.d("retina","blob mean,std $meanValue, $stdDevValue")

Debugg prints

 D  blob channels 1 type 5 size 3x1 dim 4
 D  blob size 1  3  640  640
 D  blob sum [-304352.546875, 0.0, 0.0, 0.0]
 D  blob mean,std -0.247682736714681, 0.4440458841118953