Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I just want to convert Mat type variable to Texture2D type.
I can convert texture2D to mat and this only use EncodeToJPG() function.
like this:
Mat mat = Mat.FromImageData(_texture.EncodeToPNG());
Texture2D -> Mat is easy...but I cannot convert "MAT -> Texture2D"
With opencvsharp, use Mat.GetArray
to get the byte array data of the mat then loop over it based on the the height and width of the mat. Copy the mat data to Color32
in that loop and finally use Texture2D.SetPixels32()
and Texture2D.Apply()
to set and apply the pixel.
void MatToTexture(Mat sourceMat)
//Get the height and width of the Mat
int imgHeight = sourceMat.Height;
int imgWidth = sourceMat.Width;
byte[] matData = new byte[imgHeight * imgWidth];
//Get the byte array and store in matData
sourceMat.GetArray(0, 0, matData);
//Create the Color array that will hold the pixels
Color32[] c = new Color32[imgHeight * imgWidth];
//Get the pixel data from parallel loop
Parallel.For(0, imgHeight, i => {
for (var j = 0; j < imgWidth; j++) {
byte vec = matData[j + i * imgWidth];
var color32 = new Color32 {
r = vec,
g = vec,
b = vec,
a = 0
c[j + i * imgWidth] = color32;
//Create Texture from the result
Texture2D tex = new Texture2D(imgWidth, imgHeight, TextureFormat.RGBA32, true, true);
tex.SetPixels32(c);
tex.Apply();
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.