Move pixel shift to separate method. Change upper_skin

This commit is contained in:
DSTO\pivatom
2018-12-18 10:58:10 +10:30
parent cd11686cc7
commit df8bec3b3e

View File

@@ -26,27 +26,25 @@ class SimpleHandRecogniser(HandRecogniser):
""" """
Segments the hand from the rest of the image to get a threshold. Segments the hand from the rest of the image to get a threshold.
""" """
# Need to shift red pixels so they can be 0-20 rather than 250-~20
self.img_hsv[:,:,0] = self.img_hsv[:,:,0] + 30
self.img_hsv[:,:,0] = np.where(self.img_hsv[:,:,0] > 179, self.img_hsv[:,:,0] - 179, self.img_hsv[:,:,0])
self.img_hsv = cv2.GaussianBlur(self.img_hsv,(5,5),0) self.img_hsv = cv2.GaussianBlur(self.img_hsv,(5,5),0)
lower_skin = (0, 0, 153) lower_skin = (0, 0, 153)
upper_skin = (50, 153, 255) upper_skin = (45, 153, 255)
# Only need mask, as we can just use this to do the hand segmentation. # Only need mask, as we can just use this to do the hand segmentation.
self.mask = cv2.inRange(self.img_hsv, lower_skin, upper_skin) self.mask = cv2.inRange(self.img_hsv, lower_skin, upper_skin)
# Apply another blur to rmeove any small holes/noise
self.mask = self.__denoise(self.mask)
ret, self.mask = cv2.threshold(self.mask, 50, 255, cv2.THRESH_BINARY) ret, self.mask = cv2.threshold(self.mask, 50, 255, cv2.THRESH_BINARY)
def __denoise(self, image): def __denoise(self, image):
""" """
Applies a 5x5 gaussian blur to remove noise from the image. Applies a 5x5 gaussian blur to remove noise from the image.
""" """
image = cv2.GaussianBlur(image,(5,5),0) return cv2.GaussianBlur(image,(5,5),0)
def __calc_circle(self, image, radius_percent = 0.55): def __calc_circle(self, image, radius_percent = 0.52):
""" """
Calculates the equation of the circle (radius, centre) from a given Calculates the equation of the circle (radius, centre) from a given
threshold image, so that the circle is the center of gravity of the threshold image, so that the circle is the center of gravity of the
@@ -91,15 +89,24 @@ class SimpleHandRecogniser(HandRecogniser):
return radius, centre return radius, centre
def __shift_pixels(self, image, shift_radius):
image[:,:,0] = image[:,:,0] + shift_radius
return np.where(image[:,:,0] > 179, image[:,:,0] - 179, image[:,:,0])
def get_gesture(self): def get_gesture(self):
""" """
Calculates the actual gesture, returning the number of fingers Calculates the actual gesture, returning the number of fingers
seen in the image. seen in the image.
""" """
if not self.img: if self.img is None:
return 0 return 0
self.img_hsv = cv2.cvtColor(self.img, cv2.COLOR_BGR2HSV) self.img_hsv = cv2.cvtColor(self.img, cv2.COLOR_BGR2HSV)
# Need to shift red pixels so they can be 0-20 rather than 250-~20
self.img_hsv = self.__shift_pixels(self.img_hsv, 30)
self.img_hsv = self.__denoise(self.img_hsv)
self.__segment_image() self.__segment_image()
radius, centre = self.__calc_circle(self.mask) radius, centre = self.__calc_circle(self.mask)