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?