File: generate_label_testvectors.py

package info (click to toggle)
python-scipy 0.18.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 75,464 kB
  • ctags: 79,406
  • sloc: python: 143,495; cpp: 89,357; fortran: 81,650; ansic: 79,778; makefile: 364; sh: 265
file content (43 lines) | stat: -rw-r--r-- 1,672 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import numpy as np
from scipy.ndimage import label


def generate_test_vecs(infile, strelfile, resultfile):
    "test label with different structuring element neighborhoods"
    def bitimage(l):
        return np.array([[c for c in s] for s in l]) == '1'
    data = [np.ones((7, 7)),
            bitimage(["1110111",
                      "1100011",
                      "1010101",
                      "0001000",
                      "1010101",
                      "1100011",
                      "1110111"]),
            bitimage(["1011101",
                      "0001000",
                      "1001001",
                      "1111111",
                      "1001001",
                      "0001000",
                      "1011101"])]
    strels = [np.ones((3, 3)),
              np.zeros((3, 3)),
              bitimage(["010", "111", "010"]),
              bitimage(["101", "010", "101"]),
              bitimage(["100", "010", "001"]),
              bitimage(["000", "111", "000"]),
              bitimage(["110", "010", "011"]),
              bitimage(["110", "111", "011"])]
    strels = strels + [np.flipud(s) for s in strels]
    strels = strels + [np.rot90(s) for s in strels]
    strels = [np.fromstring(s, dtype=int).reshape((3, 3))
              for s in set(t.astype(int).tostring() for t in strels)]
    inputs = np.vstack(data)
    results = np.vstack([label(d, s)[0] for d in data for s in strels])
    strels = np.vstack(strels)
    np.savetxt(infile, inputs, fmt="%d")
    np.savetxt(strelfile, strels, fmt="%d")
    np.savetxt(resultfile, results, fmt="%d")

generate_test_vecs("label_inputs.txt", "label_strels.txt", "label_results.txt")