# to detect small foreign objects

**URL:** <https://forums.openmv.io/t/to-detect-small-foreign-objects/512>\
**Category:** OpenMV Boards\
**Created:** [December 5, 2017, 8:06am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512 "2017-12-05T08:06:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bennyedward](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bennyedward](https://forums.openmv.io/u/bennyedward)\
**Post date:** [December 5, 2017, 8:06am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/1 "2017-12-05T08:06:39Z")

</div>

My application is to detect small foreign objects (please see attached photo) such as cloth fibers that stick between (shiny) transparent film and white synthetic paper surface.  
Can you suggest how to use OpenMV Cam for this application?  
Is there any recommended microscope lens for use with OpenMV Cam?  
Thank you.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [December 5, 2017, 8:27am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/2 "2017-12-05T08:27:28Z")

</div>

Hi, there’s no photo attached.

Anyway, you’ll want to use the img.find\_blobs() method to detect things like this. You’d use the inverse=True argument and search for everything that isn’t the color of the paper you’re looking at.

As for a microscope lens… the telephoto lens offer 4x zoom. Do you need more than that? Here’s an 8x zoom lens.

[https://www.aliexpress.com/store/product/8x-25mm-CCTV-IR-Board-Lens-view-70m-12-degrees-M12-for-both-1-3-and/404325\_1573699886.html](https://www.aliexpress.com/store/product/8x-25mm-CCTV-IR-Board-Lens-view-70m-12-degrees-M12-for-both-1-3-and/404325_1573699886.html)

Just google for M12 lenses and whatever zoom you need.

---

<div class="post-metadata">

**Author:** ![bennyedward](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bennyedward](https://forums.openmv.io/u/bennyedward)\
**Post date:** [December 5, 2017, 9:24am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/3 "2017-12-05T09:24:14Z")

</div>

Very sorry forgot to attach a photo. Kindly refer to the attachment in this post.  
Thank you very much for your advice.

 ![IMG-20171205-WA0006.jpg](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/7130a44e548975ceeb5b31fcc844fdbb469f7450.jpeg)  
 ![IMG-20171205-WA0003.jpg](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/f3e64d73ffd11b8c87bd1bf1c15163b50f441fb7.jpeg)

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [December 5, 2017, 4:10pm UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/4 "2017-12-05T16:10:51Z")

</div>

Hi, can you run the hello world demo with VGA resolution and tell me what the camera sees? That object is easy to detect… So, you just need a good about of zoom.

I just bought one of those lenses and if it works out and I can find a source I may stock it. 8x zoom is cool. I’ll call it the microscope lens.

---

<div class="post-metadata">

**Author:** ![bennyedward](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bennyedward](https://forums.openmv.io/u/bennyedward)\
**Post date:** [December 8, 2017, 8:00am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/5 "2017-12-08T08:00:08Z")

</div>

Hi,  
This is what the camera sees from a distance of less than 10 millimeters and from an angle of about 45 degrees.  
1st object is human eyelashes (3.5 millimeter length) & 2nd object is cloth fiber (1 millimeter length).

 ![sample12_2017124_line18_cam_nodigzoom_nomacro_lighting_190_255.PNG](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/1e1ae011168f893b35cdde272d6e154053b93621.png)  
 ![sample23_2017124_line6_cam_nodigzoom_nomacro_lighting_198_255.PNG](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/08d4f422bc6ab8cf9b7797f6bdd347f780b74301.png)

---

<div class="post-metadata">

**Author:** ![bennyedward](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bennyedward](https://forums.openmv.io/u/bennyedward)\
**Post date:** [December 8, 2017, 8:01am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/6 "2017-12-08T08:01:53Z")

</div>

Sorry.  
1st object is cloth fiber (1 millimeter length) & 2nd object is human eyelashes (3.5 millimeter length)

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [December 9, 2017, 7:40pm UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/7 "2017-12-09T19:40:02Z")

</div>

Hi, sorry for not getting back to you quickly.

For your application you really need adaptive thresholding. We don’t have that right now but it’s on my giant to do list.

Anyway, in the mean time you can get around the issue with the giant blob on the bottom by filtering out blobs which have a high pixel count.

So, call the find\_blobs() method on the image using the color thresholds you figured out. Then, filter out all blobs with more than 500 pixels and less than 100 pixels. This should just tell you about blobs that are effectively dirt.

Also, you may wish to make the lighting flatter. Finally, use histeq() to pump the contrast up the image to make setting the color thresholds easier.

---

<div class="post-metadata">

**Author:** ![bennyedward](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bennyedward](https://forums.openmv.io/u/bennyedward)\
**Post date:** [December 10, 2017, 1:22am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/8 "2017-12-10T01:22:04Z")

</div>

Hi,  
Thanks for your guidance and strong support.  
Please advice further, where should define two pixels values, i.e. 500 and 100 in the instruction below?

image.find\_blobs(thresholds[, roi=Auto, x\_stride=2, y\_stride=1, invert=False, area\_threshold=10, pixels\_threshold=10, merge=False, margin=0, threshold\_cb=None, merge\_cb=None])

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [December 10, 2017, 1:37am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/9 "2017-12-10T01:37:58Z")

</div>

You have to do it on the returned objects:

```python
blobs = img.find_blobs(...)
out_blobs = []
    for b in out_blobs:
        if((100 <= b.pixels()) and (b.pixels() < 500)):
            out_blobs.append(b)

```

See the documentation on the blob object for more information.

---

<div class="post-metadata">

**Author:** ![bennyedward](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bennyedward](https://forums.openmv.io/u/bennyedward)\
**Post date:** [December 10, 2017, 1:39am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/10 "2017-12-10T01:39:05Z")

</div>

Is it by calling “image.find\_blobs” twice with separate thresholds? The first is for the 500 pixels and the second is for the 100 pixels?  
Sorry to bother you with this kind of question.

---

<div class="post-metadata">

**Author:** ![bennyedward](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bennyedward](https://forums.openmv.io/u/bennyedward)\
**Post date:** [December 10, 2017, 1:41am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/11 "2017-12-10T01:41:02Z")

</div>

Thank you for your latest answer  
Please ignore my last question, as it has been answered.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [December 10, 2017, 1:44am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/12 "2017-12-10T01:44:38Z")

</div>

There’s a typo in my code:

```python
blobs = img.find_blobs(...)
out_blobs = []
    for b in blobs:
        if((100 <= b.pixels()) and (b.pixels() < 500)):
            out_blobs.append(b)

```

---

<div class="post-metadata">

**Author:** ![bennyedward](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bennyedward](https://forums.openmv.io/u/bennyedward)\
**Post date:** [December 12, 2017, 10:03am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/13 "2017-12-12T10:03:42Z")

</div>

Hi,  
Attached is the result of camera detection of 4 objects.  
The camera can detect 1.5 mm dirt (fb1), with and without additional lighting.  
But it can not detect 1 mm cloth fiber (fb2) and 3.5 mm human eyelashes (fb3).  
Can you suggest another method to detect fb2 and fb3?  
How to reduce the influence of environmental lighting on detection?  
Thanks.

 ![fb3.jpg](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/2480273554287cfa0940a6fde1d32946457632ef.jpeg)  
 ![fb2.jpg](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/5b2a51e2c658315bd2c0805444085e4569ad93fb.jpeg)  
 ![fb1.jpg](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/bb6c3613bbab4ad363b41ca009c0cb3ab35755b7.jpeg)

---

<div class="post-metadata">

**Author:** ![bennyedward](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bennyedward](https://forums.openmv.io/u/bennyedward)\
**Post date:** [December 12, 2017, 12:17pm UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/14 "2017-12-12T12:17:15Z")

</div>

Sorry, previous post is the detection result of 3 objects, not 4.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [December 12, 2017, 4:59pm UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/15 "2017-12-12T16:59:05Z")

</div>

Hi, you really need to improve the lighting in the scene. I can see the cameras shadow in the image…

If you’re serious about this application you need to mount the camera to a jig where it’s stable, then add flood lights around it that evenly illuminate the scene.

An evenly illuminated scene should not have the shadow ring you see around the edge of the image. Nor the cameras shadow in the image. You have to fix these things because the camera is effectively just looking for dark spots which shadows will look like.

If you look at professional computer vision systems like a pick and place machine you’ll notice they use giant high powered flood lights to remove all shadows. This is want you need to do. Note that the lighting should not cast more shadows, so, it needs to be diffused.

---

<div class="post-metadata">

**Author:** ![bennyedward](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bennyedward](https://forums.openmv.io/u/bennyedward)\
**Post date:** [December 13, 2017, 4:17am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/16 "2017-12-13T04:17:55Z")

</div>

Hi,  
Thank you very much for your suggestion.  
I will try to improve the lighting as you suggest.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [December 27, 2017, 9:41pm UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/17 "2017-12-27T21:41:53Z")

</div>

Hi, the next version of the firmware will feature adaptive thresholding to deal with the issue you’re having. This will pick out the hair and just that very easily without any effort or additional lighting control.

Let me know if you need the feature now and I can make a firmware image available to you.

---

<div class="post-metadata">

**Author:** ![bennyedward](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bennyedward](https://forums.openmv.io/u/bennyedward)\
**Post date:** [December 28, 2017, 10:09am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/18 "2017-12-28T10:09:51Z")

</div>

Hi,  
Thank you very much for the good news and for providing the required features for our application.  
Yes, I need the feature now and please make a firmware image available to me.  
Thank you.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [December 28, 2017, 6:41pm UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/19 "2017-12-28T18:41:16Z")

</div>

Hi, I’ve attached the firmware and an example script. The new method solves your problems for the first set of images you presented to me. As for the second set… no method will clean that much noise up in the image. Adaptive threshold just solves the problem of making light shadows not an issue.  
[mean\_adaptive\_threshold\_filter.zip](https://forums.openmv.io/uploads/short-url/tSLhDCUCPM6rHiH6VK0l23uo7LO.zip) (1.04 MB)

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [December 29, 2017, 3:15am UTC](https://forums.openmv.io/t/to-detect-small-foreign-objects/512/20 "2017-12-29T03:15:53Z")

</div>

Hi,

1. The algorithm decides to make the output pixel white or not based on this logic:

pixel\_area\_average(x,y) - offset \< pixel\_value(x,y)

Where the area average is the average of all pixels around that pixel. Basically, if the average minus the offset is less than the value of the pixel then the output is a 1. So, changing the offset value just adjusts how the output is generated.

In general, the algorithm tends to mark areas where there’s a sudden change. The offset value allows you to control exactly how much of a sudden change you threshold.

1. It looks like you deleted your question while I was writing…

[Next page](https://forums.openmv.io/t/to-detect-small-foreign-objects/512.md?page=2)
