https://blog.csdn.net/wsLJQian/article/details/81210867
终于解决了多年以来为什么读取txt文件时总是从第二行开始打印的问题,记住readline是读一行,readlines才是读所有行
第一行:
import cv2
f = open("result_final_2.txt","r") #设置文件对象
lines = f.readlines()
count = 0
a = []
b = []
import numpy as np
img1 = cv2.imread('l.png',0)
img2 = cv2.imread('r.png',0)
j = 0
jj = -1
for line in lines: #直到读取完文件
count = count + 1
l = line.split('\t')[0]
if count != 1213:
jj = jj + 1
a.append(l) #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
a.append(img1[1, j])
a.append(img2[1, jj])
a.append(img1[1, j]-img2[1, jj])
print(a)
a = []
b.append(a)
else:
count = 1
j = j + 1
jj = 0
a.append(l) #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
a.append(img1[1, j])
a.append(img2[1, jj])
a.append(img1[1, j]-img2[1, jj])
print(a)
a = []
b.append(a)
np.savetxt("b.txt", b,fmt='%s',delimiter=',')
第三到10行:
import cv2
f = open("result_final_3.txt","r") #设置文件对象
lines = f.readlines()
count = 0
a = []
b = []
import numpy as np
img1 = cv2.imread('l.png',0)
img2 = cv2.imread('r.png',0)
j = 0
jj = -1
i = 2
cnt = 0
for line in lines: #直到读取完文件
cnt = cnt + 1
if cnt > 1468944:
cnt = 1
i = i + 1
count = 0
j = 0
jj = -1
count = count + 1
l = line.split('\t')[0]
if count != 1213:
jj = jj + 1
a.append(l) #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
a.append(img1[i, j])
a.append(img2[i, jj])
a.append(int(img1[i, j])-int(img2[i, jj]))
print(a)
a = []
b.append(a)
else:
count = 1
j = j + 1
jj = 0
a.append(l) #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
a.append(img1[i, j])
a.append(img2[i, jj])
a.append(int(img1[i, j])-int(img2[i, jj]))
print(a)
a = []
b.append(a)
np.savetxt("c.txt", b,fmt='%s',delimiter=',')
第11到20行:
import cv2
f = open("result_final_4.txt","r") #设置文件对象
lines = f.readlines()
count = 0
a = []
b = []
import numpy as np
img1 = cv2.imread('l.png',0)
img2 = cv2.imread('r.png',0)
j = 0
jj = -1
i = 10
cnt = 0
for line in lines: #直到读取完文件
cnt = cnt + 1
if cnt > 1468944:
cnt = 1
i = i + 1
count = 0
j = 0
jj = -1
count = count + 1
l = line.split('\t')[0]
if count != 1213:
jj = jj + 1
a.append(l) #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
a.append(img1[i, j])
a.append(img2[i, jj])
a.append(int(img1[i, j])-int(img2[i, jj]))
print(a)
a = []
b.append(a)
else:
count = 1
j = j + 1
jj = 0
a.append(l) #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
a.append(img1[i, j])
a.append(img2[i, jj])
a.append(int(img1[i, j])-int(img2[i, jj]))
print(a)
a = []
b.append(a)
np.savetxt("d.txt", b,fmt='%s',delimiter=',')
21-30
import cv2
f = open("result_final_5.txt","r") #设置文件对象
lines = f.readlines()
count = 0
a = []
b = []
import numpy as np
img1 = cv2.imread('l.png',0)
img2 = cv2.imread('r.png',0)
j = 0
jj = -1
i = 20
cnt = 0
for line in lines: #直到读取完文件
cnt = cnt + 1
if cnt > 1468944:
cnt = 1
i = i + 1
count = 0
j = 0
jj = -1
count = count + 1
l = line.split('\t')[0]
if count != 1213:
jj = jj + 1
a.append(l) #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
a.append(img1[i, j])
a.append(img2[i, jj])
a.append(int(img1[i, j])-int(img2[i, jj]))
print(a)
a = []
b.append(a)
else:
count = 1
j = j + 1
jj = 0
a.append(l) #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
a.append(img1[i, j])
a.append(img2[i, jj])
a.append(int(img1[i, j])-int(img2[i, jj]))
print(a)
a = []
b.append(a)
np.savetxt("e.txt", b,fmt='%s',delimiter=',')