简体   繁体   中英

Convert image numpy array into grayscale array directly without saving image

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.

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)


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM