-2

I have a NumPy array img_array of dimension (h,w,3) of one image, which is the result of some function. I want to convert this NumPy array directly into grayscale.

Possible Solution: Save the img_array as image using cv2.imwrite(path). Then read again with cv2.imread(path, cv2.GRAYSCALE)

However, I am looking for something like this :

def convert_array_to_grayscale_array(img_array):
        do something...
        return grayscare_version

I have already tried cv2.imread(img_array, CV2.GRAYSCALE), but it is throwing error of img_array must be a file pathname.

I think saving a separate image will consume more space disk. Is there any better way to do that with or without using the OpenCV library function.

3

1 Answer 1

1

scikit-image has color conversion functions: https://scikit-image.org/docs/dev/auto_examples/color_exposure/plot_rgb_to_gray.html

from skimage.color import rgb2gray

grayscale = rgb2gray(img_array)


Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.