shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.22k stars 1.13k forks source link

mat.shape not available #1547

Closed mdjavedakhtar closed 1 year ago

mdjavedakhtar commented 1 year ago

Summary of your issue

in python i was able to use mat.shape[3] but in c the mat.shape is not available

Environment

.net winui3

What did you do when you faced the problem?

trying to extract x,y co-ordinate from mat

Example code:

Output:

paste your output

What did you intend to be?

in python out.shape[3] was working but in here mat.shape is not available i am using a tensorflow dnn to match object is there any other function to get the xy value

shimat commented 1 year ago

Use Mat.Size() or Mat.Size(int dim) instead. OpenCvSharp is OpenCV C++ API compatible and not opencv-python (numpy) compatible.

using var mat = new Mat(3, 4, MatType.CV_8UC1);

Console.WriteLine(mat.Size()); // Width=4, Height=3  (Rows=3, Cols=4)
Console.WriteLine(mat.Size(0)); // 3
Console.WriteLine(mat.Size(1)); // 4

var shape = Enumerable.Range(mat.Dims).Select(i => mat.Size(i)).ToArray();
mdjavedakhtar commented 1 year ago

i am using c# and bit new to opencvsharp the code bit i am using is as bellow

` private Mat _capturedFrame = new Mat(); private Net net; private Mat out1;

net = CvDnn.ReadNetFromTensorflow("model.pb"); net.SetInput(CvDnn.BlobFromImage(_capturedFrame, 1.0, s1, s2, true, false)); out1 = net.Forward(); `

Debug.WriteLine(out1.Size()); gives (width:57 height:1) as i can understand there are 57 rows but my question is how can i get the data of say row 1 and even extract the x,y co-ordinate of the data at row 1

out1.Type() is CV_32FC1

shimat commented 1 year ago

You can get values of out1 like this:

for (int col = 0; col < 57; col++)
{
    var v = out1.Get<float>(col);
    Console.WriteLine(v);
}
mdjavedakhtar commented 1 year ago

the code given by you i have already tried it gives some float value.

for object detection i need to know the X,Y co ordinate of the 57 objects and confidence value in python it was giving a shape heatMap = out[0, i, :, :] cv.minMaxLoc(heatMap)