Reputation: 483
I want to trace all the connected pixels of an image. I used the canny algorithm for edge detection and now need to get each unique shape.
here is my code for the following
x_axis = marker_i; // x & y co-ordinates of the first detected pixels
y_axis = marker_j;
do
{
for (int i=0;i<8;i++){
if(i==0){x_axis = x_axis-1; y_axis = y_axis-1;} // each iteration checks one
if(i==1){x_axis = x_axis; y_axis = y_axis+1;} // of the 8 adjacent pixels
if(i==2){x_axis = x_axis; y_axis = y_axis+1;} // for a match
if(i==3){x_axis = x_axis+1; y_axis = y_axis-2;}
if(i==4){x_axis = x_axis; y_axis = y_axis+2;}
if(i==5){x_axis = x_axis+1; y_axis = y_axis-2;}
if(i==6){x_axis = x_axis; y_axis = y_axis+1;}
if(i==7){x_axis = x_axis; y_axis = y_axis+1;}
if(x_axis >= 1024 || y_axis >= 1280){ x_axis = 0; y_axis = 0;}
if(ch_rd == red [x_axis][y_axis] && ch_gr == green[x_axis][y_axis]
&& ch_bl == blue[x_axis][y_axis] && pixel_comp[x_axis][y_axis]==0){
map++;
pixel_comp[x_axis][y_axis] = 2; // marks that pixel as read
pixal[num][0] = x_axis;
pixal[num][1] = y_axis;
if (map == 1){ curs[0] = x_axis; curs[1] = y_axis;}
if (map > 1){ div[est] = num; est++;} // if there is more than one detection
num++; // adds remaining to an array div
}
}
if(map==0){
x_axis = pixal[div[prev]][0]; y_axis = pixal[div[prev]][1]; prev++;
// if nothing is detected goes to the last item from array div
}
else{
x_axis = curs[0]; y_axis = curs[1];
}
if(map==0 && prev>=est){ dead_end = 1;}
map = 0;
}
while (dead_end==0);
I was using this on a previous version of my program and it would unfortuantely detect all shapes even if they were not connected instead of one by one, now i've been trying opencv and was wondering if there was a function or implementation which does this or even an non-opencv implementation or example that i could use ? What is a simple algorithm for this purpose ?
Upvotes: 1
Views: 1073
Reputation: 38043
A good place to start is the Wikipedia article on Flood Fill . This has animations of the filling algorithm.
[You didn't ask for corrections to your program.]
Upvotes: 1