Loop error in segmentation project/OpenCV

Hello everyone. It's my first post in here.
I'm trying to create a program which will group objects in the picture. The result should be: all elements of the same type in the same color.

What I've made till now is:
-binarization (objects are black (255), background is white (0));
-I've created a function "f_check" which I think should be a next step - this function counts the area of every element (later I'm gonna color elements according to their areas)

The problem is with a loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(int j=200; j<(img->height)-200; j++)                                                       
{
        for(int i=200; i<(img->width)-200; i++)
                {
                area=0;
                pixel_value=cvGet2D(img_bin,j,i);
                if (pixel_value.val[0]=255)  
                   {
                   cvSet2D(img_bin,j,i,qq3);
                   area++;
                   f_check(j,i);
                   };
                };       
};


Every element here is declared. Dev-C++ compiler gives no errors. The program starts, works until he gets to that loop and quits. I start the loop from i,j=200 because the elements are only in the middle of an image. These elements are small (none of them is even close to the edge of an image).

The function f_check:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void f_check(int position2, int position1)
{
pixel_value=cvGet2D(img_bin,position2,position1+1);
if (pixel_value.val[0]=255) {
   cvSet2D(img_bin,position2,position1+1,qq3);
   f_check(position2,position1+1);
   area++;}
pixel_value=cvGet2D(img_bin,position2-1,position1);
if (pixel_value.val[0]=255) {
   cvSet2D(img_bin,position2-1,position1,qq3);
   f_check(position2-1,position1);
   area++;}
pixel_value=cvGet2D(img_bin,position2,position1-1);
if (pixel_value.val[0]=255) {
   cvSet2D(img_bin,position2,position1-1,qq3);
   f_check(position2,position1-1);
   area++;}
pixel_value=cvGet2D(img_bin,position2+1,position1);
if (pixel_value.val[0]=255) {
   cvSet2D(img_bin,position2+1,position1,qq3);
   f_check(position2+1,position1);
   area++;}
cout << "Area=" << area;     
};


This function checks the value of a pixel - if its 255 that means it's a pixel of an element. Then I check the neighborhood of that pixel using the same function (recursion). Every already counted pixel is changed to "qq3" which is 240 - I do it, because I don't want this pixel to be counted ever again.
The image (img, img_bin) is 1600x1200. Could you suggest me please what is wrong with that loop/function? I would be very grateful for any suggestions.
1
2
a=b; //assignation
a==b; //comparison 


About f_check: I would recommend to make it return the area, and avoid the global variable.
Last edited on
Thank you. I changed "=" for "==" in every "if". Unfortunately the program is still not working. If I delete this loop the program starts to work properly.
¿Are you checking for "out of bounds"? An easy way will be putting sentinel in the border

Also
1
2
3
4
5
if (pixel_value.val[0]=255) {
   cvSet2D(img_bin,position2,position1+1,qq3); //¿shouldn't this be done after the check?
   f_check(position2,position1+1);
   area++;}
//¿where do you set img_bin(position2, position1) ? (the current pixel) 
What do you mean by "sentinel in the border"?
This program doesn't want to work even if I tell him to check only some small area in the center of the image (far from edge of image).

I think that cvSet2D(img_bin,posit...) is in a right place. F_check checks only the neighbourhood of a pixel. That's why set2d should be before the next f_check will be performed. That's because the next f_check (for example position2+1,position1) could count the pixel (position2,position1) which already has been counted. I don't see any error in here.

The current pixel is checked in the main loop, not in the f_check function. f_check is performed only if the main loop found a 255-pixel. So the f_check itself doesn't care about current pixel, only about neighbouring pixels.

EDIT: I checked, it doesn't want to work even if I give it an empty image (all white). There are no elements, f_check shouldn't be performed at all. Still the program quits... It's as if I was telling him to check pixel that are outside the image...
Last edited on
What do you mean by "sentinel in the border"?
Skiena or Revilla wrote:
A sentinel is a guard element, implicitly checking that the program does not run beyond the bounds of the array without performing an explicit test.
(...)
Proper use of sentinels, and making sure that your array is a little larger than it presumably needs to be, can help avoid many boundary errors.

So your image will be greater and in the borders ([0,K] [K,0] [n,K] [K,n]) will have an element (like qq3).
When f_check encounter a sentinel it will not look at its neighbours.


Trace your code (with a debugger or allusive messages)
Sorry for so long silence. I've been working on this program. A lot of things changed, generaly it works but I still have some problems. I think I know what's wrong but I'm not sure if it can be fixed:
My f_check function is a recurrence function. It calls itself inside. But that "inside function" again calls itself and so many times. Is there a limit of number of times that it can be done? Because I think, that when I run the program, and the program finds big object - where every pixel of that object is a next recurrence - the program brokes. Is it possible?

@ne555: thanks for the sentinel idea. I used it and it helped :)
Topic archived. No new replies allowed.