-
Notifications
You must be signed in to change notification settings - Fork 3
/
micrometer_calibrate.py
executable file
·76 lines (59 loc) · 2.06 KB
/
micrometer_calibrate.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python3
import argparse
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import math
import sys
import os
# Convert rgb to grayscale
# from https://stackoverflow.com/questions/12201577/how-can-i-convert-an-rgb-image-into-grayscale-in-python
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
# Process micrometer image
def process(jpg):
if not os.path.exists(jpg):
print("Please provide an image that exists",file=sys.stderr)
return
# Load image and threshold
micrometer = Image.open(jpg)
micrometer = rgb2gray(np.array(micrometer))
micrometer = micrometer > np.mean(micrometer)*0.83
width = micrometer.shape[1]
height = micrometer.shape[0]
pixels = np.zeros((width))
for y in range(0, height):
for x in range(0, width):
if not micrometer[y][x]:
pixels[x] = pixels[x] + 1
# Record the x position of each line
lines = []
for i in range(len(pixels)):
if pixels[i] > height * 0.25:
lines.append(i)
pos = [] # position of lines
yaxis = [] # y axis value for 'dot' for visualisation
lastcolumn = lines[0]
newcolumn = lines[0]
i = 0
# Find groups of pixels that make up micrometer lines
for curline in lines:
if curline >= lastcolumn + 2 or i == len(lines)-1:
pos.append((newcolumn + lastcolumn) / 2)
yaxis.append(height/2)
newcolumn = curline
lastcolumn = curline
i+=1
print("Number of micrometer lines",len(pos))
if len(pos) == 101:
print("Found all micrometer lines, length is: ",pos[-1] - pos[0]," pixels")
diffs = np.diff(pos)
print("Differences: max",np.max(diffs)," min",np.min(diffs)," mean",np.mean(diffs))
plt.scatter(pos, yaxis)
plt.imshow(micrometer)
plt.show() # show visualisation
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("image", help="Filename of micrometer image")
args = parser.parse_args()
process(args.image)