Ask Your Question

Revision history [back]

you can find a solution related to your question here

also i tried to solve by another way.here is my trial C++ code ( i think you can convert it to JAVA ) if you need some explanation to understand the algorithm you can ask.

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

using namespace cv;
using namespace std;

int main( int, char** argv )
{
    Mat src = imread( argv[1] );
    Mat gray;

    int threshold = 12;

    cvtColor(src, gray, COLOR_BGR2GRAY );
    blur(gray, gray, Size(3, 3));
    erode(gray, gray, Mat());
    Canny(gray, gray, threshold, threshold * 3, 3, true);

    vector<vector<Point> > contours;

    findContours(gray, contours,
                 RETR_EXTERNAL,
                 CHAIN_APPROX_SIMPLE);

    gray = 0;
    for( size_t i = 0; i< contours.size(); i++ )
    {

        Rect rect = boundingRect(contours[i]);
        if(rect.width > 10 | rect.height > 10 )
            rectangle(gray, rect, Scalar(255), -1 );
    }
    erode(gray, gray, Mat());

    findContours(gray, contours,
                 RETR_EXTERNAL,
                 CHAIN_APPROX_SIMPLE);

    Rect big;
    for( size_t i = 0; i< contours.size(); i++ )
    {
        if( contourArea(contours[i]) > src.cols * src.rows / 8)
        {
            Rect rect = boundingRect(contours[i]);
            rectangle(src, rect, Scalar(255,0,0), 2 );
        }
        else
        {
            if(big.height < 1 )
                big = boundingRect(contours[i]);
            big = big | boundingRect(contours[i]);
        }
    }

    rectangle(src, big, Scalar(0,255,0), 2 );

    imshow( "result", src );

    waitKey(0);
    return(0);
}

Result Image :

image description