Ask Your Question
1

Fastest way to apply alpha channel to image

asked 2014-04-02 12:48:35 -0600

B. Bogart gravatar image

updated 2020-10-24 02:17:17 -0600

I have a list of instances that contain a cv::Mat image and a cv::Mat alpha/mask channel. These are separate Mats because they are generated according to different processes.

Before I render (in GL) I need to convert the RGB image and mask images into an RGBA image. Here is my current function:

void percepUnit::applyAlpha() {

    int x,y,w,h;
    /*vector<cv::Mat> channels;
    if (image.rows == mask.rows and image.cols == mask.cols) {
        cv::split(image,channels); // break image into channels
        channels.push_back(mask); // append alpha channel

        cv::merge(channels,alphaImage); // combine channels
    }*/

    // Avoid merge
    cv::Mat src[] = {this->image, this->mask};
    int from_to[] = {0,0, 1,1, 2,2, 3,3};
    this->alphaImage = Mat(image.rows, image.cols, CV_8UC4);
    cv::mixChannels(src, 2, &(this->alphaImage), 1, from_to, 4); // &(*alphaImage)?
}

I just loop through my list of instances and applyAlpha() before rendering. I had to increase the resolution of the Mats to work around an opencv bug to 1280x720, and now this function uses a lot of time (about half the cycles of the whole program).

Is there a way I can speed up this operation?

The list contains a few thousand instances, so it seems some parallelism could help; I have 4 cores and a fast GPU (GeForce 780).

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
4

answered 2014-04-10 11:50:12 -0600

B. Bogart gravatar image

updated 2014-04-10 11:51:55 -0600

I ended up doing split / merge on the GPU:

void percepUnit::applyAlpha() {

    cv::gpu::GpuMat tmpImage, tmpMask, tmpAlphaImage;
    std::vector<cv::gpu::GpuMat> channels;

    tmpImage.upload(this->image);
    tmpMask.upload(this->mask);

    cv::gpu::split(tmpImage,channels); // break image into channels
    channels.push_back(tmpMask); // append alpha channel
    cv::gpu::merge(channels,tmpAlphaImage); // combine channels

    tmpAlphaImage.download(this->alphaImage);

    tmpAlphaImage.release();
    tmpImage.release();
    tmpMask.release();

    channels[0].release();
    channels[1].release();
    channels[2].release();
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-04-02 12:48:35 -0600

Seen: 12,269 times

Last updated: Apr 10 '14