我想跟踪图像的所有连接像素。我使用精明算法进行边缘检测,现在需要获取每个独特的形状。
这是我的以下代码
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实现或示例?为此目的的简单算法是什么?