Urko Sanchez Ortiz
Urko Sanchez Ortiz

Reputation: 311

Problems with cv::Rectangle in C++

I have a problem when drawing a rectangle in a cv :: mat. I am making a communication between Unity and C ++ to generate an Android application.

I use the webcamtexture camera of unity and I send the information of to C ++ using the PInvoke method. Once in the C ++ code, I want to draw a rectangle but I get more than one in the image (see image) and I really do not understand why. I hope you can help me I attach the C++ and C# code.

introducir la descripción de la imagen aquí

C#

void Update()
{
    //texto.text = texto.text + " / "+ testo().ToString();
    imgData = null;
    imgData = new byte[width * height * 3];
    resultado = null;
    resultado = new byte[width * height * 3];
    color = webcam.GetPixels32();
    int bytesPerPixel = 3;
    const int indexR = 0;
    const int indexG = 1;
    const int indexB = 2;
    for (var i = 0; i < color.Length; i++)
    {
        imgData[(i * bytesPerPixel) + indexR] = color[i].r;
        imgData[(i * bytesPerPixel) + indexG] = color[i].g;
        imgData[(i * bytesPerPixel) + indexB] = color[i].b;
    }
    color = null;
    ProcessFrame(imgData, resultado, 0, 0, 0,0, nuevo);
    nuevo = false;
    textura2 = new Texture2D(width, height, TextureFormat.RGB24, false);
    textura2.LoadRawTextureData(resultado);
    textura2.Apply();
    renderer.material.mainTexture = textura2;
    textura2 = null;
    Resources.UnloadUnusedAssets();
}

C++

void ProcessFrame(unsigned char* arr, unsigned char* resu,int posx, int posy, int poswidth, int posheight,bool nuevo) {
    Mat dst;//dst image
    trueRect.x = 150;
    trueRect.y = 150;
    trueRect.width = 100;
    trueRect.height = 100;
    wi = poswidth;
    he = posheight;
    mal = 20;
    dst = Mat(tamWid,tamHeid,CV_8UC3, arr);
    rectangle(dst, Point(50,50), Point(100,100), cv::Scalar(0, 255, 255));
    copy(dst.datastart, dst.dataend, resu);
}

Upvotes: 0

Views: 994

Answers (1)

api55
api55

Reputation: 11420

Just for the sake of completeness I will post the answer.

The problem is in the line

dst = Mat(tamWid,tamHeid,CV_8UC3, arr); 

The constructor of Mat takes as first two arguments rows and then columns, in your case is columns and rows.

So it should be:

dst = Mat(tamHeid,tamWid,CV_8UC3, arr);

Other considerations

As I said in the comments, you should be aware of certain possible source of errors, since the conventions are not the same in OpenCV and Unity. One of them is the origin of your 2D coordinate system. In OpenCV is on the top left and in Unity, or at least with the GetPixels32 function is in the bottom left of the image.

Another possible problem is the Colorspace. OpenCV when it loads, saves, and other functions expects BGR images, while in other frameworks they use RGB or RGBA which is 32 bits.

Upvotes: 1

Related Questions