简体   繁体   中英

Convert Texture2D to OpenCV Mat?

There is a post about converting OpenCV cv::Mat to Texture2D in Unity and I provided an answer which works well. Now, I am trying to do the opposite but have been stuck on this for few hours now.

I want to convert Unity's Texture2D to OpenCV cv::Mat so that I can process the Texture on the C++ side.

Here is the original Texture2D in my Unity project that I want to convert to cv:Mat :

在此处输入图片说明

Here is what it looks like after converting it into cv:Mat :

在此处输入图片说明

It looks so washed out. I am not worried about the rotation of the image . I can fix that. Just wondering why it looks so washed out. Also used cv::imwrite to save the image for testing purposes but the issue in also in the saved image.

C# code :

[DllImport("TextureConverter")]
private static extern float TextureToCVMat(IntPtr texData, int width, int height);

unsafe void TextureToCVMat(Texture2D texData)
{
    Color32[] texDataColor = texData.GetPixels32();
    //Pin Memory
    fixed (Color32* p = texDataColor)
    {
        TextureToCVMat((IntPtr)p, texData.width, texData.height);
    }
}

public Texture2D tex;

void Start()
{
    TextureToCVMat(tex);
}

C++ code :

DLLExport void TextureToCVMat(unsigned char*  texData, int width, int height)
{
    Mat texture(height, width, CV_8UC4, texData);

    cvNamedWindow("Unity Texture", CV_WINDOW_NORMAL);
    //cvResizeWindow("Unity Texture", 200, 200);
    cv::imshow("Unity Texture", texture);
    cv::imwrite("Inno Image.jpg", texture);
}

I also tried creating a struct on the C++ side to hold the pixel information instead of using unsigned char* but the result is still the-same:

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


DLLExport void TextureToCVMat(Color32* texData, int width, int height)
{
    Mat texture(height, width, CV_8UC4, texData);

    cvNamedWindow("Unity Texture", CV_WINDOW_NORMAL);
    cvResizeWindow("Unity Texture", 200, 200);
    cv::imshow("Unity Texture", texture);
}

Why does the image look so so washed out and how do you fix this?

OpenCV creates images as BGR by default, whereas Color32 stores pixels as RGBA . However since OP mentioned in the comments that the Texture2D.format gives texture format as RGB24 , we can ignore the alpha channel altogether.

DLLExport void TextureToCVMat(unsigned char*  texData, int width, int height)
{
    Mat texture(height, width, CV_8UC4, texData);
    cv::cvtColor(texture,texture,cv::COLOR_BGRA2RGB);

    cv::imshow("Unity Texture", texture);
    cv::waitKey(0);
    cv::destroyAllWindows();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM