mirror of
https://github.com/EyeTrackVR/EyeTrackVR.git
synced 2025-11-04 14:39:42 +08:00
fit circle crop in ransac3d
This commit is contained in:
parent
786c9e3d5f
commit
73fef266c6
@ -36,57 +36,48 @@ class EyeId(IntEnum):
|
|||||||
BOTH = 2
|
BOTH = 2
|
||||||
SETTINGS = 3
|
SETTINGS = 3
|
||||||
|
|
||||||
|
def circle_crop(self):
|
||||||
|
global cct
|
||||||
|
print(cct)
|
||||||
|
if cct == 0:
|
||||||
|
try:
|
||||||
|
ht, wd = self.current_image_gray.shape[:2]
|
||||||
|
radius = int(float(self.lkg_projected_sphere["axes"][0]))
|
||||||
|
self.xc = int(float(self.lkg_projected_sphere["center"][0]))
|
||||||
|
self.yc = int(float(self.lkg_projected_sphere["center"][1]))
|
||||||
|
if radius < 8: #minimum size
|
||||||
|
radius = 8
|
||||||
|
# draw filled circle in white on black background as mask
|
||||||
|
mask = np.zeros((ht, wd), dtype=np.uint8)
|
||||||
|
mask = cv2.circle(mask, (self.xc, self.yc), radius, 255, -1)
|
||||||
|
# create white colored background
|
||||||
|
color = np.full_like(self.current_image_gray, (255))
|
||||||
|
# apply mask to image
|
||||||
|
masked_img = cv2.bitwise_and(self.current_image_gray, self.current_image_gray, mask=mask)
|
||||||
|
# apply inverse mask to colored image
|
||||||
|
masked_color = cv2.bitwise_and(color, color, mask=255 - mask)
|
||||||
|
# combine the two masked images
|
||||||
|
self.current_image_gray = cv2.add(masked_img, masked_color)
|
||||||
|
return self.current_image_gray
|
||||||
|
except:
|
||||||
|
return self.current_image_gray
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
cct = cct - 1
|
||||||
|
return self.current_image_gray
|
||||||
def BLOB(self):
|
def BLOB(self):
|
||||||
|
global cct
|
||||||
# define circle
|
# define circle
|
||||||
if self.eye_id in [EyeId.LEFT]:
|
if self.eye_id in [EyeId.LEFT] and self.settings.gui_circular_crop_left:
|
||||||
if self.gui_circular_crop_left:
|
self.current_image_gray = circle_crop(self)
|
||||||
if self.cct == 0:
|
else:
|
||||||
try:
|
|
||||||
ht, wd = self.current_image_gray.shape[:2]
|
|
||||||
|
|
||||||
radius = int(float(self.lkg_projected_sphere["axes"][0]))
|
|
||||||
|
|
||||||
# draw filled circle in white on black background as mask
|
|
||||||
mask = np.zeros((ht, wd), dtype=np.uint8)
|
|
||||||
mask = cv2.circle(mask, (self.xc, self.yc), radius, 255, -1)
|
|
||||||
# create white colored background
|
|
||||||
color = np.full_like(self.current_image_gray, (255))
|
|
||||||
# apply mask to image
|
|
||||||
masked_img = cv2.bitwise_and(self.current_image_gray, self.current_image_gray, mask=mask)
|
|
||||||
# apply inverse mask to colored image
|
|
||||||
masked_color = cv2.bitwise_and(color, color, mask=255 - mask)
|
|
||||||
# combine the two masked images
|
|
||||||
self.current_image_gray = cv2.add(masked_img, masked_color)
|
|
||||||
except:
|
|
||||||
pass
|
pass
|
||||||
else:
|
|
||||||
self.cct = self.cct - 1
|
|
||||||
_, larger_threshold = cv2.threshold(self.current_image_gray, int(self.settings.gui_threshold + 12), 255, cv2.THRESH_BINARY)
|
|
||||||
else:
|
|
||||||
if self.gui_circular_crop_right:
|
|
||||||
if self.cct == 0:
|
|
||||||
try:
|
|
||||||
ht, wd = self.current_image_gray.shape[:2]
|
|
||||||
|
|
||||||
radius = int(float(self.lkg_projected_sphere["axes"][0]))
|
if self.eye_id in [EyeId.RIGHT] and self.settings.gui_circular_crop_right:
|
||||||
|
self.current_image_gray = circle_crop(self)
|
||||||
# draw filled circle in white on black background as mask
|
else:
|
||||||
mask = np.zeros((ht, wd), dtype=np.uint8)
|
|
||||||
mask = cv2.circle(mask, (self.xc, self.yc), radius, 255, -1)
|
|
||||||
# create white colored background
|
|
||||||
color = np.full_like(self.current_image_gray, (255))
|
|
||||||
# apply mask to image
|
|
||||||
masked_img = cv2.bitwise_and(self.current_image_gray, self.current_image_gray, mask=mask)
|
|
||||||
# apply inverse mask to colored image
|
|
||||||
masked_color = cv2.bitwise_and(color, color, mask=255 - mask)
|
|
||||||
# combine the two masked images
|
|
||||||
self.current_image_gray = cv2.add(masked_img, masked_color)
|
|
||||||
except:
|
|
||||||
pass
|
pass
|
||||||
else:
|
_, larger_threshold = cv2.threshold(self.current_image_gray, int(self.settings.gui_threshold), 255,
|
||||||
self.cct = self.cct - 1
|
|
||||||
_, larger_threshold = cv2.threshold(self.current_image_gray, int(self.settings.gui_threshold + 12), 255,
|
|
||||||
cv2.THRESH_BINARY)
|
cv2.THRESH_BINARY)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -108,42 +99,25 @@ def BLOB(self):
|
|||||||
for cnt in contours:
|
for cnt in contours:
|
||||||
(x, y, w, h) = cv2.boundingRect(cnt)
|
(x, y, w, h) = cv2.boundingRect(cnt)
|
||||||
|
|
||||||
# if our blob width/height are within suitable (yet arbitrary) boundaries, call that good.
|
# if our blob width/height are within boundaries, call that good.
|
||||||
#
|
|
||||||
# TODO This should be scaled based on camera resolution.
|
|
||||||
|
|
||||||
if not self.settings.gui_blob_minsize <= h <= self.settings.gui_blob_maxsize or not self.settings.gui_blob_minsize <= w <= self.settings.gui_blob_maxsize:
|
if not self.settings.gui_blob_minsize <= h <= self.settings.gui_blob_maxsize or not self.settings.gui_blob_minsize <= w <= self.settings.gui_blob_maxsize:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
cx = x + int(w / 2)
|
cx = x + int(w / 2)
|
||||||
|
|
||||||
cy = y + int(h / 2)
|
cy = y + int(h / 2)
|
||||||
|
|
||||||
# cv2.line(
|
cv2.drawContours(self.current_image_gray, [cnt], -1, (0, 0, 0), 3)
|
||||||
# self.current_image_gray,
|
|
||||||
# (x + int(w / 2), 0),
|
|
||||||
# (x + int(w / 2), rows),
|
|
||||||
# (255, 0, 0),
|
|
||||||
# 1,
|
|
||||||
# ) # visualizes eyetracking on thresh
|
|
||||||
# cv2.line(
|
|
||||||
# self.current_image_gray,
|
|
||||||
# (0, y + int(h / 2)),
|
|
||||||
# (cols, y + int(h / 2)),
|
|
||||||
# (255, 0, 0),
|
|
||||||
# 1,
|
|
||||||
# )
|
|
||||||
cv2.drawContours(self.current_image_gray, [cnt], -1, (255, 0, 0), 3)
|
|
||||||
cv2.rectangle(
|
cv2.rectangle(
|
||||||
self.current_image_gray, (x, y), (x + w, y + h), (255, 0, 0), 2
|
self.current_image_gray, (x, y), (x + w, y + h), (0, 0, 0), 2
|
||||||
)
|
)
|
||||||
|
|
||||||
#out_x, out_y = cal_osc(self, cx, cy) #filter and calibrate values
|
#out_x, out_y = cal_osc(self, cx, cy) #filter and calibrate values
|
||||||
|
|
||||||
|
print("S:KLJGHLDKIJGHLKS")
|
||||||
self.failed = 0
|
self.failed = 0
|
||||||
return cx, cy, larger_threshold
|
return cx, cy, larger_threshold
|
||||||
|
print("S:KLJGHLDKIJGHLKS")
|
||||||
self.failed = self.failed + 1
|
self.failed = self.failed + 1
|
||||||
return 0, 0, larger_threshold
|
return 0, 0, larger_threshold
|
||||||
|
|
||||||
@ -295,7 +295,9 @@ class EyeProcessor:
|
|||||||
self.current_algorithm = EyeInfoOrigin.RANSAC
|
self.current_algorithm = EyeInfoOrigin.RANSAC
|
||||||
|
|
||||||
def BLOBM(self):
|
def BLOBM(self):
|
||||||
|
print("LSKDGFHL")
|
||||||
self.rawx, self.rawy, self.thresh = BLOB(self)
|
self.rawx, self.rawy, self.thresh = BLOB(self)
|
||||||
|
|
||||||
self.out_x, self.out_y = cal.cal_osc(self, self.rawx, self.rawy)
|
self.out_x, self.out_y = cal.cal_osc(self, self.rawx, self.rawy)
|
||||||
self.current_algorithm = EyeInfoOrigin.BLOB
|
self.current_algorithm = EyeInfoOrigin.BLOB
|
||||||
|
|
||||||
|
|||||||
@ -488,7 +488,7 @@ class HSRAC_cls(object):
|
|||||||
# cv_end_time = timeit.default_timer()
|
# cv_end_time = timeit.default_timer()
|
||||||
# self.timedict["ransac"].append(cv_end_time - ransac_start_time)
|
# self.timedict["ransac"].append(cv_end_time - ransac_start_time)
|
||||||
# self.timedict["total_cv"].append(cv_end_time - cv_start_time)
|
# self.timedict["total_cv"].append(cv_end_time - cv_start_time)
|
||||||
print(radius)
|
# print(radius)
|
||||||
try:
|
try:
|
||||||
y, x = ori_frame.shape
|
y, x = ori_frame.shape
|
||||||
th_frame = cv2.resize(th_frame, (x, y))
|
th_frame = cv2.resize(th_frame, (x, y))
|
||||||
|
|||||||
@ -352,5 +352,5 @@ class IntensityBasedOpeness:
|
|||||||
#print(eyevec)
|
#print(eyevec)
|
||||||
if eyevec > 0.4:
|
if eyevec > 0.4:
|
||||||
print("BLINK LCOK")
|
print("BLINK LCOK")
|
||||||
print(eyeopen)
|
# print(eyeopen)
|
||||||
return eyeopen
|
return eyeopen
|
||||||
|
|||||||
@ -27,7 +27,13 @@ Copyright (c) 2023 EyeTrackVR <3
|
|||||||
'''
|
'''
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from enums import EyeLR
|
from enum import IntEnum
|
||||||
|
|
||||||
|
class EyeId(IntEnum):
|
||||||
|
RIGHT = 0
|
||||||
|
LEFT = 1
|
||||||
|
BOTH = 2
|
||||||
|
SETTINGS = 3
|
||||||
|
|
||||||
|
|
||||||
def ellipse_model(data, y, f):
|
def ellipse_model(data, y, f):
|
||||||
@ -135,15 +141,17 @@ def fit_rotated_ellipse(data, P):
|
|||||||
|
|
||||||
return (cx, cy, w, h, theta)
|
return (cx, cy, w, h, theta)
|
||||||
|
|
||||||
|
cct = 300
|
||||||
def circle_crop(self):
|
def circle_crop(self):
|
||||||
if self.cct == 0:
|
global cct
|
||||||
|
print(cct)
|
||||||
|
if cct == 0:
|
||||||
try:
|
try:
|
||||||
ht, wd = self.current_image_gray.shape[:2]
|
ht, wd = self.current_image_gray.shape[:2]
|
||||||
radius = int(float(self.lkg_projected_sphere["axes"][0]))
|
radius = int(float(self.lkg_projected_sphere["axes"][0]))
|
||||||
self.xc = int(float(self.lkg_projected_sphere["center"][0]))
|
self.xc = int(float(self.lkg_projected_sphere["center"][0]))
|
||||||
self.yc = int(float(self.lkg_projected_sphere["center"][1]))
|
self.yc = int(float(self.lkg_projected_sphere["center"][1]))
|
||||||
if radius < 8: #minimum size TODO: make shure this size is enough
|
if radius < 8: #minimum size
|
||||||
radius = 8
|
radius = 8
|
||||||
# draw filled circle in white on black background as mask
|
# draw filled circle in white on black background as mask
|
||||||
mask = np.zeros((ht, wd), dtype=np.uint8)
|
mask = np.zeros((ht, wd), dtype=np.uint8)
|
||||||
@ -156,13 +164,18 @@ def circle_crop(self):
|
|||||||
masked_color = cv2.bitwise_and(color, color, mask=255 - mask)
|
masked_color = cv2.bitwise_and(color, color, mask=255 - mask)
|
||||||
# combine the two masked images
|
# combine the two masked images
|
||||||
self.current_image_gray = cv2.add(masked_img, masked_color)
|
self.current_image_gray = cv2.add(masked_img, masked_color)
|
||||||
|
return self.current_image_gray
|
||||||
except:
|
except:
|
||||||
|
return self.current_image_gray
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
self.cct = self.cct - 1
|
cct = cct - 1
|
||||||
|
return self.current_image_gray
|
||||||
|
|
||||||
|
|
||||||
def RANSAC3D(self):
|
def RANSAC3D(self):
|
||||||
f = False
|
f = False
|
||||||
|
global cct
|
||||||
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
||||||
thresh_add = 10
|
thresh_add = 10
|
||||||
rng = np.random.default_rng()
|
rng = np.random.default_rng()
|
||||||
@ -176,15 +189,15 @@ def RANSAC3D(self):
|
|||||||
# crop the image earlier; it gives us less possible dark area to get confused about in the
|
# crop the image earlier; it gives us less possible dark area to get confused about in the
|
||||||
# next step.
|
# next step.
|
||||||
|
|
||||||
if self.eye_id == EyeId.LEFT and self.config.gui_circular_crop_left == True: #TODO TEST function
|
if self.eye_id in [EyeId.LEFT] and self.settings.gui_circular_crop_left:
|
||||||
circle_crop(self)
|
self.current_image_gray = circle_crop(self)
|
||||||
else:
|
else:
|
||||||
self.cct = 300
|
pass
|
||||||
|
|
||||||
if self.eye_id == EyeId.RIGHT and self.config.gui_circular_crop_right == True:
|
if self.eye_id in [EyeId.RIGHT] and self.settings.gui_circular_crop_right:
|
||||||
circle_crop(self)
|
self.current_image_gray = circle_crop(self)
|
||||||
else:
|
else:
|
||||||
self.cct = 300
|
pass
|
||||||
|
|
||||||
# Crop first to reduce the amount of data to process.
|
# Crop first to reduce the amount of data to process.
|
||||||
newFrame2 = self.current_image_gray.copy()
|
newFrame2 = self.current_image_gray.copy()
|
||||||
|
|||||||
@ -513,7 +513,7 @@ class SettingsWidget:
|
|||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
if self.config.gui_circular_crop_right != values[self.gui_circular_crop_right]:
|
if self.config.gui_circular_crop_right != values[self.gui_circular_crop_right]:
|
||||||
self.gui_circular_crop_right = values[self.gui_circular_crop_right]
|
self.config.gui_circular_crop_right = values[self.gui_circular_crop_right]
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
if self.config.gui_HSF_radius != int(values[self.gui_HSF_radius]):
|
if self.config.gui_HSF_radius != int(values[self.gui_HSF_radius]):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user