2013-07-01 72 views
0

我的代碼在.tiff圖像上計算Hu時刻。我需要在輸出中刪除指數(科學)符號,看起來像:在打印NumPy數組時,如何去除指數(科學記數法)?

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))]) 

謝謝你幫我

回答

1

變化

writer.writerow([('{}\t'.format(elem))]) 

writer.writerow([('{0:.10f}\t'.format(elem))]) 

Here你可以找到字符串在Python格式化的解釋。

1

而不是使用CSV創建的文件,你可以調用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不會在每行末尾放置製表符。但是,那麼,你爲什麼要一個?