我的代码在.tiff图像上计算Hu时刻。我需要在输出中删除指数(科学)表示法,如下所示:
2.84737313535e-05
1.25610416364e-09
1.25419253619e-09
1.57419718046e-18
6.69242940524e-12
4.17512050223e-22
def humoments(self): #function for HuMoments computation
for filename in glob.iglob ('*.tif'):
img = cv.LoadImageM(filename, cv.CV_LOAD_IMAGE_GRAYSCALE)
cancer = cv.GetHuMoments(cv.Moments(img))
#arr = cancer
arr = numpy.array([cancer])
with open('hu.csv', 'wb') as csvfile:
for elem in arr.flat[:50]:
#print('{}\t'.format(elem)) #this code puts the result to console
writer = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow([('{}\t'.format(elem))])
感谢您帮助我
答案 0 :(得分:1)
更改
writer.writerow([('{}\t'.format(elem))])
要
writer.writerow([('{0:.10f}\t'.format(elem))])
Here你可以在Python中找到字符串格式的解释。
答案 1 :(得分:1)
您可以拨打np.savetxt
:
>>> import numpy as np
>>> arr = np.array([2.84e-05, 6.69e-12])
>>> np.savetxt('/tmp/out', arr, fmt='%.14f')
产生
0.00002840000000
0.00000000000669
所以在你的情况下,要写出arr使用的前50个元素:
np.savetxt('hu.csv', arr.flat[:50], fmt='%.14f')
而不是
with open('hu.csv', 'wb') as csvfile:
for elem in arr.flat[:50]:
#print('{}\t'.format(elem)) #this code puts the result to console
writer = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow([('{}\t'.format(elem))])
注意:与原始代码不同,np.savetxt
不会在每行的末尾添加制表符。但那么,你为什么要一个呢?