if
__name__=="__main__": cam=IP_Cam() img1=cam.getImage().convert('L') time.sleep(0.1) img2=cam.getImage().convert('L') t1=time.clock() arr1=np.asarray(img1, np.int8) # vorzeichenbehaftet arr2=np.asarray(img2, np.int8) t2=time.clock() diff=np.absolute(arr1-arr2) t3=time.clock() diff[diff<32]=0 t4=time.clock() print("%2.3f ms"%((t2-t1)*1000), " %2.3f ms"%((t3-t2)*1000), " %2.3f ms"%((t4-t3)*1000), " %2.3f ms"%((t4-t1)*1000), " -> ", type(diff[0, 0])) cam.saveImage(np.uint8(diff), "diff1") t1=time.clock() arr1=np.asarray(img1, np.uint8) # vorzeichenlos arr2=np.asarray(img2, np.uint8) t2=time.clock() diff=arr1-arr2 t3=time.clock() diff[diff<32]=0 diff[diff>224]=0 t4=time.clock() print("%2.3f ms"%((t2-t1)*1000), " %2.3f ms"%((t3-t2)*1000), " %2.3f ms"%((t4-t3)*1000), " %2.3f ms"%((t4-t1)*1000), " -> ", type(diff[0, 0])) cam.saveImage(diff, "diff2") t1=time.clock() arr1=np.asarray(img1) # aus input-data abgeleitet arr2=np.asarray(img2) t2=time.clock() diff=arr1-arr2 t3=time.clock() diff[diff<32]=0 diff[diff>224]=0 t4=time.clock() print("%2.3f ms"%((t2-t1)*1000), " %2.3f ms"%((t3-t2)*1000), " %2.3f ms"%((t4-t3)*1000), " %2.3f ms"%((t4-t1)*1000), " -> ", type(diff[0, 0])) cam.saveImage(diff, "diff3") |
Die beiden asarray-Anweisungen brauchen im Mittel also folgende Zeiten:
dede@i5:~/bastelein/ipcam> ./tst14.py
0.418 ms 0.723 ms 0.521 ms 1.662 ms -> <class 'numpy.int8'>
0.265 ms 0.067 ms 4.141 ms 4.473 ms -> <class 'numpy.uint8'>
0.263 ms 0.061 ms 4.132 ms 4.456 ms -> <class 'numpy.uint8'>
dede@i5:~/bastelein/ipcam> ./tst14.py
0.424 ms 0.683 ms 0.486 ms 1.593 ms -> <class 'numpy.int8'>
0.275 ms 0.069 ms 3.930 ms 4.274 ms -> <class 'numpy.uint8'>
0.255 ms 0.062 ms 3.899 ms 4.216 ms -> <class 'numpy.uint8'>
dede@i5:~/bastelein/ipcam> ./tst14.py
0.469 ms 0.808 ms 0.579 ms 1.856 ms -> <class 'numpy.int8'>
0.335 ms 0.068 ms 4.657 ms 5.060 ms -> <class 'numpy.uint8'>
0.276 ms 0.083 ms 4.455 ms 4.814 ms -> <class 'numpy.uint8'>
dede@i5:~/bastelein/ipcam> ./tst14.py
0.436 ms 0.703 ms 0.497 ms 1.636 ms -> <class 'numpy.int8'>
0.302 ms 0.061 ms 3.924 ms 4.287 ms -> <class 'numpy.uint8'>
0.266 ms 0.060 ms 4.006 ms 4.332 ms -> <class 'numpy.uint8'>
dede@i5:~/bastelein/ipcam>
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- import time import numpy as np if __name__=="__main__": v=[[10, 245], [100, 145]] vt=["change all ", "change nothing"] for i in (0, 1): for t in [np.int8, np.uint8, np.int, np.uint, np.int32, np.uint32]: a=np.empty([480, 640], t) for y in range(480): for x in range(640): a[y, x]=v[i][x&1] # (50%=10, 50%=245) oder (50%=100, 50%=145) t1=time.process_time() for l in range(1000): b=1*a b[b<32]=0 b[b>224]=0 t2=time.process_time() print("%s %8.2f ms"%(vt[i], (t2-t1)*1000), a.dtype) |
Sehr bizarr. Auf einem Array vom Typ int8 sind die Anweisungen zum "Extremwerte löschen" schneller, wenn es viel zu löschen gibt.
change all 327.99 ms int8
change all 3079.23 ms uint8
change all 3367.71 ms int64
change all 3386.04 ms uint64
change all 2847.84 ms int32
change all 2871.66 ms uint32
change nothing 1627.19 ms int8
change nothing 117.88 ms uint8
change nothing 747.36 ms int64
change nothing 747.02 ms uint64
change nothing 296.32 ms int32
change nothing 307.25 ms uint32
if
__name__=="__main__": cam=IP_Cam() img1=cam.getImage().convert('L') time.sleep(0.1) img2=cam.getImage().convert('L') i=0 for t in [np.int8, np.uint8, np.int, np.uint, np.int32, np.uint32]: arr1=np.asarray(img1, t) arr2=np.asarray(img2, t) t1=time.process_time() for l in range(1000): if t in (np.int8, np.int, np.int32): diff=np.absolute(arr1-arr2) else: diff=arr1-arr2 diff[diff>224]=0 diff[diff<32]=0 t2=time.process_time() cam.saveImage(np.uint8(diff), "diff%d"%(i,)) print("%8.2f ms"%((t2-t1)*1000), diff.dtype) i+=1 |
Das Differenz-Bild für int8 sieht so aus:
383.39 ms int8
1365.53 ms uint8
1278.36 ms int64
2317.85 ms uint64
601.93 ms int32
1638.93 ms uint32