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
This is what i get by saving the image from OpenCV
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.
First here it's an opencv forum (read faq) it is difficult to help you with third party lib or wrappers and c#.
I have tested :
result :
On my disk image saved is good. Imread is called decoder->readData( *data ); exactly as like imdecode. may be I'm wrong but I don't think problem is in opencv
Than you for your time and for your answer. I performed a similar test and the result agree with your test. I still wonder if there is something wrong in my c++ code in the way that I construct data_mat_medium from buffer and in the decoding process. Also I think it's unfair for the question to be downvoted for mentioning the C# side of the code. I present the c# side to show that just before sending the image, this image really had an alpha channel and I think that this is important. I can remove the unity and c# stuff from the question if it is better for everyone. I apologize again and thank you one more time.
Opencv is a large lib 20000 commits for 8 years.May be 30 tags since 2.2 version and last one is 3.2.0-rc. I have tested with 3.2.0-rc. If you tested too and found same result problem is not in opencv.
There is no problem to help you for your problem if you can give a piece code in c++ java or python and give OS and opencv version.
I found a solution by replacing the lines
with these
Thank you again for time and your interest.
Can you try without imdecode ?
I tried that but the third party application crashed. After debugging the DLL visual studio gave me this message
Unhandled exception at 0x00007ffa401dc58a (opencv_imgcodecs310d.dll) in Unity.exe: 0xC0000005: Access violation reading location 0x000000002476d000.
at
line 831 : memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
Try to do not use imdecode :
Sorry for the delay (too much paperwork), not using imdecode doesn't seem to work, here are 5 steps from the debugging process (it shows with imdecode - which works and without imdecode - which deosn't work with some info from the vs debugger) https://s30.postimg.org/j3hqd5601/Ste...https://s30.postimg.org/ndwe8qb35/Ste...https://s30.postimg.org/hr4mozxy9/Ste...https://s30.postimg.org/meaoqrlb5/Ste...https://s30.postimg.org/fp45aqzz5/Ste...
note at step4 the "prevAllocator=????" , it doesn't look right.
Something is wrong in your code. Try this small sample :
You example should work like my example