神剑山庄资源网 Design By www.hcban.com
在学习tensorflow的过程中,有一个问题,tensorflow在训练的过程中读取的是二进制图像数据库文件,而不是图像文件,因此
在进行训练、测试之前需要将图像文件转换为二进制格式。
下面是我在ubuntu中使用python+OpenCV读取图像并转换为二进制格式文件的代码。
#coding=utf-8 ''' Created on 2016年3月24日 使用Opencv读取图像将其保存为二进制格式文件,再读取该二进制文件,转换为图像进行显示 @author: hanchao ''' import cv2 import numpy as np import struct image = cv2.imread("test.jpg") #imageClone = np.zeros((image.shape[0],image.shape[1],1),np.uint8) #image.shape[0]为rows #image.shape[1]为cols #image.shape[2]为channels #image.shape = (480,640,3) rows = image.shape[0] cols = image.shape[1] channels = image.shape[2] #把图像转换为二进制文件 #python写二进制文件,f = open('name','wb') #只有wb才是写二进制文件 fileSave = open('patch.bin','wb') for step in range(0,rows): for step2 in range(0,cols): fileSave.write(image[step,step2,2]) for step in range(0,rows): for step2 in range(0,cols): fileSave.write(image[step,step2,1]) for step in range(0,rows): for step2 in range(0,cols): fileSave.write(image[step,step2,0]) fileSave.close() #把二进制转换为图像并显示 #python读取二进制文件,用rb #f.read(n)中n是需要读取的字节数,读取后需要进行解码,使用struct.unpack("B",fileReader.read(1))函数 #其中“B”为无符号整数,占一个字节,“b”为有符号整数,占1个字节 #“c”为char类型,占一个字节 #“i”为int类型,占四个字节,I为有符号整形,占4个字节 #“h”、“H”为short类型,占四个字节,分别对应有符号、无符号 #“l”、“L”为long类型,占四个字节,分别对应有符号、无符号 fileReader = open('patch.bin','rb') imageRead = np.zeros(image.shape,np.uint8) for step in range(0,rows): for step2 in range(0,cols): a = struct.unpack("B",fileReader.read(1)) imageRead[step,step2,2] = a[0] for step in range(0,rows): for step2 in range(0,cols): a = struct.unpack("b",fileReader.read(1)) imageRead[step,step2,1] = a[0] for step in range(0,rows): for step2 in range(0,cols): a = struct.unpack("b",fileReader.read(1)) imageRead[step,step2,0] = a[0] fileReader.close() cv2.imshow("source",image) cv2.imshow("read",imageRead) cv2.waitKey(0)
以上这篇Python+OpenCV实现将图像转换为二进制格式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无Python+OpenCV实现将图像转换为二进制格式的评论...
更新日志
2024年09月30日
2024年09月30日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】