First time here? Check out the FAQ!

Ask Your Question
0

How to join broken edges from Canny

asked Apr 13 '18

open_ranger gravatar image

Canny edge detection tends to leave small unconnected section between edges (usually just 1 pixel wide) is the any developed method in openCV to join those broken pieces? open and close method doesnt work very good if the kernel is too small it wont join all the section if too large it leave small truncks instead to skiny lines. Any ideas? image description

Preview: (hide)

Comments

1

Try the erode and dilate functions: https://docs.opencv.org/trunk/d9/d61/...

sjhalayka gravatar imagesjhalayka (Apr 13 '18)edit

I think morphological operators are still your choice, but you will need to define a kernel that works specifically well for your application. Thats the biggest challence, instead of using default square kernels for example.

StevenPuttemans gravatar imageStevenPuttemans (Apr 17 '18)edit

1 answer

Sort by » oldest newest most voted
3

answered Apr 18 '18

open_ranger gravatar image
void joinEdges(Mat input){
int width=input.cols;
int height=input.rows;
const int dx[] = {  1, 1, 0, -1, -1, -1, 0, +1, 1 };
const int dy[] = {  0, 1, 1,  1,  0, -1,-1, -1, 0 };
const int d2x[] = {  2, 2, 2, 1, 0,-1,-2,-2,-2,-2,-2,-1, 0, 1, 2, 2 };
const int d2y[] = {  0, 1, 2, 2, 2, 2, 2, 1, 0,-1,-2,-2,-2,-2,-2,-1 };
const int d3x[] = {  3,3,3,3,2,1,0,-1,-2,-3,-3,-3,-3,-3,-3,-3,-2,-1,0,1,2,3,3,3 };
const int d3y[] = {  0,1,2,3,3,3,3,3,3,3,2,1,0,-1,-2,-3,-3,-3,-3,-3,-3,-3,-2,-1 };

for(int i=0;i<input.rows;i++)
for(int j=0;j<input.cols;j++){

    uchar one_step,two_step,three_step;
    vector<int> connection;
    if(input.at<uchar>(i, j)!=0){
        for (int dir = 0; dir < 8; ++dir) {
            int tmp_x = j + dx[dir];
            int tmp_y = i + dy[dir];
            if(tmp_x < 0 || tmp_x >= width || tmp_y < 0 || tmp_y >= height ) one_step=0;//treat outside image as 0
            else one_step=input.at<uchar>(tmp_y, tmp_x);

            if(one_step!=0) connection.push_back(dir);
            }

      if(connection.size()<3 && connection.size()!=0) {
         int direction1=connection.front();
         int direction2=connection.back();
         if(direction2-direction1<=1||direction2-direction1==7){
             int start_direction=(direction2+2)*3;
             int end_direction=(direction1+6)*3;
             for(int dir2=start_direction;dir2<end_direction;dir2++){//dir2 is between 6~39
                 int tmp_1x = j + dx [dir2%24/3];
                 int tmp_1y = i + dy [dir2%24/3];
                 int tmp_2x = j + d2x[dir2%24*2/3];
                 int tmp_2y = i + d2y[dir2%24*2/3];
                 int tmp_3x = j + d3x[dir2%24];
                 int tmp_3y = i + d3y[dir2%24];
                 if(tmp_2x < 0 || tmp_2x >= width || tmp_2y < 0 || tmp_2y >= height ) two_step=0;//treat outside image as 0
                 else two_step=input.at<uchar>(tmp_2y, tmp_2x);
                 if(tmp_3x < 0 || tmp_3x >= width || tmp_3y < 0 || tmp_3y >= height ) three_step=0;//treat outside image as 0
                 else three_step=input.at<uchar>(tmp_3y, tmp_3x);
                 if (two_step!=0){
                     input.at<uchar>(tmp_1y, tmp_1x)=255;
                     break;
                 }else if (three_step!=0) {
                     input.at<uchar>(tmp_1y, tmp_1x)=255;
                     input.at<uchar>(tmp_2y, tmp_2x)=255;
                     break;

                 }
             }
        }
      }
    }
}

}

Just developed a piece of code to do that job,and look the result. image description Most of the loose bits are closed quite well expect for those still far from each other.Should definately keep this in the toolbox

Preview: (hide)

Comments

Nice work! But why there is a white slim contour in the down-right part of the result image?

moHe gravatar imagemoHe (Apr 18 '18)edit

I only made to look 3 steps ahead anything far away still left unconnected

open_ranger gravatar imageopen_ranger (Apr 18 '18)edit

that thin circle is just the mouse cursor from the photo viewing software

open_ranger gravatar imageopen_ranger (Apr 18 '18)edit

Question Tools

3 followers

Stats

Asked: Apr 13 '18

Seen: 3,562 times

Last updated: Apr 17 '18