# Ignoring Background in Binarization

**URL:** <https://forum.opencv.org/t/ignoring-background-in-binarization/17725>\
**Category:** Python\
**Tags:** imgproc\
**Created:** [May 23, 2024, 9:38am UTC](https://forum.opencv.org/t/ignoring-background-in-binarization/17725 "2024-05-23T09:38:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![3b017033](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/3b017033/32/10117_2.png) [@3b017033](https://forum.opencv.org/u/3b017033)\
**Post date:** [May 23, 2024, 9:38am UTC](https://forum.opencv.org/t/ignoring-background-in-binarization/17725/1 "2024-05-23T09:38:40Z")

</div>

I’m trying to convert this grayscale image into a binary image. I’m currently using adaptive binarization, but it’s creating outlines around the edges. How can I avoid this issue?

```auto
img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

```

 ![Origin](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/8/8ff63d77d3205226b18078a844e164edb375a7f4.jpeg) ![Result](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/1/1585a480afca5fee661f2505fecce837bef80e22.png)

---

<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:** [May 23, 2024, 11:20am UTC](https://forum.opencv.org/t/ignoring-background-in-binarization/17725/2 "2024-05-23T11:20:09Z")

</div>

a simple threshold() call can’t do this. I suspect you’re applying more than a threshold() to the picture.

[MRE](https://stackoverflow.com/help/minimal-reproducible-example) please.

also: one of the pictures is a cropped screenshot. don’t do that. save the image itself, not a screenshot.

---

<div class="post-metadata">

**Author:** ![3b017033](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/3b017033/32/10117_2.png) [@3b017033](https://forum.opencv.org/u/3b017033)\
**Post date:** [May 23, 2024, 11:59am UTC](https://forum.opencv.org/t/ignoring-background-in-binarization/17725/3 "2024-05-23T11:59:15Z")

</div>

Sorry about that, I’ll add a minimal, reproducible example below.

 ![gray](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/9/9e1ddc52fba104f8080e37104fbd04467ca9ebbd.jpeg)  
Here’s the code and results I used after converting the grayscale image.

```auto
img = cv2.GaussianBlur(img, (11, 11), 0)
kernel = np.ones((11, 11), np.uint8)
erode = cv2.erode(img, kernel, iterations=1)
img = cv2.absdiff(img, erode)
img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
result = img

```

 ![result](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/6f798395767dc8d94667b1bfa7e23e5f413dda57.png)

---

<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:** [May 23, 2024, 12:13pm UTC](https://forum.opencv.org/t/ignoring-background-in-binarization/17725/4 "2024-05-23T12:13:07Z")

</div>

> [@3b017033](#):
>
> `cv2.absdiff(img, erode)`

that’s where that comes from.

that operation is literally how you get outlines/borders with morphology operations.

---

<div class="post-metadata">

**Author:** ![3b017033](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/3b017033/32/10117_2.png) [@3b017033](https://forum.opencv.org/u/3b017033)\
**Post date:** [May 24, 2024, 8:30am UTC](https://forum.opencv.org/t/ignoring-background-in-binarization/17725/5 "2024-05-24T08:30:35Z")

</div>

Oh yes, I forgot to check that part of the code. Replacing the erosion operation with an opening operation yielded the desired result.

```auto
img = cv2.GaussianBlur(img, (11, 11), 0)
kernel = np.ones((11, 11), np.uint8)
morph_open = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel, iterations=1)
img = cv2.absdiff(img, morph_open)
img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

```
