Gets the number of black objects in a binary image.


#include "l_bitmap.h"

L_LTIMGCOR_API L_INT EXT_FUNCTION L_ObjectCounter(pBitmap, uCount, pCallback, pUserData, uFlags)



Pointer to the bitmap handle referencing the binary image.

L_UINT * uCount

Pointer to a variable to be updated with the number of black objects.


Optional callback function for additional processing.

If you do not provide a callback function, use NULL as the value of this parameter.

If you do provide a callback function, use the function pointer as the value of this parameter.

The callback function must adhere to the function prototype described in OBJECTCOUNTERCALLBACK function.

L_VOID * pUserData

Void pointer that you can use to pass one or more additional parameters that the callback function needs.

To use this feature, assign a value to a variable or create a structure that contains as many fields as you need. Then, in this parameter, pass the address of the variable or structure, casting it to L_VOID *. The callback function, which receives the address in its own pUserData parameter, can cast it to a pointer of the appropriate data type to access your variable or structure.

If the additional parameters are not needed, you can pass NULL in this parameter.

L_UINT32 uFlags

Reserved for future use. Must be 0.


Value Meaning
SUCCESS The function was successful.
< 1 An error occurred. Refer to Return Codes.


If white objects are on a black image, the colors in the specified bitmap must be inverted by calling L_InvertBitmap function.

This function only works on 1-bit images.

This function does not support 32-bit grayscale images. It returns the error code ERROR_GRAY32_UNSUPPORTED if a 32-bit grayscale image is passed to this function.

Required DLLs and Libraries


Win32, x64, Linux.

See Also




This example loads a bitmap and counts the objects in it and prints the parameters of the rectangle surrounding the region .

L_INT EXT_CALLBACK ObjectCounterExampleCB(L_RECT rect, L_INT** pObject, L_VOID* pUserData) 
   L_TCHAR szMsg[200]; 
   wsprintf(szMsg, TEXT("Region Bounds: left=%d, right=%d, top=%d, bottom=%d\n"), rect.left, rect.right, rect.top, rect.bottom); 
   _tprintf(_T("%s"), szMsg); 
   return SUCCESS; 
L_INT ObjectCounterExample(L_VOID) 
   L_INT          nRet; 
   BITMAPHANDLE   LeadBitmap; /*Bitmap handle to hold the loaded image*/ 
   L_UINT         uCount;     /*variable to hold the count*/ 
   /* Load the bitmap, keeping the bits per pixel of the file */ 
   nRet = L_LoadBitmap(MAKE_IMAGE_PATH(TEXT("clean.tif")), &LeadBitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGR, NULL, NULL); 
   if (nRet != SUCCESS) 
      return nRet; 
   if (LeadBitmap.BitsPerPixel != 1) 
      return ERROR_BITPERPIXEL; 
   //Apply Object Counter  
   nRet = L_ObjectCounter(&LeadBitmap, &uCount, ObjectCounterExampleCB, NULL, 0); 
   if (nRet != SUCCESS) 
      return nRet; 
   //free bitmap 
   if (LeadBitmap.Flags.Allocated) 
   return SUCCESS; 

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Raster Imaging C API Help

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.