First time here? Check out the FAQ!

Ask Your Question
2

eliminating edges

asked Feb 26 '16

xinyiman gravatar image

updated Jun 4 '16

Hello guys, who tells me how to remove the edges and the center line? So as to have only the characters?

Thank you

https://03054610326450256607.googlegr...

Preview: (hide)

Comments

Look for erode and dilate. May be you can use this post

LBerger gravatar imageLBerger (Feb 26 '16)edit

I would use Erode and Dilate to remove the little bits around the edge, then shrink your bounding box to the white pixels only, which should get rid of the outside edge. Then remove any row that has no white in it to get rid of the center line. Then dilate again to get any bits that didn't get removed because of rotation or whatever.

Tetragramm gravatar imageTetragramm (Feb 26 '16)edit

1 answer

Sort by » oldest newest most voted
1

answered Feb 26 '16

updated Feb 26 '16

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, 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);
    }

    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 images:

image description - image description

Preview: (hide)

Comments

pi@raspberrypi:~/app $ g++ linee.c++ -o linee -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect linee.c++: In function ‘int main(int, char**)’: linee.c++:17:35: error: ‘REDUCE_AVG’ was not declared in this scope reduce( eroded, reduced_w, 1, REDUCE_AVG );

xinyiman gravatar imagexinyiman (Feb 26 '16)edit

what is your OpenCV version? try CV_REDUCE_AVG or simply 1

sturkmen gravatar imagesturkmen (Feb 26 '16)edit

ok , with CV_REDUCE_AVG it's ok. But to remove the vertical line?!

xinyiman gravatar imagexinyiman (Feb 26 '16)edit

Switch the rows and cols in the Mat::ones() and change the direction of the reduce.

Tetragramm gravatar imageTetragramm (Feb 26 '16)edit

please example?!

xinyiman gravatar imagexinyiman (Feb 26 '16)edit

you can apply the technique also vertically see here. try yourself first, if you can't i will update the answer later.

sturkmen gravatar imagesturkmen (Feb 26 '16)edit

I did some tests but do not understand how.

xinyiman gravatar imagexinyiman (Feb 26 '16)edit

will you use countours of digits to recognize?

sturkmen gravatar imagesturkmen (Feb 26 '16)edit

@sturkmen: I do not understand what you mean. @Tetragramm: I reversed as you say. From Mat::ones(8,1,CV_8UC1) to Mat::ones(1,8,????) but I do not know what to put in the place of CV_8UC1

xinyiman gravatar imagexinyiman (Feb 28 '16)edit

i updated the answer. did you try edited code?

sturkmen gravatar imagesturkmen (Feb 28 '16)edit

Question Tools

2 followers

Stats

Asked: Feb 26 '16

Seen: 1,688 times

Last updated: Feb 26 '16