This repository was archived by the owner on Jun 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanModule.py
More file actions
85 lines (71 loc) · 2.92 KB
/
Copy pathScanModule.py
File metadata and controls
85 lines (71 loc) · 2.92 KB
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
76
77
78
79
80
81
82
83
84
85
from __future__ import print_function
import time, numpy, sys, requests, threading
import cv2
import cv2.cv as cv
from CameraPi import Camera
objString = ""
class Scanner:
def preprocess(self, img, LASER_TRESHHOLD):
B, G, img = cv2.split(img) #Select the red channel only
ret, img = cv2.threshold(img, LASER_TRESHHOLD, 255, cv2.THRESH_TOZERO) #Remove noise
#Mask out everything thats not in the capture range
img = cv2.bitwise_and(img, img, mask=self.cropImage)
return img
def process(self, img, z, CAPTURE_HEIGHT):
#And write this information to te obj file as well
objString = ""
for diagonalNr in range(-(CAPTURE_HEIGHT-1), CAPTURE_HEIGHT):
maxPosition = numpy.diagonal(img, offset=diagonalNr).argmax()
if maxPosition < 5:
continue
if diagonalNr < 0:
posX = maxPosition
posY = abs(diagonalNr)+maxPosition
else:
posX = diagonalNr+maxPosition
posY = maxPosition
objString += "v " + str(posX) + " " + str(z*2) + " " + str(posY) + "\n"
return objString
def processThread(self, transformationMatrix, LASER_TRESHHOLD, CAPTURE_HEIGHT):
global objString
print("Process Thread running")
while self.cam.readable:
try: sliceNr, img = self.cam.getFrame(includeFramecounter=True)
except EOFError:
print("Exiting because camera not readable")
return #All images from the queue were processed
print("Processing slice " + str(sliceNr))
img = self.preprocess(img, LASER_TRESHHOLD)
img = cv2.warpPerspective(img, transformationMatrix, (CAPTURE_HEIGHT, CAPTURE_HEIGHT), flags=cv2.INTER_LANCZOS4) #Make sure we loose as low resolution as possible
objString += self.process(img, sliceNr, CAPTURE_HEIGHT)
def __init__(self, CAPTURE_WIDTH, CAPTURE_HEIGHT, STEPPER):
self.LASER_TRESHHOLD = 50
self.CAPTURE_WIDTH = 1640
self.CAPTURE_HEIGHT = 1232
self.transformationMatrix = numpy.loadtxt("calibration.txt") #Get the calibration Matrix
self.cropImage = cv2.imread("crop_image.png", 0)
self.cam = Camera(CAPTURE_WIDTH, CAPTURE_HEIGHT)
self.cam.startCapture()
STEPPER.goTop()
self.cam.release()
print("Waiting for camera to stop recording")
time.sleep(3)
print("Starting computation threads...")
t1 = threading.Thread(target=self.processThread, args=(self.transformationMatrix, self.LASER_TRESHHOLD, self.CAPTURE_HEIGHT))
t2 = threading.Thread(target=self.processThread, args=(self.transformationMatrix, self.LASER_TRESHHOLD, self.CAPTURE_HEIGHT))
t3 = threading.Thread(target=self.processThread, args=(self.transformationMatrix, self.LASER_TRESHHOLD, self.CAPTURE_HEIGHT))
t4 = threading.Thread(target=self.processThread, args=(self.transformationMatrix, self.LASER_TRESHHOLD, self.CAPTURE_HEIGHT))
#Wait for the image computation to finish
t1.start()
t2.start()
t3.start()
t4.start()
t1.join()
t2.join()
t3.join()
t4.join()
#Write the .obj file
print("Writing to .obj file test.obj")
f = open("test.obj", "w")
f.write(objString)
f.close()