Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Losing alpha channel when passing from Unity to OpenCV?

Hello, I'm trying to develop an educational application using Unity3d and OpenCV (c++ Dll). My problem is that if pass an image from unity encoded in PNG (in BGRA32 textureFromat) to OpenCV, if i save the image through OpenCV it looks like alpha channel is removed. Alpha channel is critical in some 'heavy' OpenCV-side operations for my application and this removal is disastrous.

This is what i get by saving the image from Unity (there is a transparent border) image description

This is what i get by saving the image from OpenCV image description

Here is the relative portion of the c++ code

public void (uchar imMedium, int imMediumLength) {

         std::vector<uchar> vectordataImMedium(imMedium, imMedium + imMediumLength);

     cv::Mat data_mat_medium(vectordataImMedium, true);

     mediumImage = cv::imdecode(data_mat_medium, CV_LOAD_IMAGE_UNCHANGED);

     cv::imwrite("From Opencv1_medium.png", mediumImage, compression_params);

} and here is the c# code respectful //Create a texture2D to pass data from rendertexture with BGRA32 format

    Texture2D textMedium = new Texture2D(rtMedium.width, rtMedium.height, TextureFormat.BGRA32, false);

    textMedium.alphaIsTransparency = true;

    textMedium.Apply();

//fill texture2D with data from rendertexture

    RenderTextureToTexture2D(ref rtMedium, ref textMedium);

//create the array to pass to OpenCV

   byte[] imageMedium = textMedium.EncodeToPNG();

I think I need some help or some guidance, Thank you.

Losing alpha channel when passing from Unity to OpenCV?

Hello, I'm trying to develop an educational application using Unity3d and OpenCV (c++ Dll). My problem is that if pass an image from unity encoded in PNG (in BGRA32 textureFromat) to OpenCV, if i save the image through OpenCV it looks like alpha channel is removed. Alpha channel is critical in some 'heavy' OpenCV-side operations for my application and this removal is disastrous.

This is what i get by saving the image from Unity (there is a transparent border) border) before sending to OpenCV dll image description

This is what i get by saving the image from OpenCV image description

Here is the relative portion of the c++ code

public void (uchar imMedium, int imMediumLength) {

         std::vector<uchar> vectordataImMedium(imMedium, imMedium + imMediumLength);

     cv::Mat data_mat_medium(vectordataImMedium, true);

     mediumImage = cv::imdecode(data_mat_medium, CV_LOAD_IMAGE_UNCHANGED);

     cv::imwrite("From Opencv1_medium.png", mediumImage, compression_params);

} and here is the c# code respectful respectful

//Create a texture2D to pass data from rendertexture with BGRA32 format

    Texture2D textMedium = new Texture2D(rtMedium.width, rtMedium.height, TextureFormat.BGRA32, false);

    textMedium.alphaIsTransparency = true;

    textMedium.Apply();

//fill texture2D with data from rendertexture

    RenderTextureToTexture2D(ref rtMedium, ref textMedium);

//create the array to pass to OpenCV

   byte[] imageMedium = textMedium.EncodeToPNG();

I think I need some help or some guidance, Thank you.

Losing alpha channel when passing from Unity to OpenCV?

Hello, I'm trying to develop an educational application using Unity3d and OpenCV (c++ Dll). My problem is that if when I pass an image from unity encoded in PNG (in BGRA32 textureFromat) to OpenCV, if i save the image through OpenCV it looks like alpha channel is removed. Alpha channel is critical in some 'heavy' OpenCV-side operations for my application and this removal is disastrous.

This is what i get by saving the image from Unity (there is a transparent border) before sending it to OpenCV dll image description

This is what i get by saving the image from OpenCV image description

Here is the relative portion of the c++ code

public void (uchar saveIncomingImageArray(uchar * imMedium, int imMediumLength) {

         std::vector<uchar> vectordataImMedium(imMedium, imMedium + imMediumLength);

     cv::Mat data_mat_medium(vectordataImMedium, true);

     mediumImage = cv::imdecode(data_mat_medium, CV_LOAD_IMAGE_UNCHANGED);

     cv::imwrite("From Opencv1_medium.png", mediumImage, compression_params);

} }

compression_params are defined just before the saveIncomingImageArray function
and here is the c# code respectful

//Create a texture2D to pass data from rendertexture with BGRA32 format

    Texture2D textMedium = new Texture2D(rtMedium.width, rtMedium.height, TextureFormat.BGRA32, false);

    textMedium.alphaIsTransparency = true;

    textMedium.Apply();

//fill texture2D with data from rendertexturerendertexture - function defined in my code elsewhere

    RenderTextureToTexture2D(ref rtMedium, ref textMedium);

//create the array to pass to OpenCV

   byte[] imageMedium = textMedium.EncodeToPNG();

I think I need some help or some guidance, Thank you.

Losing alpha channel when passing from Unity to OpenCV?

Hello, I'm trying to develop an educational application using Unity3d and OpenCV (c++ Dll). My problem is that when I pass an image from unity encoded in PNG (in BGRA32 textureFromat) to OpenCV, if i save the image through OpenCV it looks like alpha channel is removed. Alpha channel is critical in some 'heavy' OpenCV-side operations for my application and this removal is disastrous.

This is what i get by saving the image from Unity (there is a transparent border) before sending it to OpenCV dll image description

This is what i get by saving the image from OpenCV image description

Here is the relative portion of the c++ code

public void saveIncomingImageArray(uchar * imMedium, int imMediumLength) {

         std::vector<uchar> vectordataImMedium(imMedium, imMedium + imMediumLength);

     cv::Mat data_mat_medium(vectordataImMedium, true);

     mediumImage = cv::imdecode(data_mat_medium, CV_LOAD_IMAGE_UNCHANGED);

     cv::imwrite("From Opencv1_medium.png", mediumImage, compression_params);

}

compression_params are defined just before the saveIncomingImageArray function
and here is the c# code respectful

//Create a texture2D to pass data from rendertexture with BGRA32 format

    Texture2D textMedium = new Texture2D(rtMedium.width, rtMedium.height, TextureFormat.BGRA32, false);

    textMedium.alphaIsTransparency = true;

    textMedium.Apply();

//fill texture2D with data from rendertexture - function defined in my code elsewhere

    RenderTextureToTexture2D(ref rtMedium, ref textMedium);

//create the array to pass to OpenCV

   byte[] imageMedium = textMedium.EncodeToPNG();

I think I need some help or some guidance, Thank you.