Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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);
  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 mat.

if(dst.channels() == 4)
{

            std::vector<cv::Mat> matrixChannels;
            cv::split(dst,matrixChannels);

            image->Create(dst.cols,dst.rows,dst.data,matrixChannels[3].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)

I can't for the life of me achieve the correct data for the unsigned char *alpha parameter. I know for a fact CV::Mat dst has Alpha data, but I don't know how to get to it and use it where I want to use it.. Any ideas? I'm pulling my hair out here! :(

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);
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 mat.

if(dst.channels() == 4)
{

            std::vector<cv::Mat> matrixChannels;
            cv::split(dst,matrixChannels);

            image->Create(dst.cols,dst.rows,dst.data,matrixChannels[3].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)

I can't for the life of me achieve the correct data for the unsigned char *alpha parameter. I know for a fact CV::Mat dst has Alpha data, but I don't know how to get to it and use it where I want to use it.. Any ideas? I'm pulling my hair out here! :(

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 mat.

    cv::Mat dst2;
    cv::cvtColor( dst,dst2, CV_BGR2BGRA );

if(dst.channels() == 4)
{

         std::vector<cv::Mat> matrixChannels;
            cv::split(dst,matrixChannels);
cv::split(dst2,matrixChannels);

            image->Create(dst.cols,dst.rows,dst.data,matrixChannels[3].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)

I can't for the life EDIT: The above is the correct way of me achieve the correct data for the unsigned char *alpha parameter. I know for a fact CV::Mat dst has Alpha data, doing this. This is no longer a question, but I don't know how to get to it and use it where I want to use it.. Any ideas? I'm pulling my hair out here! :(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.

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 mat.

 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 RGBMartrix; 
    //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 dst2;
    cv::cvtColor( dst,dst2, CV_BGR2BGRA );

alpha = cv::Mat::zeros(dst.size(),CV8UC1); 
    int from_to[] = {3,0};
    cv::mixchannels(&dst,1,&alpha,1,from_to,1);
    if(dst.channels() == 4)
4) //if we have four channels then build the wximage.
    {

        std::vector<cv::Mat> matrixChannels;
        cv::split(dst2,matrixChannels);

            image->Create(dst.cols,dst.rows,dst.data,matrixChannels[3].data,false);
            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.

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 mat.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 RGBMartrix; 
    //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(),CV8UC1); 
    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.

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 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 RGBMartrix; 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(),CV8UC1); cv::Mat::zeros(dst.size(),CV_8UC1); 
    int from_to[] = {3,0};
    cv::mixchannels(&dst,1,&alpha,1,from_to,1);
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.

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 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.