# Thresholding for grayscaled images

**URL:** <https://forum.opencv.org/t/thresholding-for-grayscaled-images/7032>\
**Category:** Python\
**Tags:** numpy\
**Created:** [January 8, 2022, 3:10am UTC](https://forum.opencv.org/t/thresholding-for-grayscaled-images/7032 "2022-01-08T03:10:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![K\_Miura](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/k_miura/32/4172_2.png) [@K\_Miura](https://forum.opencv.org/u/K_Miura)\
**Post date:** [January 8, 2022, 3:10am UTC](https://forum.opencv.org/t/thresholding-for-grayscaled-images/7032/1 "2022-01-08T03:10:28Z")

</div>

Is there a way to make it so that below a certain threshold; everything becomes black, but above it; the whiteness stays the same as the original?

(For a grayscaled image)

---

<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:** [January 8, 2022, 3:30am UTC](https://forum.opencv.org/t/thresholding-for-grayscaled-images/7032/2 "2022-01-08T03:30:00Z")

</div>

all doable with numpy operations.

```python
mask = (img < threshold) # boolean array
img[mask] = 0 # indexing using boolean array

```

---

<div class="post-metadata">

**Author:** ![K\_Miura](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/k_miura/32/4172_2.png) [@K\_Miura](https://forum.opencv.org/u/K_Miura)\
**Post date:** [January 8, 2022, 3:43am UTC](https://forum.opencv.org/t/thresholding-for-grayscaled-images/7032/3 "2022-01-08T03:43:44Z")

</div>

So would that work by adding:

‘’’  
import numpy  
‘’’  
at the top?

---

<div class="post-metadata">

**Author:** ![K\_Miura](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/k_miura/32/4172_2.png) [@K\_Miura](https://forum.opencv.org/u/K_Miura)\
**Post date:** [January 8, 2022, 3:56am UTC](https://forum.opencv.org/t/thresholding-for-grayscaled-images/7032/4 "2022-01-08T03:56:09Z")

</div>

I’m not so familiar with numpy’s image processing, so could you explain how to implement it to some code?

Here’s what I am trying to use it for:

```auto
import cv2 as cv

    number = 0
    black_and_white = []
    while True:
        if number == 0:
                originalImage = cv.imread(f'/Users/Pictures/PythonScreenshot/Image.png')
        else:
                originalImage = cv.imread(f'/Users/Pictures/PythonScreenshot/Image{number}.png')

        try:
            grayImage = cv.cvtColor(originalImage, cv.COLOR_BGR2GRAY)
        except:
            break

        number = number + 1

        mask = (originalImage < 75)
        blackAndWhiteImage = originalImage[mask]

        #DELETE FROM
        cv.imshow('Black white image', blackAndWhiteImage)
        cv.imshow('Original image', originalImage)
        cv.imshow('Gray image', grayImage)

        cv.waitKey(0)
        cv.destroyAllWindows()
        #DELETE UNTIL

        black_and_white.append(blackAndWhiteImage)

```
