Ask Your Question

Revision history [back]

my proposed solution is using the technique that explained at another question

i applied it only horizontally in the code below

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

using namespace cv;

int main( int argc, char** argv )
{
    Mat src = imread( argv[1] );
    if (src.empty())
        return -1;

    Mat gray,eroded,reduced_w;

    cvtColor( src, gray, CV_BGR2GRAY );
    erode( gray, eroded, Mat::ones(8,1,CV_8UC1) ); // change of kernel gives different results

    reduce( eroded, reduced_w, 1, REDUCE_AVG );

    for ( int i = 0; i < src.rows; i++)
    {
        if( reduced_w.at<uchar>(0,i) < 100)
        line(gray,Point(0,i),Point(gray.cols,i),Scalar(255,255,255),3);//try to change Line thickness
    }

    imshow("result", gray );
    waitKey(0);
    return 0;
}

Result image:

image description

there is many ways to solve this problem.

my proposed solution is using the technique technique that explained at another question

i applied it only horizontally in the code below

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

using namespace cv;

int main( int argc, char** argv )
{
    Mat src = imread( argv[1] );
    if (src.empty())
        return -1;

    Mat gray,eroded,reduced_w;

    cvtColor( src, gray, CV_BGR2GRAY );
    erode( gray, eroded, Mat::ones(8,1,CV_8UC1) ); // change of kernel gives different results

    reduce( eroded, reduced_w, 1, REDUCE_AVG );

    for ( int i = 0; i < src.rows; i++)
    {
        if( reduced_w.at<uchar>(0,i) < 100)
        line(gray,Point(0,i),Point(gray.cols,i),Scalar(255,255,255),3);//try to change Line thickness
    }

    imshow("result", gray );
    waitKey(0);
    return 0;
}

Result image:

image description

there is many ways to solve this problem.

my proposed solution is using the technique that explained at another question

i applied it only horizontally in the code below

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

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat src = imread( argv[1] );
    if (src.empty())
        return -1;

    Mat gray,eroded,reduced_w;

    cvtColor( src, gray, CV_BGR2GRAY );
    erode( gray, eroded, Mat::ones(8,1,CV_8UC1) ); // change of kernel gives different results

    reduce( eroded, reduced_w, 1, REDUCE_AVG CV_REDUCE_AVG );

    for ( int i = 0; i < src.rows; i++)
    {
        if( reduced_w.at<uchar>(0,i) < 100)
        line(gray,Point(0,i),Point(gray.cols,i),Scalar(255,255,255),3);//try to change Line thickness
    line( gray,Point(0,i),Point(gray.cols,i),Scalar(255,255,255),3);
    }

    vector< vector <Point> > contours;

    eroded = gray < 127;
    findContours( eroded, contours,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );

    eroded = Scalar(255);
    for( size_t i = 0; i< contours.size(); i++ )
    {
        Rect r = boundingRect(contours[i]);
        if((contourArea( contours[i] ) > 200) & r.x > 5 & r.x + r.width < gray.cols - 5 )
        {
            rectangle( eroded, Point(r.x,r.y), Point(r.x+r.width,r.y+r.height), Scalar(0), -1 );
            rectangle( src, Point(r.x,r.y), Point(r.x+r.width,r.y+r.height), Scalar(0,0,255), 2 );
        }
    }
    gray = gray + eroded;
    imshow("result", gray );
    imshow("color result", src );
    waitKey(0);
    return 0;
}

Result image:

image description

images:

image description - image description