window_height: int,
display_size: tuple,
device_id: int = 0,
- brightness: float = 1.,
- contrast: float = 0.,
- temperature: float = 0.,
- tint: float = 0.,
debug: bool = True,
- dummy: bool = False,
- use_lookup: bool = False,
- use_files: bool = False,
+ dummy: bool = False
):
self.window_size = window_size
self.window_height = window_height
self.display_size = display_size
- self.match_histograms = False
+
self.show_debug = debug
self.dummy = dummy
- self.use_lookup = use_lookup
- self.use_files = use_files
self.camera = cv.VideoCapture(device_id, cv.CAP_V4L)
self.camera.set(cv.CAP_PROP_FRAME_HEIGHT, 1080.0)
self.homography = None
- self.lookup = None
- self.lookup_compression = 100
self.last_display = None
self.last_capture = None
self.last_recovered = None
print("calibration failed")
quit()
- if self.use_lookup == True:
- self.get_lookup()
-
- def get_lookup(
- self
- ) -> None:
-
- if self.use_files == True:
- self.lookup = np.load("lookup.npy")
- return
-
- lookup = None
-
- for (r, g, b) in [(0,0,0), (255,255,255)]:
- pixel = np.array([[[b, g, r]]], dtype=np.int8)
- pixel = cv.resize(pixel, self.display_size, interpolation=cv.INTER_NEAREST_EXACT)
- self.display(pixel)
-
- for i in range(100): # silly hack
- cv.waitKey(1)
- recovered = self.capture()
-
- error = np.copy(recovered.astype(np.int16))
- error[..., 0] -= b
- error[..., 1] -= g
- error[..., 2] -= r
-
- error = np.clip(error, -50, 255)
-
- if lookup is None:
- lookup = error
- else:
- lookup += error
- lookup = lookup // 2
-
- self.lookup = lookup
- np.save("lookup.npy", lookup)
-
- lookup += np.min(lookup)
- lookup = np.round(lookup * (255 / np.min(lookup)))
- cv.imwrite("lookup.jpg", lookup)
-
-
def display(
self,
image: np.ndarray
self.last_display = image
image = cv.resize(image, self.display_size, interpolation=cv.INTER_NEAREST_EXACT)
- image = cv.convertScaleAbs(image, alpha=self.contrast, beta=self.brightness) # contrast / brightness correction
- image[...,2] + self.temperature # color correction
- image[...,1] + self.tint
- image[...,0] - self.temperature
+ image[...,0] = np.round(image[...,0] * 0.9)
cv.imshow("display", image)
cv.waitKey(1)
- def debug(
- self
- ) -> None:
-
- if self.last_display is not None and self.last_capture is not None and self.last_recovered is not None:
- height = round(self.last_capture.shape[0] / 2)
- width = round((self.display_size[0] / self.display_size[1]) * height)
- last_display = cv.resize(self.last_display, (width, height))
- last_recovered = cv.resize(self.last_recovered, (width, height))
- comparison = np.concatenate((last_display, last_recovered), axis=0)
- debug_image = np.concatenate((self.last_capture, comparison), axis=1)
- cv.imshow("debug", debug_image)
- cv.waitKey(1)
-
def capture(
self
) -> np.ndarray:
image = cv.warpPerspective(image, self.homography, self.display_size)
image = cv.resize(image, (self.window_size, self.window_height))
- if type(self.lookup) == np.ndarray and self.use_lookup == True:
- image = np.clip(image + self.lookup, 0, 255).astype(np.uint8)
-
self.last_recovered = image
if self.show_debug == True:
return image
+ def debug(
+ self
+ ) -> None:
+
+ if self.last_display is not None and self.last_capture is not None and self.last_recovered is not None:
+
+ height = round(self.last_capture.shape[0] / 2)
+ width = round((self.display_size[0] / self.display_size[1]) * height)
+
+ last_display = cv.resize(self.last_display, (width, height))
+ last_recovered = cv.resize(self.last_recovered, (width, height))
+
+ comparison = np.concatenate((last_display, last_recovered), axis=0)
+ debug_image = np.concatenate((self.last_capture, comparison), axis=1)
+
+ cv.imshow("debug", debug_image)
+ cv.waitKey(1)
+