# Binarizing images of similar background and object color(Threshold)?

**URL:** <https://forum.opencv.org/t/binarizing-images-of-similar-background-and-object-color-threshold/7841>\
**Category:** Python\
**Tags:** imgproc\
**Created:** [March 7, 2022, 10:35am UTC](https://forum.opencv.org/t/binarizing-images-of-similar-background-and-object-color-threshold/7841 "2022-03-07T10:35:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MJ\_S](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mj_s/32/4676_2.png) [@MJ\_S](https://forum.opencv.org/u/MJ_S)\
**Post date:** [March 7, 2022, 10:35am UTC](https://forum.opencv.org/t/binarizing-images-of-similar-background-and-object-color-threshold/7841/1 "2022-03-07T10:35:04Z")

</div>

![pothole](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/3/3618e54248165d3a1014e1892c28a59209e50c3f.jpeg)

As shown in the example picture below, if the background and the color of the object to be obtained are similar, What would be good to use as a preprocessing before proceeding with binarization?

import cv2  
img = cv2.imread(‘image.jpg’, cv2.IMREAD\_GRAYSCALE)

ret, binary = cv2.threshold(img, 127, 255, cv2.THRESH\_BINARY)

cv2.imshow(‘img’, img)  
cv2.imshow(‘binary’, binary)  
cv2.waitKey(0)  
cv2.destroyAllWindows()

I used the code below, but it’s hard to separate.  
What is the pre-processing method for neat binarization?

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [March 7, 2022, 12:28pm UTC](https://forum.opencv.org/t/binarizing-images-of-similar-background-and-object-color-threshold/7841/2 "2022-03-07T12:28:58Z")

</div>

The difference between the two areas is not in **color** , but in **texture**. So you have to use a texture based segmentation on the image.

See this [old answer of mine](https://answers.opencv.org/question/187251/algorithm-recommendation-for-texture-analysissegmentation/) about some algorithms you can use in this case.
