Python+OpenCV实现将图像转换为二进制格式

作者:大蛇王 时间:2021-06-25 08:10:33 

在学习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)

来源:https://blog.csdn.net/t8116189520/article/details/78889327

标签:Python,OpenCV,图像,二进制
0
投稿

猜你喜欢

  • asp实现ACCESS数据库加密方法

    2008-04-18 12:33:00
  • 解析PHP中VC6 X86和VC9 X86的区别及 Non Thread Safe的意思

    2023-11-06 13:11:37
  • Python基于list的append和pop方法实现堆栈与队列功能示例

    2022-10-20 02:26:26
  • 删除PHP数组中的重复元素的实现代码

    2023-06-06 21:19:46
  • Python实现字符串与数组相互转换功能示例

    2021-08-13 15:50:12
  • 部署Django到阿里云服务器教程示例

    2022-03-28 23:46:19
  • 从XML中读取数据到内存的实例

    2008-09-04 14:43:00
  • 微信小程序实现图片上传、删除和预览功能的方法

    2023-09-20 08:54:30
  • jQuery 横向滚动图片

    2009-03-11 13:09:00
  • 分析运行中的 Python 进程详细解析

    2021-09-19 14:47:30
  • python实现简单的计算器功能

    2021-02-08 03:38:54
  • 使用go求幂的几种方法小结

    2023-09-23 05:07:45
  • SQL Server索引管理的六大铁律

    2009-03-25 14:05:00
  • 有效LOGO设计的最重要的提示

    2010-06-09 12:05:00
  • asp截取指定英汉混合字符串_支持中文

    2011-04-19 10:39:00
  • js实现QQ邮箱邮件拖拽删除功能

    2023-09-07 04:31:05
  • 阿里云CentOS7搭建Apache+PHP+MySQL环境

    2023-11-23 02:44:59
  • python3操作微信itchat实现发送图片

    2022-03-26 11:42:52
  • asp如何做一个专门显示文本文件的页面?

    2010-07-12 19:04:00
  • Python注释、分支结构、循环结构、伪“选择结构”用法实例分析

    2021-01-15 14:45:25
  • asp之家 网络编程 m.aspxhome.com