# Segmentation of white object on white background

**URL:** <https://forum.opencv.org/t/segmentation-of-white-object-on-white-background/3886>\
**Category:** Python\
**Created:** [June 16, 2021, 9:03pm UTC](https://forum.opencv.org/t/segmentation-of-white-object-on-white-background/3886 "2021-06-16T21:03:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Houne](https://avatars.discourse-cdn.com/v4/letter/h/ba9def/32.png) [@Houne](https://forum.opencv.org/u/Houne)\
**Post date:** [June 16, 2021, 9:03pm UTC](https://forum.opencv.org/t/segmentation-of-white-object-on-white-background/3886/1 "2021-06-16T21:03:06Z")

</div>

Hello,

i have a problem with the segmentation of a picture.  
The background of this picture is kind of white and when i change everything to gray i have some similar gray values from the background and the objects and so I have problems with the threshhold. I’ve tried a lot, also with HSV, algorithms (watershed,…) and stuff like this. At the moment I am trying it with adaptive threshhold, afterwards I use a median filter and remove small white remaining points. So I have the best result so far.

 ![segmentation](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/3/351ab596f63efe1b7209fe3d1bd4e3ebf7ae0dab.png)  
But as you see do I have splitted contours.  
Now I want to ask if its possible to merge these contours and fill it afterwards or if you have a good solution.  
This is the code so far:

```auto
img = cv2.imread('ducks.jpg', 1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# Addaptive Gauss Threshholding
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
            cv2.THRESH_BINARY_INV,11,3)

# median filter
median_blur= cv2.medianBlur(thresh, 5)

# Remove small white point
nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(median_blur, None, None, None, 8, cv2.CV_32S)

#get CC_STAT_AREA component as stats[label, COLUMN] 
areas = stats[1:,cv2.CC_STAT_AREA]

result = np.zeros((labels.shape), np.uint8)

for i in range(0, nlabels - 1):
    if areas[i] >= 100: #keep
        result[labels == i + 1] = 255

cv2.imshow("Result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

```

I would be really glad for some help.

---

<div class="post-metadata">

**Author:** ![Houne](https://avatars.discourse-cdn.com/v4/letter/h/ba9def/32.png) [@Houne](https://forum.opencv.org/u/Houne)\
**Post date:** [June 16, 2021, 9:04pm UTC](https://forum.opencv.org/t/segmentation-of-white-object-on-white-background/3886/2 "2021-06-16T21:04:57Z")

</div>

This is the original picture:

 ![ducks](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/4/416eaed3773457d4dfd54aac7c24c1d66f944d71.jpeg)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 16, 2021, 11:52pm UTC](https://forum.opencv.org/t/segmentation-of-white-object-on-white-background/3886/3 "2021-06-16T23:52:20Z")

</div>

fix the camera’s exposure settings, take a blank picture, then take this picture, take pixel differences

or run a median of size ~200 on this, take differences (then use inRange to get a background mask, or do something else):

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/b/b878510b9b17a584e75f49126469a7f77c0f2a5f.png)

or throw deep learning at it. it’s good at these things.
