Ask Your Question

Revision history [back]

you could try the code below, it works for the image you provided. you can ping me if you have other pictures that fails :)

Note:

roi = roi - Scalar(255, 255, 0);

in this line i used Scalar(255, 255, 0) produces a red color instead of white.

for black use Scalar(255, 255, 255) or calculate proper Scalar for other colors.

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat src = imread("e:/test/3903-lloyd-cw30-dont-worry-be-hoppy-W.png",-1);
    if (src.empty())
        return -1;

    Mat bw, bgra[4];
    split(src, bgra);

    vector<vector<Point> > contours;
    findContours(bgra[3], contours, RETR_TREE, CHAIN_APPROX_SIMPLE);

    Rect textRect;

    for (size_t i = 0; i < contours.size(); i++)
    {
        Rect r = boundingRect(contours[i]);
        Mat roi = src(r);
        Mat mask = bgra[3](r);

        Scalar smean = mean(roi,mask);

        if( (smean[0] > 240) & (smean[1] > 240) & (smean[2] > 240))
        {
            if (textRect == Rect())
                textRect = r;
            else
                textRect = textRect | r;
        }
    }

    Mat roi = src(textRect);
    roi = roi - Scalar(255, 255, 0);
    imwrite("e:/test/src.png", src);
    return 0;
}

you could try the code below, it works for the image you provided. you can ping me if you have other pictures that fails :)

Note:

roi = roi - Scalar(255, 255, 0);

in this line i used Scalar(255, 255, 0) produces a red color instead of white.

for black use Scalar(255, 255, 255) or calculate proper Scalar for other colors.

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat src = imread("e:/test/3903-lloyd-cw30-dont-worry-be-hoppy-W.png",-1);
    if (src.empty())
        return -1;

    Mat bw, bgra[4];
    split(src, bgra);

    vector<vector<Point> > contours;
    findContours(bgra[3], contours, RETR_TREE, CHAIN_APPROX_SIMPLE);

    Rect textRect;

    for (size_t i = 0; i < contours.size(); i++)
    {
        Rect r = boundingRect(contours[i]);
        Mat roi = src(r);
        Mat mask = bgra[3](r);

        Scalar smean = mean(roi,mask);

        if( (smean[0] > 240) & (smean[1] > 240) & (smean[2] > 240))
        {
            if (textRect == Rect())
                textRect = r;
            else
                textRect = textRect | r;
        }
    }

    Mat roi = src(textRect);
    roi = roi - Scalar(255, 255, 0);
0); // don't touch alpha channel otherwise you lose smooth alpha effects
    imwrite("e:/test/src.png", src);
    return 0;
}

EDIT1 : you can find a test python code here

you could try the code below, it works for the image you provided. you can ping me if you have other pictures that fails :)

Note:

roi = roi - Scalar(255, 255, 0);

in this line i used Scalar(255, 255, 0) produces a red color instead of white.

for black use Scalar(255, 255, 255) or calculate proper Scalar for other colors.

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat src = imread("e:/test/3903-lloyd-cw30-dont-worry-be-hoppy-W.png",-1);
    if (src.empty())
        return -1;

    Mat bw, bgra[4];
    split(src, bgra);

    vector<vector<Point> > contours;
    findContours(bgra[3], contours, RETR_TREE, CHAIN_APPROX_SIMPLE);

    Rect textRect;

    for (size_t i = 0; i < contours.size(); i++)
    {
        Rect r = boundingRect(contours[i]);
        Mat roi = src(r);
        Mat mask = bgra[3](r);

        Scalar smean = mean(roi,mask);

        if( (smean[0] > 240) & (smean[1] > 240) & (smean[2] > 240))
        {
            if (textRect == Rect())
                textRect = r;
            else
                textRect = textRect | r;
        }
    }

    Mat roi = src(textRect);
    roi = roi - Scalar(255, 255, 0); // don't touch alpha channel otherwise you lose smooth alpha effects
    imwrite("e:/test/src.png", src);
    return 0;
}