I will collect a dataset consisting of blackberries with and without defects. For this purpose, before I venture into this business I want to make sure that I can detect and extract blackberries from an image.

So I have this image

enter image description here

With the otsu method, morphology opening and closing I'm able to obtain a masked version of the image: enter image description here

Now I want to obtain a bounding box or extract every blackberry from the image. What tool should I use? A trained neural network? Are there other non neural network technique for this purupose?

Results:Thanks to Kevin M

enter image description here enter image description here

  • $\begingroup$ Search the documentation of your image processing library of choice for the term "connected component labelling". $\endgroup$
    – cdalitz
    Jan 18 at 14:44

1 Answer 1


In Python, there's a function called regionprops under the sci-kit learn package. You first have to use skimage.measure.label on your image before inputting it into regionprops. The result after regionprops contains a list of regions and bounding box coordinates for each region in your image (after using .bbox).

Example (taken and edited from reginoprops documentation):

from skimage import data, util
from skimage.measure import label, regionprops
img = util.img_as_ubyte(data.coins()) > 110
label_img = label(img, connectivity=img.ndim)
props = regionprops(label_img)

>>> (0, 0, 102, 376)

regionprops also exists in MATLAB, though there's some slight differences. There are many different versions of the regionprops algorithm. Hope this helps!

  • $\begingroup$ It worked. Thank you! $\endgroup$
    – Girigio
    Jan 17 at 18:57
  • $\begingroup$ Your welcome! Since it helped, was wondering if you could select my answer as accepted? Helps in the future I believe on both our ends. $\endgroup$
    – Kevin M
    Jan 17 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.