网络编程
位置:首页>> 网络编程>> Python编程>> python-OpenCV 实现将数组转换成灰度图和彩图

python-OpenCV 实现将数组转换成灰度图和彩图

作者:li_il  发布时间:2023-07-22 11:22:19 

标签:python,OpenCV,数组,灰度图,彩图

主要步骤

1.生成普通python数组(bytearray(),os.urandom())

2.转换成numpy数组(numpy.array())

3.通过reshape将数组转换到所需的维数

4.以图像的形式显示出来(cv.imshow())

代码


import os

import cv2 as cv
import numpy as np

# Make an array of 120000 random bytes
randomByteArray = bytearray(os.urandom(120000))
# translate into numpy array
flatNumpyArray = np.array(randomByteArray)
# Convert the array to make a 400*300 grayscale image(灰度图像)
grayImage = flatNumpyArray.reshape(300, 400)
# show gray image
cv.imshow('GrayImage', grayImage)
# print image's array
print(grayImage)
cv.waitKey()

# byte array translate into RGB image
randomByteArray1 = bytearray(os.urandom(360000))
flatNumpyArray1 = np.array(randomByteArray1)
BGRimage = flatNumpyArray1.reshape(300,400,3)
cv.imshow('BGRimage', BGRimage)
cv.waitKey()
cv.destroyAllWindows()

效果

python-OpenCV 实现将数组转换成灰度图和彩图

来源:https://blog.csdn.net/li_l_il/article/details/83214114

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com