您好,欢迎来到意榕旅游网。
搜索
您的当前位置:首页python-OpenCV实现将数组转换成灰度图和彩图

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

来源:意榕旅游网
python-OpenCV实现将数组转换成灰度图和彩图

主要步骤

1.⽣成普通python数组(bytearray(),os.urandom())2.转换成numpy数组(numpy.array())3.通过reshape将数组转换到所需的维数4.以图像的形式显⽰出来(cv.imshow())代码

import os

import cv2 as cvimport 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 arrayprint(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 实现将数组转换成灰度图和彩图就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- yrrf.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务