Ask Your Question
2

eliminating edges

asked 2016-02-26 13:46:51 -0600

xinyiman gravatar image

updated 2016-06-04 14:59:08 -0600

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...

edit retag flag offensive close merge delete

Comments

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

LBerger gravatar imageLBerger ( 2016-02-26 13:56:49 -0600 )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 ( 2016-02-26 14:24:59 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2016-02-26 15:25:03 -0600

updated 2016-02-26 17:57:41 -0600

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

edit flag offensive delete link more

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 ( 2016-02-26 15:36:56 -0600 )edit

what is your OpenCV version? try CV_REDUCE_AVG or simply 1

sturkmen gravatar imagesturkmen ( 2016-02-26 15:41:04 -0600 )edit

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

xinyiman gravatar imagexinyiman ( 2016-02-26 15:47:39 -0600 )edit

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

Tetragramm gravatar imageTetragramm ( 2016-02-26 15:57:44 -0600 )edit

please example?!

xinyiman gravatar imagexinyiman ( 2016-02-26 16:04:26 -0600 )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 ( 2016-02-26 16:05:10 -0600 )edit

I did some tests but do not understand how.

xinyiman gravatar imagexinyiman ( 2016-02-26 16:54:47 -0600 )edit

will you use countours of digits to recognize?

sturkmen gravatar imagesturkmen ( 2016-02-26 17:05:10 -0600 )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 ( 2016-02-28 08:04:55 -0600 )edit

i updated the answer. did you try edited code?

sturkmen gravatar imagesturkmen ( 2016-02-28 08:55:10 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-02-26 13:46:51 -0600

Seen: 1,633 times

Last updated: Feb 26 '16