Debian Codegraph @

examples/tutorials/introduction/0_introduction.py

raw
1 # (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10
11 """
12 Introduction to Traits
13 ======================
14
15 To make the tutorial more practical, let's imagine that you are
16 writing some tools to organize and process image data, perhaps
17 to help you develop a machine learning algorithm, or to document
18 your research. This image data might come from a camera, a scanning
19 electron microscope, or from some more exotic source. No matter the
20 source, the image will have data associated with it, such as who
21 produced the image, the equipment that was used, the date and time
22 that the image was taken, and so forth.
23
24 In this tutorial, we'll consider an example where we have a collection
25 of greyscale SEM images, each of them stored in a file, along with other
26 information, such as the sample that was imaged, the size of the scanned
27 region, and the operator who took the image.
28
29 We would like to read in the image and provide a number of standard
30 analysis steps to the user, as well as making the raw image data
31 available as a NumPy array.
32
33 The sample code here shows how you might create a class in standard
34 object-oriented Python with NumPy for analysis and Pillow for image
35 loading. Experiment with the code in the example to see how you can use it.
36
37 In the subsequent steps of the tutorial, we'll look at how we can use Traits
38 to simplify and improve the code here.
39 """
40
41 import datetime
42 import os
43
44 import numpy as np
45 from PIL import Image as PILImage
46
47
48 class Image:
49 """ An SEM image stored in a file. """
50
51 def __init__(self, filename, sample_id, date_acquired, operator,
52 scan_size=(1e-5, 1e-5)):
53 # initialize the primary attributes
54 self.filename = filename
55 self.sample_id = sample_id
56 self.operator = operator
57 self.date_acquired = operator
58 self.scan_size = scan_size
59
60 # useful secondary attributes
61 self.scan_width, self.scan_height = self.scan_size
62
63 def read_image(self):
64 """ Read the image from disk. """
65 pil_image = PILImage.open(self.filename).convert("L")
66 self.image = np.array(pil_image)
67
68 # compute some extra secondary attributes from the image
69 if self.image.size > 0:
70 self.pixel_area = (
71 self.scan_height * self.scan_width / self.image.size
72 )
73 else:
74 self.pixel_area = 0
75
76 def histogram(self):
77 """ Compute the normalized histogram of the image. """
78 hist, bins = np.histogram(
79 self.image,
80 bins=256,
81 range=(0, 256),
82 density=True,
83 )
84 return hist
85
86 def threshold(self, low=0, high=255):
87 """ Compute a threshold mask for the array. """
88 return (self.image >= low) & (self.image <= high)
89
90
91 # ---------------------------------------------------------------------------
92 # Demo code
93 # ---------------------------------------------------------------------------
94
95 this_dir = os.path.dirname(__file__)
96 image_dir = os.path.join(this_dir, "images")
97 filename = os.path.join(image_dir, "sample_0001.png")
98
99 # load the image
100 image = Image(
101 filename=filename,
102 operator="Hannes",
103 sample_id="0001",
104 date_acquired=datetime.datetime.today(),
105 scan_size=(1e-5, 1e-5),
106 )
107
108 # read the image from disk
109 image.read_image()
110
111 # perform some sample computations
112 print(
113 "The maximum intensity of {} is {}".format(
114 image.sample_id,
115 image.histogram().argmax(),
116 )
117 )
118 pixel_count = image.threshold(low=200).sum()
119 print(
120 "The area with intensity greater than 200 is {:0.3f} µm²".format(
121 pixel_count * image.pixel_area * 1e12
122 )
123 )