Thresholding for grayscaled images

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)

all doable with numpy operations.

mask = (img < threshold) # boolean array
img[mask] = 0 # indexing using boolean array
1 Like

So would that work by adding:

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

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:

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)