鱼C论坛

 找回密码
 立即注册
查看: 2278|回复: 1

关于openCV里imshow()显示图片的问题

[复制链接]
发表于 2019-3-29 19:30:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import cv2
  2. import os
  3. import os.path
  4. import numpy

  5. def img2txt(img_path = r'C:\Users\LuvPeppa\Desktop\img_files\1',txt_name = 'new_test.txt'):
  6.     R = []
  7.     G = []
  8.     B = []
  9.     with open(txt_name,'w') as f:
  10.         for each_file in os.listdir(img_path):
  11.             #path = r'C:\Users\Wongtao\Desktop\Cifarpy\img_files\1'
  12.             if os.path.splitext(each_file)[1] == '.jpg':
  13.                 image = cv2.imread(each_file)        
  14.                 p1=cv2.resize(image,(32,32),interpolation=cv2.INTER_CUBIC)
  15.                 #each_data = numpy.reshape(p1,(32,32,3))
  16.                 print('数组p1的形状是',numpy.shape(p1),type(p1))
  17.                 for each_pix in p1:
  18.                     for each_color in each_pix:
  19.                         B.append(each_color[0])
  20.                         G.append(each_color[1])
  21.                         R.append(each_color[2])
  22.                 new_data = list(R+G+B)
  23.                 print('数组R的形状是%s'%numpy.shape(R))
  24.                 print('数组new_data的形状是%s'%numpy.shape(new_data))
  25.                 #for each_line in new_data:
  26.                 new_data = str(new_data).replace(',',' ')
  27.                 new_data = new_data.replace('[','')
  28.                 new_data = new_data.replace(']','')
  29.                 f.write(str(new_data)+'\n')
  30.                 R = []
  31.                 G = []
  32.                 B = []
  33.                 cv2.imshow('a',p1)
  34.                 #cv2.imwrite('new_'+each_file,p1)#保存图片
  35.                 print(p1)
  36.                 break

  37. def txt2img(txt_name = 'new_test.txt'):
  38.     with open(txt_name,'r') as f:
  39.         img_data = [];R = [];G = [];B =[];col = ['[',']']
  40.         img_mat = numpy.zeros((1024,3))
  41.         for each_line in f:
  42.             img_data = [];R = [];G = [];B =[];
  43.             print(type(each_line),len(each_line),'1')
  44.             #print(each_line)   
  45.             print(type(each_line),len(each_line),'2')
  46.             each_line,nothing = each_line.split(sep = '\n')
  47.             each_line = each_line.split(sep = '  ')
  48.             print(type(each_line),len(each_line))
  49.             for each_color in each_line:
  50.                 #if each_color not in col:
  51.                 img_data.append(int(each_color))
  52.                 R = numpy.array(img_data[0:1024])
  53.                 G = numpy.array(img_data[1024:2048])
  54.                 B = numpy.array(img_data[2048:3072])
  55.             #img_mat = numpy.array(numpy.hstack((B,G,R)))
  56.             for each in range(len(B)):
  57.                 img_mat[each][0] = int(B[each])
  58.                 img_mat[each][1] = int(G[each])
  59.                 img_mat[each][2] = int(R[each])
  60.             #img_mat = img_mat.T
  61.             #print('B',type(B))
  62.             print(B[0],G[0],R[0])
  63.             print('%s的形状是%s,类型是%s'%('img_mat',numpy.shape(img_mat),type(img_mat)),img_mat[0])
  64.             cv2.imshow('a',numpy.reshape(img_mat,(32,32,3)))
  65.             print(img_mat)
  66.             break
  67.    
  68. if __name__ == '__main__':
  69.     img2txt()
  70.     txt2img()

复制代码

这段代码段img2txt()可以将图片转换为txt文档,但是txt2img()没法将该文档显示成图片。
明明打印出来的图片数据都一样,请问这是什么原因呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-3-29 21:48:57 | 显示全部楼层
已解决
  1. import cv2
  2. import os
  3. import os.path
  4. import numpy

  5. def img2txt(img_path = r'C:\Users\LuvPeppa\Desktop\img_files\1',txt_name = 'new_test.txt'):
  6.     R = [];G = [];B = [];extentions = ['.jpg','.bmp','.png']
  7.     with open(txt_name,'w') as f:
  8.         type_no = 0
  9.         for each_file in os.listdir(img_path):
  10.             #path = r'C:\Users\Wongtao\Desktop\Cifarpy\img_files\1'
  11.             type_no = type_no+1
  12.             if os.path.splitext(each_file)[1] in extentions:
  13.                 image = cv2.imread(each_file)        
  14.                 p1=cv2.resize(image,(32,32))
  15.                 for each_pix in p1:
  16.                     for each_color in each_pix:
  17.                         B.append(each_color[0])
  18.                         G.append(each_color[1])
  19.                         R.append(each_color[2])
  20.                 new_data = list(R+G+B)
  21.                 new_data = str(new_data).replace(',',' ')
  22.                 new_data = new_data.replace('[','')
  23.                 new_data = new_data.replace(']','')
  24.                 f.write(str(new_data)+'\n')
  25.                 R = []
  26.                 G = []
  27.                 B = []
  28.                 #cv2.imshow('a',p1)
  29.                 #os.chdir(img_path+'\\'+str(type_no))
  30.                 print('图像%s正在保存。。。'%('new_'+each_file))
  31.                 #cv2.imwrite('new_'+each_file,p1)#保存图片
  32.                 #os.chdir(img_path)
  33.                

  34. def txt2img(txt_name = 'new_test.txt'):
  35.     with open(txt_name,'r') as f:
  36.         pic_num = 0
  37.         for each_line in f:
  38.             pic_num = pic_num+1
  39.             img_data = [];R = [];G = [];B =[];img_mat = []
  40.             each_line,nothing = each_line.split(sep = '\n')
  41.             each_line = each_line.split(sep = '  ')
  42.             for each_color in each_line:
  43.                 img_data.append(int(each_color))
  44.                 R = bytearray(img_data[0:1024])
  45.                 G = bytearray(img_data[1024:2048])
  46.                 B = bytearray(img_data[2048:3072])
  47.             for index in range(1024):
  48.                 img_mat.append(B[index])
  49.                 img_mat.append(G[index])
  50.                 img_mat.append(R[index])
  51.             img_mat = bytearray(img_mat)
  52.             img_mat = numpy.array(img_mat)
  53.             bgrImage = img_mat.reshape(32,32,3)
  54.             print('图像%s正在显示。。。'%pic_num)
  55.             #cv2.imshow(str(pic_num)+'_back.png', bgrImage)
  56.             # print('图像%s正在保存。。。'%pic_num)
  57.             #cv2.imwrite(str(pic_num)+'_back.png', bgrImage)
  58.             
  59.             
  60. if __name__ == '__main__':
  61.     img2txt()
  62.     txt2img()

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-23 22:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表