I need to take an image, that I removed everything except two colors, a red and a green, and label the blobs of color as discussed in the computer vision tutorial. The problem is that it sometimes splits a blob into two separate blobs due to the way I scan the pixels, left to right, top to bottom. I attached an image of the output. Each different shade is a different group.
Here is my code
for (i=0;i<(cinfo.output_height*row_stride);i+=3) //for each pixel
{
cur_row=i/row_stride;//row we are on
cur_pix=(i-(cur_row*row_stride));//pixel we are on * 3 because of RGB
if (imgdec[i+2]==1)//if marked as red object
{
if (cur_pix&&imgdec[i-1]==1&&imgdec[i-3])//top
{
imgdec[i]=imgdec[i-3];
}
else if (cur_row&&cur_pix&&imgdec[i-1-row_stride]==1&&imgdec[i-3-row_stride])//top left
{
imgdec[i]=imgdec[i-3-row_stride];
}
else if (cur_row&&imgdec[i+2-row_stride]==1&&imgdec[i-row_stride])//top center
{
imgdec[i]=imgdec[i-row_stride];
}
else if (cur_row&&cur_pix!=(row_stride-3)&&imgdec[i+5-row_stride]==1&&imgdec[i+3-row_stride])//top right
{
imgdec[i]=imgdec[i+3-row_stride];
}
else //new object
{
obj_r++;
imgdec[i]=obj_r;
}
}
else if (imgdec[i+2]==2)//if marked as green object
{
if (cur_pix&&imgdec[i-1]==2&&imgdec[i-2])
{
imgdec[i+1]=imgdec[i-2];
}
else if (cur_row&&cur_pix&&imgdec[i-1-row_stride]==2&&imgdec[i-2-row_stride])
{
imgdec[i+1]=imgdec[i-2-row_stride];
}
else if (cur_row&&imgdec[i+2-row_stride]==2&&imgdec[i+1-row_stride])
{
imgdec[i+1]=imgdec[i+1-row_stride];
}
else if (cur_row&&cur_pix!=(row_stride-3)&&imgdec[i+5-row_stride]==2&&imgdec[i+4-row_stride])
{
imgdec[i+1]=imgdec[i+4-row_stride];
}
else
{
obj_g++;
imgdec[i+1]=obj_g;
}
}
}
The problem is that it only checks the top and left pixels as the ones under it and to the right have not been grouped yet. What happens is if there is a corner that sticks out two pixels then it is assigned to a new group and everything after that is in that group.
I can't think of any way to make this work and still be fast. This is running on a Gumstix with 320*240 images. I need to get the best FPS as possible.
Also the attached images colors were enhanced so you could see the color of each group better as group 1 would be almost black otherwise (1/255).
Thanks for any help,
Justin