As the assertion states, adaptiveThreshold() requires a single-channeled 8-bit image.

Assuming your floating-point image ranges from 0 to 1, which appears to be the case, you can convert the image by multiplying by 255 and casting to np.uint8:

float_img = np.random.random((4,4))
im = np.array(float_img * 255, dtype = np.uint8)
threshed = cv2.adaptiveThreshold(im, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 0)
Answer from Aurelius on Stack Overflow
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert a python numpy array to a grayscale image
5 Best Ways to Convert a Python NumPy Array to a Grayscale Image - Be on the Right Side of Change
February 20, 2024 - In this code example, a 2D NumPy array with random values is first created, then Pillow’s Image.fromarray() function is used to transform this array into an image. The mode ‘L’ stands for luminance, which is used to create grayscale images.
Discussions

Building a gray scale image via a numpy array from ultrasonic sensor values - programming - OpenCV
Hi all, I have an ultrasonic sensor. I want to convert its distance readings to an image. I have the readings stored in a numpy array but how do I construct the array so that it is a valid gray scale image? Here is the code I have so far. import maxSonarTTY import numpy as np import cv2 def ... More on forum.opencv.org
🌐 forum.opencv.org
0
December 15, 2022
Grayscale image using opencv from numpy array failed - Stack Overflow
I use the following numpy array that hold an image which is black and white image with the following shape print(img.shape) (28, 112) when I try to grayscale the image, to use it to get contours ... More on stackoverflow.com
🌐 stackoverflow.com
June 3, 2018
python 3.x - Applying CV2 Grayscale to a Numpy 4D array - Stack Overflow
While I know how to convert a single color image (32,32,3) to grayscale using CV2: img = cv2.cvtColor( img, cv2.COLOR_RGB2GRAY ) I need to convert a batch of 60,000 images in a 4D numpy array (600... More on stackoverflow.com
🌐 stackoverflow.com
python - cv2 - Load grayscale Image as Numpy Array from Variable - Stack Overflow
I have a 2D numpy array that contains my grayscale image (8bit per pixel). I now want to load it into cv2 and resize it to a fixed width and height. But when I load the numpy array I get the follow... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-grayscaling-of-images-using-opencv
Python | Grayscaling of Images using OpenCV - GeeksforGeeks
September 23, 2025 - cv2.cvtColor(): Converts color spaces; cv2.COLOR_BGR2GRAY produces a single-channel grayscale image.
🌐
OpenCV
forum.opencv.org › t › building-a-gray-scale-image-via-a-numpy-array-from-ultrasonic-sensor-values › 11324
Building a gray scale image via a numpy array from ultrasonic sensor values - programming - OpenCV
December 15, 2022 - Hi all, I have an ultrasonic sensor. I want to convert its distance readings to an image. I have the readings stored in a numpy array but how do I construct the array so that it is a valid gray scale image? Here is the code I have so far. import maxSonarTTY import numpy as np import cv2 def scale_list(l, to_min, to_max): return [scale_number(i, to_min, to_max, min(l), max(l)) for i in l] m=0 raw_sonar_readings=[] while True: m=maxSonarTTY.measure("/dev/ttyUSB0") raw_sonar_r...
🌐
pythontutorials
pythontutorials.net › blog › how-to-change-numpy-array-into-grayscale-opencv-image
How to Convert Numpy Array to Grayscale OpenCV Image in Python: Fixing CV_8UC1 Type Error for AdaptiveThreshold — pythontutorials.net
Suppose you load an image with matplotlib (which returns RGB float arrays in [0.0, 1.0]), convert it to grayscale, and try to apply adaptiveThreshold: import numpy as np import cv2 from matplotlib import image # Loads image as RGB float (0.0-1.0) # Load image with matplotlib (returns RGB float array) rgb_float_array = image.imread("test_image.jpg") # Shape: (H, W, 3), dtype: float64 # Convert to "grayscale" by averaging channels (naive approach) grayscale_float = np.mean(rgb_float_array, axis=2) # Shape: (H, W), dtype: float64 # Attempt adaptive thresholding thresh = cv2.adaptiveThreshold(grayscale_float, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
🌐
CSDN
devpress.csdn.net › python › 63045d887e6682346619a944.html
How to change numpy array into grayscale opencv image_python_Mangs-Python
August 23, 2022 - How can I change numpy array into grayscale opencv image in python? After some processing I got an array with following atributes: max value is: 0.99999999988, min value is 8.269656407e-08 and type is: <type 'numpy.ndarray'>. I can show it as an image using cv2.imshow() function, but I can't pass it into cv2.AdaptiveTreshold() function because it has wrong type:
🌐
Note.nkmk.me
note.nkmk.me › home › python › opencv
Reading and saving image files with Python, OpenCV (imread, imwrite) | note.nkmk.me
October 15, 2022 - If you want to save a color image (3D ndarray) as a grayscale image file, convert it to grayscale with cv2.cvtColor() and cv2.COLOR_BGR2GRAY. If you save 2D ndarray to a file and read it again with cv2.imread(), it will be read as 3D ndarray ...
Find elsewhere
🌐
Quora
quora.com › How-do-I-turn-NumPY-arrays-into-grayscale-images
How to turn NumPY arrays into grayscale images - Quora
Answer (1 of 3): There are several available libraries and examples of using them. They are easily found. Seek and ye shall find. [code]from PIL import Image import matplotlib.pyplot as plt import imageio [/code]You don’t use all three, but you can choose among them.
🌐
GitHub
gist.github.com › panzi › 1ceac1cb30bb6b3450aa5227c02eedd3
Python: Convert PIL.Image to OpenCV BGR(A)/grayscale image (NumPy array). This supports the most common image modes, but there are more! Patches for those are welcome. · GitHub
Python: Convert PIL.Image to OpenCV BGR(A)/grayscale image (NumPy array). This supports the most common image modes, but there are more! Patches for those are welcome. - pil2cv.py
🌐
Stack Overflow
stackoverflow.com › questions › 68186862 › cv2-load-grayscale-image-as-numpy-array-from-variable
python - cv2 - Load grayscale Image as Numpy Array from Variable - Stack Overflow
np_img = np.fromfile(path, dtype='uint8') np_img = np_img.reshape(height,width) img = cv2.imdecode(np_img,cv2.IMREAD_GRAYSCALE) ... No need for cv2.imdecode here. If you already have an uint8 NumPy array, you can use it directly in any OpenCV ...
🌐
Stack Overflow
stackoverflow.com › questions › 55864705 › displaying-a-numpy-array-as-a-grayscale-image-using-opencv
python - Displaying a numpy array as a grayscale image using OpenCV - Stack Overflow
April 26, 2019 - You can do: cv2.imshow("Window", np.uint8(img)) api55 – api55 · 2019-04-26 09:39:25 +00:00 Commented Apr 26, 2019 at 9:39 · That was it! thanks mate :) KillyGourou – KillyGourou · 2019-04-26 09:41:57 +00:00 Commented Apr 26, 2019 at 9:41 · Add a comment | Related questions · 3 · Convert a 2D numpy array into a 3d numpy array representing a grayscaled image ·
🌐
OpenCV
docs.opencv.org › 3.4.20 › d3 › df2 › tutorial_py_basic_ops.html
OpenCV: Basic Operations on Images
>>> import numpy as np · >>> import ... coordinates. For BGR image, it returns an array of Blue, Green, Red values. For grayscale image, just corresponding intensity is returned....
🌐
GitHub
github.com › JaidedAI › EasyOCR › issues › 804
Cannot process high bit-depth grayscale numpy array · Issue #804 · JaidedAI/EasyOCR
July 29, 2022 - Where you convert from a grayscale numpy array to a cv2 image EasyOCR/easyocr/utils.py Line 709 in b7ddee3 img = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) cv2 gives an error for high bit-depth images > Unsupported depth of input image: > '...
Author: JaidedAI
🌐
Medium
medium.com › @rndayala › getting-started-with-opencv-2a2bc80ba2d6
Getting started with OpenCV. OpenCV-Python is a great tool for fast… | by Raghunath D | Medium
January 28, 2019 - cv2.IMREAD_GRAYSCALE : Loads image in gray-scale mode (0) cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel (-1) Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.
🌐
TutorialsPoint
tutorialspoint.com › how-to-show-numpy-2d-array-as-grayscale-image-in-jupyter-notebook
How to show numpy 2D array as grayscale image in Jupyter Notebook?
from matplotlib import pyplot as plt import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Random data points data = np.random.rand(5, 5) # Plot the data using imshow with gray colormap plt.imshow(data, cmap='gray') # Display the plot plt.show()
🌐
Stack Overflow
stackoverflow.com › questions › 27708831 › how-to-load-grayscale-image-as-numpy-array-in-opencv › 27709848
python - How to load grayscale image as numpy array in openCV - Stack Overflow
i have the 96x96 pixel image as numpy array. The thing is i dont know how to make opencv load this ndarray and detect faces. ... Thats what i did and it is throwing the above error. ... import numpy as np import cv2, cv import numpy as np import pandas as pd import pylab as pl from skimage import transform from numpy import ravel import pylab as pl from sklearn import linear_model, cross_validation from sklearn.svm import SVR from sklearn.decomposition import PCA from sklearn.neural_network import BernoulliRBM from sklearn.tree import DecisionTreeRegressor from sklearn.ensemble import RandomFo