Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I finally found out the solution and I'd like to thanks berak for his reactivity and advices. First of all, I updated my OpenCV version to the 3.4.3 to avoid any issues with any bugs in the 3.4.1.. I still had the same issue.


To understand the following corrections :

Inside my .dll, the failure was there : bool success = Tracker->update(frame, roi); I then went in github to check the Tracker.cpp src code and the method updateImpl of trackerKCF.cpp. In order to check the whole code. The assert CV_Assert(img.channels() == 1 || img.channels() == 3); reminded me that and in my code, as Unity gives RGBA images, I naturally used RGBA images in my .dll...

Remove the uchar a; in the struct Color32 inside the DLL :

struct Color32
{
    uchar b;
    uchar g;
    uchar r;
};

Create a another struct CvColor and perform the compute the image for the .dll in the c# script :

...

// OpenCV prefers BGR instead of RGB
[StructLayout(LayoutKind.Sequential, Size = 3)]
public struct CvColor
{
    public byte B, G, R;
}

...

public class TrackingFromDLL : MonoBehaviour
{    
    ...

    void Update()
    {
        pixels32 = _webcamTexture.GetPixels32();
        int nbPixel = pixels32.Length;
        CvColor[] reverseCvPixels32 = new CvColor[nbPixel];
        Parallel.For(0, imHeight, i =>
        {
            for (int j = 0; j < imWidth; ++j)
            {
                Color32 currentColor = pixels32[i * imWidth + j];

                // OpenCV's pixel (0,0) is the top-left pixel
                int newPos = nbPixel - (i + 1) * imWidth + j;
                reverseCvPixels32[newPos].B = currentColor.b;
                reverseCvPixels32[newPos].G = currentColor.g;
                reverseCvPixels32[newPos].R = currentColor.r;
            }
        });

        // Do whatever you want with the DLL and reverseCvPixels32
        ...
    }
...
}

Everything works fine now and you can do very various things within unity and the tracking from OpenCV ! (A Space Invader where the spaceship move with your hand or your face for example)

I finally found out the solution and I'd like to thanks berak for his reactivity and advices. First of all, I updated my OpenCV version to the 3.4.3 to avoid any issues with any bugs in the 3.4.1.. I still had the same issue.


To understand the following corrections :

Inside my .dll, the failure was there : bool success = Tracker->update(frame, roi); I then went in github to check the Tracker.cpp src code and the method updateImpl of trackerKCF.cpp. In order to check the whole code. The assert CV_Assert(img.channels() == 1 || img.channels() == 3); reminded me that and in my code, as Unity gives RGBA images, I naturally used RGBA images in my .dll....dll... This why it crashed !

Remove the uchar a; in the struct Color32 inside the DLL :

struct Color32
{
    uchar b;
    uchar g;
    uchar r;
};

Create a another struct CvColor and perform the compute the image for the .dll in the c# script :

...

// OpenCV prefers BGR instead of RGB
[StructLayout(LayoutKind.Sequential, Size = 3)]
public struct CvColor
{
    public byte B, G, R;
}

...

public class TrackingFromDLL : MonoBehaviour
{    
    ...

    void Update()
    {
        pixels32 = _webcamTexture.GetPixels32();
        int nbPixel = pixels32.Length;
        CvColor[] reverseCvPixels32 = new CvColor[nbPixel];
        Parallel.For(0, imHeight, i =>
        {
            for (int j = 0; j < imWidth; ++j)
            {
                Color32 currentColor = pixels32[i * imWidth + j];

                // OpenCV's pixel (0,0) is the top-left pixel
                int newPos = nbPixel - (i + 1) * imWidth + j;
                reverseCvPixels32[newPos].B = currentColor.b;
                reverseCvPixels32[newPos].G = currentColor.g;
                reverseCvPixels32[newPos].R = currentColor.r;
            }
        });

        // Do whatever you want with the DLL and reverseCvPixels32
        ...
    }
...
}

Everything works fine now and you can do very various things within unity and the tracking from OpenCV ! (A Space Invader where the spaceship move with your hand or your face for example)example).