How to obtain Alpha from cv::mat (For wxWidgets::wxImage)
The following code is not the entire block of code , instead it is the important pieces needed to get my question across. In short: I am asking how to receive the Alpha data from CV::Mat so that I may pass it into a parameter requesting an unsigned char* as Alpha.
in a scaling function, I have a cv::mat(Destination) that is built as so:
cvImage = convertWxToMatWithAlpha(image); //(cvImage is a CV::Mat)
wxData = (byte*) malloc(width * height * 4);
dst = cv::Mat(height, width, CV_8UC4, wxData);
I then re-size the image and save the results in dst which is a CV::Mat
cv::resize(cvImage,dst, cv::Size(width,height), 0, 0, CV_INTER_LINEAR);
I now need to create a wxImage with alpha using the dst(Be sure this is created using convertWxToMatWithAlpha, this is a function that is defined below in one of the answers.) mat. (Thanks pklab !)
CV_Assert(dst.type() == CV_8UC4); //Throw an error if this is false.
//Create a matrix which we will store the data in RBG format.
cv::mat RGBMatrix;
//Convert the dst(BGRA) to RBG and store it in RBGMatrix.
cv::cvtColor( dst,RGBMatrix, CV_BGRA2RGB );
//Create an alpha matrix which we will mix into the RBGMatrix.
cv::Mat alpha = cv::Mat::zeros(dst.size(),CV_8UC1);
int from_to[] = {3,0};
cv::mixChannels(&dst,1,&alpha,1,from_to,1);
if(dst.channels() == 4) //if we have four channels then build the wximage.
{
image->Create(dst.cols,dst.rows,RGBMatrix.data,alpha.data,false);
return true;
}
The constructor for wxImage I am using is specified as:
wxImage(int width, int height, unsigned char *data, unsigned char *alpha, bool static_data=false)
EDIT: The above is the correct way of doing this. This is no longer a question, but now just a "how to". I hope this helps people in the future on the internet find a solution to this problem. Also take note of the answers given below this question.
You can have an example here After for alpha channel you can use addWeighted function
I'm sorry but I don't really follow (Or understand the language.) I tried to re-read that function as much as possible to attempt and understand it, and I can see the case CV_32F: containing an if/else condition with the else case containing code being done on four pointers instead of three like all the others. So I assume that one specifically is dealing with Alpha? Even so, I have no clue really what's going on in this function.
In png color image with alpha channels have got 4 plane. (0 to 3) Alpha channels is in 3. You can split your Mat im like this
in plane[3] you have got alpha channel. After in wxwidgets constructor you can use this plane. You can forget my code because there is many case.
In your line
you can have memory management problem. Second you should check if matrixChannels[3].isContinuous is true. After if you want to use alpha channel you can read this post. In wxWidgets you can use alpha channels with wxBufferedPaintDC
You're still not getting through to me. You just did exactly as I have already done. But I did get new information out of that such as calling .isContinuous on matrixChannels[3]. The result is true. Is this a problem if it is true?
What is your problem? Is it but I don't know how to get to it and use it where I want to use it Have you try save data using wxWidgets and retrieve alpha with an another software like in this post and this one ?
Well, It appears the output from this function goes into a different object named wxMemoryDC which does not play well with alpha at all, causing me to indirectly assume my code that builds an alpha channel into a new image was broken, when in actuality, that was not the case. Sorry! haha.