Ask Your Question

DJ7179D's profile - activity

2018-12-13 04:14:06 -0600 received badge  Enthusiast
2018-11-06 08:56:18 -0600 received badge  Teacher (source)
2018-11-06 08:53:10 -0600 commented answer Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

It is still weird and frustrating that Unity prefers to crash and say that no fixes are available instead of showing the

2018-11-06 08:51:22 -0600 edited answer Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

I finally found out the solution and I'd like to thanks berak for his reactivity and advices. First of all, I updated my

2018-11-06 08:50:12 -0600 marked best answer Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

Hi guys !

I'd like to use some OpenCV Tracker's inside a Unity Project. To do so, I created a .dll file that I imported inside my unity project.

Here is my code to create the DLL :

cv::Ptr<cv::TrackerKCF> Tracker;
cv::Rect2d roi;

// To use image date from Unity
struct Color32
{
    uchar r;
    uchar g;
    uchar b;
    uchar a;
};

// To pass ROI's position information beetwen c# and c++
struct Square
{
    Square(int x, int y, int width, int height) : X(x), Y(y), Width(width), Height(height) {}
    int X, Y, Width, Height;
};

extern "C" bool __declspec(dllexport) __stdcall InitTracker(Color32* frameData, int cameraWidth, int cameraHeight, Square & square)
{
    Tracker = cv::TrackerKCF::create();
    cv::Mat frame(cameraHeight, cameraWidth, CV_8UC4, frameData);

    roi = selectROI("Selection", frame);
    square = Square(roi.x, roi.y, roi.width, roi.height);

    // return true if the init is OK, false otherwise
    bool success = Tracker->init(frame, roi);

    cv::rectangle(frame, roi, cv::Scalar(0, 0, 255));
    cv::imshow("Init", frame);

    return success;
}

extern "C" bool __declspec(dllexport) __stdcall UpdateTracker(Color32* frameData, int cameraWidth, int cameraHeight, Square & square)
{
    cv::Mat frame(cameraHeight, cameraWidth, CV_8UC4, frameData);
    cv::Mat copyToCheck(frame);

    cv::rectangle(copyToCheck, roi, cv::Scalar(0, 0, 255));
    cv::imshow("before Update", copyToCheck);
    cv::waitkey(0);

    bool success = Tracker->update(frame, roi);

    cv::rectangle(copyToCheck, roi, cv::Scalar(0, 0, 255));
    cv::imshow("after Uptade", copyToCheck);
    cv::waitkey(0);

    square = Square(roi.x, roi.y, roi.width, roi.height);
    return success;
}

extern "C" void __declspec(dllexport) __stdcall Close()
{
    cv::destroyAllWindows();
}

And here is my c# script :

using System;
using System.Runtime.InteropServices;

using Uk.Org.Adcock.Parallel;

using UnityEngine;

// Define the structure to be sequential and with the correct byte size (4 ints = 4 bytes * 4 = 16 bytes)
[StructLayout(LayoutKind.Sequential, Size = 16)]
public struct CvSquare
{
    public int X, Y, Width, Height;

    public CvSquare(int x, int y, int width, int height) : this()
    {
        this.X = x;
        this.Y = y;
        this.Width = width;
        this.Height = height;
    }
}

// To import the DLL's methods
internal static class TrackingCpp
{
    [DllImport("TrackingCppToDLL")]
    internal static extern void Close();

    [DllImport("TrackingCppToDLL")]
    internal static extern bool InitTracker(Color32[] frameData, int cameraWidth, int cameraHeight, ref CvSquare square);

    [DllImport("TrackingCppToDLL")]
    internal static extern bool UpdateTracker(Color32[] frameData, int cameraWidth, int cameraHeight, ref CvSquare square);
}

public class TrackingFromDLL : MonoBehaviour
{
    private Color32[] pixels32;
    private WebCamTexture _webcamTexture;
    private const int imWidth = 640;
    private const int imHeight = 480;
    private CvSquare square;

    void Start()
    {
        // initialized the webcam texture
        WebCamDevice[] devices = WebCamTexture.devices;
        _webcamTexture = new WebCamTexture(devices[0].name, imWidth, imHeight);

        // Play the video source
        _webcamTexture.Play();
    }

    void Update()
    {
        pixels32 = _webcamTexture.GetPixels32();
        int nbPixel = pixels32.Length;
        Color32[] reversePixels32 = new Color32[nbPixel];
        Parallel.For(0, imHeight, i =>
        {
            for (int j = 0; j < imWidth; ++j)
            {
                // OpenCV's pixel (0,0) is the top-left pixel
                int newPos = nbPixel - (i + 1) * imWidth + j;
                reversePixels32[newPos] = pixels32[i * imWidth + j];

                // OpenCV prefers BGRA instead of RBGA
                byte tmp = reversePixels32[newPos].b;
                reversePixels32[newPos].b = reversePixels32[newPos].r;
                reversePixels32[newPos].r = tmp;
            }
        });

        if (!isInitDone)
            isInitDone = TrackingCpp.InitTracker(reversePixels32 ...
(more)
2018-11-06 08:49:34 -0600 received badge  Self-Learner (source)
2018-11-06 08:49:13 -0600 answered a question Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

I finally found out the solution and I'd like to thanks berak for his reactivity and advices. First of all, I updated my

2018-11-04 10:46:48 -0600 commented question Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

About the debugging in mixed mode, do you know how to do that within this kind of project ? I'm using VS 2017, and I ha

2018-11-02 11:57:25 -0600 commented question Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

I uptaded the post with the good code (my mistake with the rectangle in the Init and the Displays I didn't copy/paste in

2018-11-02 11:50:41 -0600 edited question Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL Hi guys ! I'd like to us

2018-11-02 11:49:19 -0600 edited question Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL Hi guys ! I'd like to us

2018-11-02 11:45:54 -0600 edited question Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL Hi guys ! I'd like to us

2018-11-02 11:43:29 -0600 commented question Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

Normally, I should use the Tracker->Update(..) return's value (true/false) to display or not the updated box in the i

2018-11-02 11:37:01 -0600 commented question Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

I'll remotely debug it during the night and I'll keep you updated. The selectROI method blocks the processing until an

2018-11-02 10:38:39 -0600 received badge  Editor (source)
2018-11-02 10:38:39 -0600 edited question Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL Hi guys ! I'd like to us

2018-11-02 10:35:42 -0600 commented question Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

Thx for the answer ! There is no chance that my c++ code is used from more than 1 thread, but still, I have no idea how

2018-11-02 10:10:09 -0600 asked a question Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL

Create DLL using TrackerKCF for a Unity Project - Crash when updating the tracker with the DLL Hi guys ! I'd like to us