Ask Your Question

Revision history [back]

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

int main()
{
    std::vector<cv::Mat> channels;

    cv::Mat img = cv::imread("15399391149075274.png", cv::IMREAD_UNCHANGED);

    if (img.channels() == 4)
    {
        cv::split(img, channels);               // seperate channels
        cv::Rect r = boundingRect(channels[3]); // find boundingRect of alpha channel
        cv::imwrite("result.png", img(r));
        printf("result.png saved!");
    }

    return 0;
}

result.png :

image description