我想跟踪图像的所有连接像素。我使用canny算法进行边缘检测,现在需要获得每个独特的形状。
这是我的以下代码
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);
我在我的程序的早期版本中使用它,即使它们没有连接而不是一个接一个,它也会不幸地检测到所有形状,现在我一直在尝试opencv并且想知道是否有功能或实现这可以做到甚至是非opencv实现或我可以使用的例子?为此目的,什么是简单的算法?