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 Top answer 1 of 3
27
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)
2 of 3
14
This one worked for me:
uint_img = np.array(float_arr*255).astype('uint8')
grayImage = cv2.cvtColor(uint_img, cv2.COLOR_GRAY2BGR)
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
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
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
Cannot process high bit-depth grayscale numpy array
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: > '... More on github.com
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...
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:
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....
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 ·
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()
Saturn Cloud
saturncloud.io › blog › how-to-convert-an-image-to-grayscale-using-numpy-arrays-a-comprehensive-guide
How to Convert an Image to Grayscale Using NumPy Arrays: A Guide | Saturn Cloud Blog
May 1, 2026 - Color images are often represented as three-dimensional NumPy arrays, with dimensions corresponding to the height, width, and color channels of the image. Converting these to grayscale can reduce computational complexity and noise, making subsequent image processing tasks more manageable.
Note.nkmk.me
note.nkmk.me › home › python › opencv
Binarize image with Python, NumPy, OpenCV | note.nkmk.me
October 9, 2020 - This article describes how to binarize an image into black and white with a threshold. There are two ways: one is to use OpenCV function cv2.threshold(), and the other is to process ndarray with a bas ...
YouTube
youtube.com › watch
OpenCV Numpy Basics - Image Data Structure - Raspberry Pi - YouTube
Learn how images are structured for processing in OpenCV and learn the very basics of Numpy for use in Open CV. After this video, you will understand how Num...
Published: October 2, 2024