Extract connected pixels using the idea of Breadth-first search
I am trying to collect a set of pixels between two pixels:start and end point from a skeleton image . So the idea is : I start on a position in the image. I put that position in a list. Then in a loop,
- I get and remove the point at the beginning of the list and make it black in the current image so it won't be added again.
- I put that point in another list (this is the list that you want).I look at every point around the position and if it is not black, add it to the beginning of the list.
Then start the loop again.
I have develop a piece of code but the loop ends after adding four or five pixels ,before arriving to the end point
vector<Point> traceLine(Mat img , Point peak)
{
vector<Point> vect1;
vector<Point> vect2;
vect1.push_back(peak);
while(vect1.size() != 0)
{
Point p=vect1[0];
img.at<uchar>(p.x,p.y)=0;
vect1.erase(vect1.begin());
//cout<<vect1[1];
vect2.push_back(p);
vector<Point> vectN;
vectN=search8Neighbor(img,p);
cout<<vectN.size()<<endl;
if(vectN.size()!=0)
{
for(int i=0;i<vectN.size();i++)
vect1.push_back(vectN[i]);
}
}
return vect2;
}