Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Color32[] to byte[] too slow (in c#)

Discussion in 'Editor & General Support' started by kyewong, Sep 11, 2014.

  1. kyewong

    kyewong

    Joined:
    Sep 11, 2014
    Posts:
    22
    Hi guys,

    I have a live video captured from the web camera( done with WebCamTexture), and extracted the data in Color32[] format. Now I would like to convert it to byte[] (by reading the r,g,b value of each pixel one by one and storing it in byte[]), and pass the byte stream into a function defined in a dll file. However, the conversion is quite slow for a 1920*1080 sized video(more than 100ms per frame).
    I would like to know:

    1 why it is so slow converting from Color32[] to byte []? Is there a faster method?

    2 When I pass Color32[] directly into the functions in the dll file, the time becomes even longer (more than 300ms per frame), why does that happen?

    3 Are there any better solution to pass the captured video into the function defined in the dll file?

    Thanks and wait for ur suggestions!
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,491
    Hi,

    the reason it's slow is because there's just too much pixel information to process in 1080p video. That's over 2 million pixels! Did you try just passing the pointer directly to the buffer? Like this:

    Code (csharp):
    1.  
    2. Color32[] pixels = GetPixels();
    3.  
    4. unsafe
    5. {
    6.     fixed (byte* ptr = (byte*)pixels)
    7.     {
    8.         MyNativeFunction(ptr);
    9.     }
    10. }
    11.  
    12.  
     
  3. kyewong

    kyewong

    Joined:
    Sep 11, 2014
    Posts:
    22
    Thanks!
    The followed question is, how to recover the image from the "byte* ptr" in "MyNativeFunction"? Especially, if "MyNativeFunction" is written in c++ and wrapped in a dll file, how to recover an image from "byte* ptr" ?



     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,491
    It shouldn't change. You get a pointer to RGBA32 pixel array in native code.
     
  5. kyewong

    kyewong

    Joined:
    Sep 11, 2014
    Posts:
    22
    Thanks Tau, it realy works.
    But there's still one problem:
    I restore the image in the function using the following method:

    int _DLLExport GetWebcamImg(byte* ImgIn)
    {
    cv::Mat opencvImage(1080,1920,CV_8UC4);
    memcpy(opencvImage.data,ImgIn,1920*1080*4);
    cv::cvtColor(opencvImage,opencvImage,CV_BGR2RGB);
    }

    but the image I obtained is the one that flipped horizontally, why is that please?