Blob detection code

Support and queries relating to all previous versions of ZoneMinder
Locked
hajo
Posts: 2
Joined: Tue Oct 25, 2005 6:45 pm

Blob detection code

Post by hajo »

Hi,
to understand how the motion detection works I have had a look at the source code, namely zm_zone.cpp. I don't understand the part which handles the detection of blobs so I would appreciate if anybody could give me the big picture what the code does.

I'm talking about the code after

Code: Select all

if ( check_method >= BLOBS )
(line 211)

Code: Select all

BlobStats *bsx, *bsy;
			BlobStats *bsm, *bss;
What does bsx, bsy, bsm, bss stand for? BlockStat...?

Code: Select all

pdiff = diff_image->Buffer( lo_x, y );
Now, pdiff contains a pointer to a pixel of the picture, right?

The two for-loops around this statement are iterating over the zone.

Code: Select all

if ( *pdiff == WHITE )
Checks whether the current pixel has changed (WHITE) or not (BLACK). The prior processing changed the picture so that there are only black and white pixels.

Code: Select all

lx = x>lo_x?*(pdiff-1):0;
ly = y>lo_y?*(pdiff-diff_image->Width()):0;
						if ( lx )
						{
							//printf( "Left neighbour is %d\n", lx );
							bsx = &blob_stats[lx];
							if ( ly )
							{
								//printf( "Top neighbour is %d\n", ly );
								bsy = &blob_stats[ly];
								if ( lx == ly )
								{
									//printf( "Matching neighbours, setting to %d\n", lx );
									// Add to the blob from the x side (either side really)
									*pdiff = lx;
									bsx->count++;
									if ( x > bsx->hi_x ) bsx->hi_x = x;
									if ( y > bsx->hi_y ) bsx->hi_y = y;
								}
								else
								{
So I don't understand this. lx contains the information if the left pixel is black or white and it is considered being black if it is not in the zone. That's the same with ly. But either lx=0x00 or lx=0xff since that's the definition of BLACK and WHITE.
I can't see what the check "if ( lx == ly )" does. There are the conditions "if (ly)" and "if (lx)" before so if the programm reaches this check it is every time true and the else clause is never executed. Or is it possible that lx is neither 0x00 nor 0xff?

Thanks,
hajo
User avatar
zoneminder
Site Admin
Posts: 5215
Joined: Wed Jul 09, 2003 2:07 pm
Location: Bristol, UK
Contact:

Post by zoneminder »

I agree it is a little hard to follow. Essentially lx and ly are the last x and y pixels in the x and y directions. If a pixel is found which is white (ie is set in the difference image) then it is compared with it's neighbours. If all neighbours are black then a new blob is allocated and the blob and pixel is coloured 0xfe. The next pixel is then examined and if it's neighbour is not white but non-black then the pixel is added to that blob.

Basically :-

If a pixel is black (0x00) it is ignored.
If a pixel is white (0xff) it is added to an existing blob (if it borders one) or used to start a new blob. It is then changed to it's blob colour of 0x01-0xfe.
If a pixel is between black and white it has already been examined and is a member of a blob that is coded in that shades.

As blobs of different shades are found to be touching the smaller of the two is reshaded to the colour of the larger one and the smaller blob shade is then released for further use.

The maximum number of blobs at any one time is therefore 254 (1-254) though the number of blobs found will start off by growing and then usually reduce as blobs are merged with each other.

Is that helpful?

Phil
hajo
Posts: 2
Joined: Tue Oct 25, 2005 6:45 pm

Helpful

Post by hajo »

That's it. What I didn't realise was that different blobs get different brightnesses in the picture. I was confused because of the lx == ly and hesitated to go beyond that line I didn't understand.

Thanks a lot!
hajo
User avatar
zoneminder
Site Admin
Posts: 5215
Joined: Wed Jul 09, 2003 2:07 pm
Location: Bristol, UK
Contact:

Post by zoneminder »

Yes, they are only 'virtual' brightnesses though and just used to identify the different blobs. They are never displayed with those values.

Phil
Locked