Python: read text file into list

24 12 2012

20121224

text file contain data of 15 ground point. file name is grounddata.txt

 15
 1 18160.153 -57509.869 1284.700 0.2 0.2 0.1
 2 24207.094 -54489.362 1341.192 0.2 0.2 0.1
 3 27248.245 -51442.223 824.272 0.2 0.2 0.1
 4 21195.711 -51470.131 946.992 0.2 0.2 0.1
 5 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 6 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 7 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 8 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 9 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 10 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 11 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 12 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 13 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 14 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 15 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0

the first line is number of points.
second line to end is point number, X, Y, Z, SDX,SDY,SDZ

I need to read text file and convert data from text to number and add them to list in Python.
I use this code

def num(s):
  try:
     return int(s)
  except ValueError:
     return float(s)
if __name__ == '__main__':
   filemane = 'grounddataTom.txt'
   with open(filemane,'r') as f:
     i = 0
     groundPointList = []
     for line in f:
       if i == 0:
         groundPoint = int(line)
       else:
         lineTxt = line.split()
         lineNum = []
           for j in lineTxt:
             lineNum.append(num(j))
           groundPointList.append(lineNum)
       i +=1
  print groundPoint
  print groundPointList

it work very well

Advertisements




Python: get variable value from text

14 12 2012

20121214

มีตัวแปรที่เก็บไว้แบบเรียงหมายเลข ต้องการระบุเฉพาะหมายเลขแล้วเข้าไปเรียกตัวแปรได้ถูกตัว

code:

frame098Data = [1,a,2,b,3,]
frame101Data = [2,d,e,f,g,]
frame104Data = [3,q,2,p,1,]

input ="098"
dataname ="frame"+input+"Data"

print dataname

จะได้ >”frame098Data”
แต่หากเราใช้ฟังก์ชั่น eval()

print eval(dataname)

จะได้ >[1,a,2,b,3,]





Python: get last folder from path

7 12 2012

20121207

ต้องการใช้ชื่อโฟล์ดเดอร์สุดท้าย ไปใช้งานสามารถทำได้ดังนี้

code:

import os
mypath = ‘G:\Python\Sample\lastFolder’
print os.path.basename(os.path.normpath(mypath)

the result is “lastFolder”





Python: OpenCV: FeatureDetector / DescriptorExtractor

24 11 2012

20121124

cv2

ของจุดที่น่าสนใจด้วยวิธี cv2.FeatureDetector_create(detector)
และหา descriptor ด้วย cv2.DescriptorExtractor_create(detector)

code:

import cv2
image01 = r'D:\Phanu\research\PanoData\ladybug_Panoramic_5400x2700_00000097.jpg'
img = cv2.imread(image01)
detector = "SIFT"
sd = cv2.FeatureDetector_create(detector)
surf = cv2.DescriptorExtractor_create(detector)
keypoints = sd.detect(img)
l,d = surf.compute(img, keypoints)

เท่าที่ลอง  [“SIFT”,”SURF”,”ORB”] สามอย่างนี้ใช้ได้





Python: OpencV: resize image

23 11 2012

20121123

resize image with opencv [cv2]

“can’t show full image?” yes, because your image bigger than monitor resolution.
let scale it down.

this sample is show how to resize image down 4 times

code:

import cv2
 filename = "panorama_00098.jpg"
 oriimage = cv2.imread(filename)
 newx,newy = oriimage.shape[1]/4,oriimage.shape[0]/4 #new size (w,h)
 newimage = cv2.resize(oriimage,(newx,newy))
 cv2.imshow("original image",oriimage)
 cv2.imshow("resize image",newimage)
 cv2.waitKey(0)

Image

I added more example, try to see
resize image by width





Python: OpenCV: Test various feature detectors

17 11 2012

20121117

Python 2.7.3
OpenCV 2.2
Windows 7 HP

ทดสอบ feature detectors ใน OpenCV ประกอบไปด้วย
“FAST”,”STAR”,”SIFT”,”SURF”,”ORB”,”MSER”,”GFTT”,”HARRIS”,”Dense”
“GridFAST”,”GridSTAR”,”GridSIFT”,”GridSURF”,”GridORB”,”GridMSER”,”GridGFTT”,”GridHARRIS”,”GridDense”
“PyramidFAST”,”PyramidSTAR”,”PyramidSIFT”,”PyramidSURF”,”PyramidORB”,”PyramidMSER”,”PyramidGFTT”,”PyramidHARRIS”,”PyramidDense”

thanks J W J (MIT)

ภาพพานอรามาที่ใช้ทดสอบ

Image

เครื่อง DELL inspiron z14 [2012]
i7 1.9 GHz 8 Mb ram Windows 7 HP

ผลลัพธ์

FAST number of KeyPoint objects 21698 (time 0.01900 )
STAR number of KeyPoint objects 1063 (time 0.17800 )
SIFT number of KeyPoint objects 2135 (time 0.33500 )
SURF number of KeyPoint objects 9556 (time 0.48800 )
ORB number of KeyPoint objects 500 (time 0.05300 )
MSER number of KeyPoint objects 858 (time 0.43200 )
GFTT number of KeyPoint objects 1000 (time 0.38300 )
HARRIS number of KeyPoint objects 1000 (time 0.36800 )
Dense number of KeyPoint objects 58482 (time 0.01200 )
GridFAST number of KeyPoint objects 992 (time 0.02000 )
GridSTAR number of KeyPoint objects 358 (time 0.06500 )
GridSIFT number of KeyPoint objects 771 (time 0.33600 )
GridSURF number of KeyPoint objects 992 (time 0.37300 )
GridORB number of KeyPoint objects 910 (time 0.06400 )
GridMSER number of KeyPoint objects 610 (time 0.52800 )
GridGFTT number of KeyPoint objects 992 (time 0.36000 )
GridHARRIS number of KeyPoint objects 975 (time 0.35900 )
GridDense number of KeyPoint objects 992 (time 0.00900 )
PyramidFAST number of KeyPoint objects 29465 (time 0.03100 )
PyramidSTAR number of KeyPoint objects 1612 (time 0.22000 )
PyramidSIFT number of KeyPoint objects 3108 (time 0.44200 )
PyramidSURF number of KeyPoint objects 13012 (time 0.62600 )
PyramidORB number of KeyPoint objects 1489 (time 0.07500 )
PyramidMSER number of KeyPoint objects 1365 (time 0.60700 )
PyramidGFTT number of KeyPoint objects 3000 (time 0.49200 )
PyramidHARRIS number of KeyPoint objects 2140 (time 0.48900 )
PyramidDense number of KeyPoint objects 76886 (time 0.01900 )

ภาพผลลัพธ์

FAST Image

STAR Image

SIFT Image

SURF Image

ORB Image

MSER Image

GFTT Image

HARRIS Image

Dense 

GridFAST Image

GridSTAR Image

GridSIFT Image

GridSURF Image

GridORB Image

GridMSER Image

GridGFTT Image

GridHARRIS Image

GridDense 

PyramidFAST Image

PyramidSTAR Image

PyramidSIFT Image

PyramidSURF Image

PyramidORB Image

PyramidMSER Image

PyramidGFTT Image

PyramidHARRIS Image

PyramidDense 

 





install numpy in python 2.7 64bit

31 10 2012

20121031

OS window 7 64bit

Python 2.7 64bit

if you want to install numpy plug-in, you can download 64 bit version from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy

more interest packages:

openCV
PIL
pyCUDA
pyLibTiff
pyOpenCL
pyProj
basemap

also dowmload from here (http://www.lfd.uci.edu/~gohlke/pythonlibs)