Example #1
0
 def threshold(self, frame):
     '''Returns a binary image with pixels falling into the appropriate hsv range.'''
     mask = cv2.inRange(frame, self.hsv_values[:, 0], self.hsv_values[:, 1])
     if self.hsv_values2 is not None:
         mask2 = cv2.inRange(frame, self.hsv_values2[:, 0], self.hsv_values2[:, 1])
         cv2.bitwise_or(mask, mask2, dst=mask)
     return mask
    def locate(self, geo_image, image, marked_image):
        '''Find sticks in image and return list of FieldItem instances.''' 
        # Extract out just blue channel from BGR image.
        #blue_channel, _, _ = cv2.split(image)
        #_, mask = cv2.threshold(blue_channel, 160, 255, 0)
        
        # Convert Blue-Green-Red color space to HSV
        hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        
        lower_blue = np.array([90, 90, 50], np.uint8)
        upper_blue = np.array([130, 255, 255], np.uint8)
        mask = cv2.inRange(hsv_image, lower_blue, upper_blue)
        
        # Night time testing
        lower_blue = np.array([90, 10, 5], np.uint8)
        upper_blue = np.array([142, 255, 255], np.uint8)
        mask = cv2.inRange(hsv_image, lower_blue, upper_blue)

        filtered_rectangles = []
        
        # Open mask (to remove noise) and then dilate it to connect contours.
        kernel = np.ones((5,5), np.uint8)
        mask_open = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
        mask = cv2.dilate(mask_open, kernel, iterations = 1)
        
        # Find outer contours (edges) and 'approximate' them to reduce the number of points along nearly straight segments.
        contours, hierarchy = cv2.findContours(mask.copy(), cv2.cv.CV_RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        #contours = [cv2.approxPolyDP(contour, .1, True) for contour in contours]
        
        # Create bounding box for each contour.
        bounding_rectangles = [cv2.minAreaRect(contour) for contour in contours]
        
        if marked_image is not None:
            for rectangle in bounding_rectangles:
                # Show rectangles using bounding box.
                drawRect(marked_image, rectangle, (0,0,0), thickness=2)
        
        # Remove any rectangles that couldn't be a plant based off specified size.
        min_stick_size = self.stick_diameter * 0.75 # looking straight down on it
        max_stick_size = self.stick_length * 1.25 # laying flat on the ground
        filtered_rectangles.extend(filter_by_size(bounding_rectangles, geo_image.resolution, min_stick_size, max_stick_size, enforce_min_on_w_and_h=True))
        
        if ImageWriter.level <= ImageWriter.DEBUG:
            # Debug save intermediate images
            mask_filename = postfix_filename(geo_image.file_name, 'blue_thresh')
            ImageWriter.save_debug(mask_filename, mask)
        
        if marked_image is not None:
            for rectangle in filtered_rectangles:
                # Show rectangles using colored bounding box.
                purple = (255, 0, 255)
                drawRect(marked_image, rectangle, purple, thickness=2)

        sticks = []
        for i, rectangle in enumerate(filtered_rectangles):
            # Just give default name for saving image until we later go through and assign to plant group.
            stick = FieldItem(name = 'stick' + str(i), bounding_rect = rectangle)
            sticks.append(stick)
                
        return sticks
Example #3
0
    def trackRobot(self, imagePath):
        '''this function track the robot and return its coordinates'''
        img = cv2.imread(imagePath)
        img = cv2.flip(img, 1)
        img = cv2.flip(img, 0)

        # convert into hsv 
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

        # Find mask that matches 
        green_mask = cv2.inRange(hsv, np.array((50., 30., 0.)), np.array((100., 255., 255.)))
        green_mask = cv2.erode(green_mask, None, iterations=2)
        green_mask = cv2.dilate(green_mask, None, iterations=2)

        green_cnts = cv2.findContours(green_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
        green_c = max(green_cnts, key=cv2.contourArea)

        # fit an ellipse and use its orientation to gain info about the robot
        green_ellipse = cv2.fitEllipse(green_c)

        # This is the position of the robot
        green_center = (int(green_ellipse[0][0]), int(green_ellipse[0][1]))

        red_mask = cv2.inRange(hsv, np.array((0., 100., 100.)), np.array((80., 255., 255.)))
        red_mask = cv2.erode(red_mask, None, iterations=2)
        red_mask = cv2.erode(red_mask, None, iterations=2)

        red_cnts = cv2.findContours(red_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
        red_c = max(red_cnts, key=cv2.contourArea)

        red_ellipse = cv2.fitEllipse(red_c)
        red_center = (int(red_ellipse[0][0]), int(red_ellipse[0][1]))


        return green_center, red_center   
Example #4
0
    def locate_tracker(self, debug):
        """
        Returns the (x, y) position of the IR tracker in the camera reference plane.
        http://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/

        :return: The (x, y) position of the IR tracker in the camera reference plane.
        :rtype: (int, int)
        """

        # tmp_image =
        # tmp_image = cv2.GaussianBlur(self.frame, (11, 11), 0)  # Experiment with this

        hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)  # Convert to HSV Color Space. This is temporary for testing using colored objects)

        mask = cv2.inRange(hsv, self.hueLower, self.hueUpper)

        try:
            mask = cv2.inRange(hsv, self.hueLower2, self.hueUpper2) + mask
        except AttributeError:
            pass

        mask = cv2.erode(mask, None, iterations=2)
        mask = cv2.dilate(mask, None, iterations=2)

        if debug:
            tmpMask = imutils.resize(mask, width=1000, height=1000)
            cv2.imshow("mask", tmpMask)


        # find contours in the mask and initialize the current (x, y) center of the object
        cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
        center = None

        # only proceed if at least one contour was found
        if len(cnts) > 0:
            # find the largest contour in the mask, then use
            # it to compute the minimum enclosing circle and
            # centroid
            c = max(cnts, key=cv2.contourArea)

            ((x, y), radius) = cv2.minEnclosingCircle(c)
            M = cv2.moments(c)
            center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

            # only proceed if the radius meets a minimum size
            # if radius > 10:
            #     # draw the circle and centroid on the frame,
            #     # then update the list of tracked points
            #     cv2.circle(frame, (int(x), int(y)), int(radius),
            #                (0, 255, 255), 2)
            #     cv2.circle(frame, center, 5, (0, 0, 255), -1)
            if debug:
                cv2.drawContours(self.frame, c, -1, (0, 255, 0), 20)
            return center, radius
        # update the points queue
        cv2.imshow("mask", imutils.resize(mask, width=1000, height=1000))
        cv2.imshow("frame", imutils.resize(self.frame, width=1000, height=1000))
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        raise OpenCVError("Could not find tracker!")
Example #5
0
    def _run_filter(self, img_bgr=None, img_gray=None):
        assert(img_bgr is not None)
        assert(len(img_bgr.shape) >= 3)
        assert(img_bgr.shape[2] == 3)
        assert(len(self._hue_range_to_list(self.hue_range)) == 1)  # FIXME

        img_hsv = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2HSV)

        vis_min = min(self.visibility_range)
        vis_max = max(self.visibility_range)

        assert(vis_min >= 0)
        assert(vis_max <= 256)

        for hue_range in self._hue_range_to_list(self.hue_range):
            hue_min = min(self.hue_range)
            hue_max = max(self.hue_range)

            assert(hue_min >= 0)
            assert(hue_max <= 256)

            #print('vis_min %d vis_max %d hue_min %d hue_max %d' % (vis_min, vis_max, hue_min, hue_max))
            img_match_h = cv2.inRange(img_hsv[:, :, 0], hue_min, hue_max)
            img_match_v = cv2.inRange(img_hsv[:, :, 2], vis_min, vis_max)
            img_match = np.minimum(img_match_h, img_match_v)
        return img_match
def green_centroid(image, lower, upper):
    hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
    mask = cv2.inRange(hsv, lower, upper)
    mask[40:100, 0:150] = 0
    #plt.imshow(mask)
    M = cv2.moments(mask)
    area = M['m00']
    if area > 0:
        cx = int(M['m10']/area)
        cy = int(M['m01']/area)
    else:
        mask = cv2.inRange(hsv, lower, upper)
        mask[70:100, 0:150] = 0
        M = cv2.moments(mask)
        area = M['m00']
        if area > 0:
            cx = int(M['m10']/area)
            cy = int(M['m01']/area)
        else:
            mask = cv2.inRange(hsv, lower, upper)
            M = cv2.moments(mask)
            area = M['m00']
            if area > 0:
                cx = int(M['m10']/area)
                cy = int(M['m01']/area)
            else:
                cx = None
                cy = None
    return area, cx, cy
def eliminateBackground(image):
    ##TODO:
    # - Add a discriminator
    # - Improve detection under weird lighting conditions
    # - Improve reliability of isolation in all areas

    ##Channels:
    #0: Blue Blocks
    #1: White Lines
    #2: Black Background (Decent - Don't rely on it)

    HSVimage = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    blue_lower = np.array([100, 66, 109], np.uint8)
    blue_upper = np.array([140, 255, 255], np.uint8)
    blue = cv2.inRange(HSVimage, blue_lower, blue_upper)
    #Isolation of low-high blue
    #Target element: Firing Blocks

    #This is the merge of the filtered blue and  g r layers to make a display image
    whitemax = np.array([180, 29, 255], np.uint8)
    white_lower = np.array([0, 0, 145], np.uint8)
    wht = cv2.inRange(HSVimage, white_lower, whitemax) - blue
    #This is the isolation of the white or very white objects.
    #Target element: Lines

    blackmax = np.array([180, 185, 185], np.uint8)
    black_lower = np.array([0, 0, 0], np.uint8)
    blk = cv2.inRange(HSVimage, black_lower, blackmax) - blue
    #Isolation of black
    #Target element: Background

    return cv2.merge((blue, wht, blk))
Example #8
0
    def find_position ( self, frame ):
        pos_x = self.last_x
        pos_y = self.last_y

        hsv_img = cv2.cvtColor( frame, cv2.COLOR_BGR2HSV )
        mask1 = cv2.inRange( hsv_img, self.lower_red_l, self.lower_red_h )
        mask2 = cv2.inRange( hsv_img, self.upper_red_l, self.upper_red_h )
       
        mask = mask1 + mask2       

        mask = cv2.erode( mask, self.kernel )
        mask = cv2.dilate( mask, self.kernel )

        mask = cv2.dilate( mask, self.kernel ) 
        mask = cv2.erode( mask, self.kernel )

        o_moments = cv2.moments( mask )
        d_m01 = o_moments['m01']
        d_m10 = o_moments['m10']
        d_area = o_moments['m00']
        print "MOMENTS " + str(d_m01) + " " + str(d_m10) + " " + str(d_area)

        if d_area > 10000:
            pos_x = int(d_m10 / d_area)
            pos_y = int(d_m01 / d_area)
            print "[" + str(pos_x) + " , " + str(pos_y) + "]"
        return pos_x, pos_y 
    def processImage(self, image_msg):
        image_bgr = self.bridge.imgmsg_to_cv2(image_msg, desired_encoding='passthrough')
        # equivalent to 
        # >>> desired_encoding='bgr8' or 'bgra8'
        image_hsv = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2HSV)

        lower_green = np.array([40,100,100])
        upper_green = np.array([70,255,255])

        lower_red1 = np.array([160, 100, 100])
        upper_red1 = np.array([180, 255, 255])

        lower_red2 = np.array([0, 100, 100])
        upper_red2 = np.array([10, 255, 255])

        mask_red = cv2.inRange(image_hsv, lower_red1, upper_red1) | cv2.inRange(image_hsv, lower_red2, upper_red2)
        mask_green = cv2.inRange(image_hsv, lower_green, upper_green)
        res = cv2.bitwise_and(image_bgr, image_bgr, mask= mask_green)

        # print "red: ", np.sum(mask_red)
        # print "green: ", np.sum(mask_green)
        if np.sum(mask_red) > 3000000:
            self.publisher_color.publish("red")
        # green is a bit hard to detect
        elif np.sum(mask_green) > 2000000: 
            self.publisher_color.publish("green")
        else:
            self.publisher_color.publish("none red or green")
Example #10
0
    def tower_pos(self, context):
        img = context["engine"]["frame"][
            self.tower_line_top : self.tower_line_top + self.tower_line_height,
            self.tower_left : self.tower_left + self.tower_width,
        ]
        img2 = cv2.resize(img, (self.tower_width, 100))
        img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        for i in range(2):
            img2[20:40, :, i] = cv2.resize(img_hsv[:, :, 0], (self.tower_width, 20))
            img2[40:60, :, i] = cv2.resize(img_hsv[:, :, 1], (self.tower_width, 20))
            img2[60:80, :, i] = cv2.resize(img_hsv[:, :, 2], (self.tower_width, 20))

        # ゲージのうち信頼できる部分だけでマスクする
        img3 = np.minimum(img, self.ui_tower_mask)

        # 白い部分にいまヤグラ/ホコがある
        img3_hsv = cv2.cvtColor(img3, cv2.COLOR_BGR2HSV)
        white_mask_s = cv2.inRange(img3_hsv[:, :, 1], 0, 8)
        white_mask_v = cv2.inRange(img3_hsv[:, :, 2], 248, 256)
        white_mask = np.minimum(white_mask_s, white_mask_v)
        x_list = np.arange(self.tower_width)
        tower_x = np.extract(white_mask[3, :] > 128, x_list)
        tower_xPos = np.average(tower_x)

        # FixMe: マスクした関係が位置がずれている可能性があるので、適宜補正すべき

        xPos_pct = (tower_xPos - self.tower_width / 2) / (self.tower_width * 0.86 / 2) * 100

        # あきらかにおかしい値が出たらとりあえず排除
        if xPos_pct < -120 or 120 < xPos_pct:
            xPos_pct = Nan

        return xPos_pct
Example #11
0
def not_colored(im):
    lower_red = np.array([0,0,80], dtype=np.uint8)
    upper_red = np.array([60,60,200], dtype=np.uint8)
    bwmask = cv2.inRange(im, lower_red, upper_red)
    bwmask = 255 - bwmask
    #thresh = .22
    black = 0
    for y in range(32):
        for x in range(32):
            pixel = bwmask[y][x]
            d = pixel
            if d == 0:
                black = black + 1
    blackpercent = float(black)/(32.0*32.0)
    if blackpercent > .1:
        return False
    lower_blue = np.array([110,90,15], dtype=np.uint8)
    upper_blue = np.array([165,120,85], dtype=np.uint8)
    bwmask = cv2.inRange(im, lower_blue, upper_blue)
    bwmask = 255 - bwmask
    #thresh = .22
    black = 0
    for y in range(32):
        for x in range(32):
            pixel = bwmask[y][x]
            d = pixel
            if d == 0:
                black = black + 1
    blackpercent = float(black)/(32.0*32.0)
    if blackpercent > .1:
        return False
    return True
Example #12
0
    def calibrate_hsv(self, img):        
        element = self.element
        result = np.zeros((img.shape[0], img.shape[1]), np.uint8)
        img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        h,s,v = cv2.split(img_hsv)
        d = cv2.inRange(h, np.array([self.current_conf[0]],np.uint8), 
                           np.array([self.current_conf[1]],np.uint8))
        d2 = cv2.inRange(h, np.array([self.current_conf[2]],np.uint8), 
                            np.array([self.current_conf[3]],np.uint8))
        d = cv2.bitwise_or(d, d2)
        d = cv2.erode(d, element)
        d = cv2.dilate(d, element)
        d = cv2.dilate(d, element)
        d = cv2.dilate(d, element)
        d = cv2.dilate(d, element)
        result = d

        res = result.copy()
        contours, hier = cv2.findContours(res, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
        self.current_rect_count = len(contours)
        self.rects = self.choose_contour(contours)
        if len(self.rects) > 0:
            self.current_biggest_rect_area = area(self.rects[0])
        else:
            self.current_biggest_rect_area = 0
        middle_roi = get_roi(result, self.middle_rect)
        self.current_middle_non_zero = cv2.countNonZero(middle_roi)

        self.feedback()
def find_marker(image, red_thres, green_thres, sat_thres):

    # h,w, channels = img.shape

    # get red and sat
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    blue, green, red = cv2.split(image)
    hue, sat, val = cv2.split(hsv)

    # find the marker by looking for red, with high saturation
    sat = cv2.inRange(sat, np.array((sat_thres[0])), np.array((sat_thres[1])))
    red = cv2.inRange(red, np.array((red_thres[0])), np.array((red_thres[1])))
    green = cv2.inRange(green, np.array((green_thres[0])), np.array((green_thres[1])))
    # AND the two thresholds, finding the car
    car = cv2.multiply(red, sat)
    car = cv2.multiply(car, green)

    # remove noise (not doing it now because the POIs are very small)
    # elem = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
    # car = cv2.erode(car,elem, iterations=1)
    # car = cv2.dilate(car,elem, iterations=3)
    # return cv2.boundingRect(car)

    img, contours, hierarchy = cv2.findContours(car.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # import ipdb; ipdb.set_trace()

    # return cv2.boundingRect(contours[1])
    return map(lambda x: cv2.boundingRect(x), contours)
def threshold_image_for_tape(image):
    """
    Thresholds image for reflective tape with light shined on it. This means it
    looks for pixels that are almost white, makes them white, and makes
    everything else black.

    Parameters:
        :param: `image` - the source image to threshold from
    """
    orig_image = numpy.copy(image)
    # print orig_image.size
    orig_image = cv2.medianBlur(orig_image, 3)
    # orig_image[orig_image > 100] = 255
    # return orig_image[orig_image > 100]
    height, width = orig_image.shape[0], orig_image.shape[1]
    eight_bit_image = numpy.zeros((height, width, 1), numpy.uint8)
    cv2.inRange(orig_image,
                (B_RANGE[0], G_RANGE[0], R_RANGE[0], 0),
                (B_RANGE[1], G_RANGE[1], R_RANGE[1], 100),
                eight_bit_image)
    # # eight_bit_image = cv2.adaptiveThreshold(orig_image,
    # #                             255,
    # #                             cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
    # #                             cv2.THRESH_BINARY,
    # #                             8,
    # #                             0)
    # cv2.medianBlur(eight_bit_image, 9)
    return eight_bit_image
def threshold_for_contours(img_from_array):
    hsvimg = cv2.cvtColor(img_from_array, cv2.COLOR_BGR2HSV)

    #print type(hsvimg)
    lower_blue = np.array([110, 50, 50], np.uint8)
    #estimated values until testing
    upper_blue = np.array([140, 255, 255], np.uint8)
    #estimated values until testing

    #threshold_for_contours boundaries to only get orange colors
    lower_orange = np.array([5, 100, 100], np.uint8)
    #estimated values until testing
    upper_orange = np.array([27, 255, 255], np.uint8)
    #estimated values until testing

    #heres where it is actually threshold_for_contourse
    #using the boundaries to get specified colors
    #print 'Type:', hsvimg.dtype, 'Other bullshit:', hsvimg.shape
    blue = cv2.inRange(hsvimg, lower_blue, upper_blue)
    orange = cv2.inRange(hsvimg, lower_orange, upper_orange)
    #blue and orange are binary threshholded images
    #if the image initially had a blue piece,
    #blue will have values of zero and 1
    # while orange will just 0's and vice versa
    contours_blue, hierarchy_blue = cv2.findContours(blue, 1, 2)
    contours_orange, hierarchy_orange = cv2.findContours(orange, 1, 2)
    #for some reason contours only has 1 element in it at the most
    return contours_blue, contours_orange
Example #16
0
    def detect_object(self, img):
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

        mask = cv2.inRange(hsv, self.lower, self.upper)
        mask = cv2.blur(mask, (7, 7))

        img_filter = cv2.bitwise_and(img, img, mask=mask)
        gray = cv2.cvtColor(img_filter, cv2.COLOR_BGR2GRAY)
        contours, _ = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

        max_idx = -1
        max_area = -1
        for i, cnt in enumerate(contours):
            area = cv2.contourArea(cnt)
            if area > max_area and area > self.min_area:
                max_idx = i
                max_area = area

        if max_idx > -1:
            cnt = contours[max_idx]
            x, y, h, w = cv2.boundingRect(cnt)
            self.track_window = (x, y, h, w)

            cv2.rectangle(img, (x, y), (x + h, y + w), (0, 255, 0), 2)
            self.img = img
            # cv2.imshow('track', img)
            # cv2.waitKey(1)

            hsv_roi = hsv[y : y + w, x : x + h]
            mask_roi = mask[y : y + w, x : x + h]

            mask_roi = cv2.inRange(hsv_roi, self.lower, self.upper)
            self.hist_track = cv2.calcHist([hsv_roi], [0], mask_roi, [180], [0, 180])
            cv2.normalize(self.hist_track, self.hist_track, 0, 255, cv2.NORM_MINMAX)
            return self.track_window
Example #17
0
def hsv_to_im_mask(im_hsv, hsv_lows, hsv_highs, is_bucket=False, is_arm=False):
    if is_bucket:
        # mask by threshold
        im_mask = cv2.inRange(im_hsv, hsv_lows, hsv_highs)
        im_mask = cv2.medianBlur(im_mask, 7)
        # erode
        im_mask = cv2.erode(im_mask, None, iterations=2)
        # dilate
        im_mask = cv2.dilate(im_mask, None, iterations=3)
    elif is_arm:
        # mask by threshold
        im_mask = cv2.inRange(im_hsv, hsv_lows, hsv_highs)
        im_mask = cv2.medianBlur(im_mask, 9)
        # erode
        # im_mask = cv2.erode(im_mask, None, iterations=2)
        # dilate
        im_mask = cv2.dilate(im_mask, None, iterations=3)
    else:
        # mask by threshold
        im_mask = cv2.inRange(im_hsv, hsv_lows, hsv_highs)
        im_mask = cv2.medianBlur(im_mask, 5)
        # erode
        # im_mask = cv2.erode(im_mask, None, iterations=2)
        # dilate
        im_mask = cv2.dilate(im_mask, None, iterations=3)
    return im_mask
Example #18
0
def dark_clouds(image_count):

	# LOAD THE IMAGE, CONVERT IT TO GRAYSCALE, AND BLUR IT
	# SLIGHTLY TO REMOVE HIGH FREQUENCY EDGES THAT WE AREN'T
	# INTERESTED IN
	img = cv2.imread("images/sky_region/ExtractedSky" + str(image_count) + ".jpg")
	original = img.copy()

	# FIND AND COUNT THE NUMBER OF BLACK PIXELS IN AN IMAGE
	BLACK = np.array([0,0,0],np.uint8)
	blackRange = cv2.inRange(img,BLACK,BLACK)
	no_black_pixels = cv2.countNonZero(blackRange)

	# CONVERT BGR TO HSV
	hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

	# DEFINE RANGE OF GREY COLOR IN HSV
	lower_grey = np.array([90,0,0], dtype=np.uint8)
	upper_grey = np.array([130,255,125], dtype=np.uint8)

	# THRESHOLD THE HSV IMAGE TO GET ONLY GREY COLORS
	mask = cv2.inRange(hsv, lower_grey, upper_grey)

	# COUNT NUMBER OF GREY PIXELS
	no_grey_pixels = cv2.countNonZero(mask)

	# BITWISE-AND MASK AND ORIGINAL IMAGE
	res = cv2.bitwise_and(original,original, mask= mask)

	'''cv2.imshow("masked grey sky",res)'''

	# GET THE TOTAL NUMBER OF PIXELS
	total_pixels = original.size / 3

	# GET THE NUMBER OF PIXELS IN THE SKY REGION OF AN IMAGE
	sky_region_pixels = total_pixels - no_black_pixels


	# CALCULATE THE PERCENTAGE OF THE COLOUR grey PRESENT IN THE SKY
	if no_grey_pixels == 0:
		return("There is no grey pixels in the image." )
	else:
		grey_percentage = (no_grey_pixels / sky_region_pixels) * 100

		'''print("The total number of pixels is: " + str(total_pixels))
		print("The number of grey pixels is: " + str(no_grey_pixels))
		print("The number of black pixels is: " + str(no_black_pixels))
		print("The number of pixels of the sky region is : " + str(sky_region_pixels))'''
		print("The percentage of grey in the sky region is : " + str(grey_percentage))

		if grey_percentage > 70:
			return("Severe stormy skies.\n" )
		elif grey_percentage > 50 and grey_percentage <= 70:
			return("Very Stormy skies.\n" )
		elif grey_percentage > 30 and grey_percentage <= 50:
			return("Some stormy skies.\n" )
		elif grey_percentage > 9 and grey_percentage <= 30:
			return("Scattered rain clouds.\n" )
		else:
			return("The sky is overcast.\n")
def calculate_skin_mask (skin_color, image):
	lower_bound = np.subtract(skin_color,(2,130,40))
	lower_bound = lower_bound[0][0]
	upper_bound = np.add(skin_color,(4,130,150))
	upper_bound = upper_bound[0][0]
	for i in range(1,3):
		if lower_bound[i] < 0:
			lower_bound[i] = 0
		if upper_bound[i] > 255:
			upper_bound[i] = 255
	if lower_bound[0] < 0:
		mask_1 = cv2.inRange(image,
							 np.array([180+lower_bound[0],lower_bound[1],lower_bound[2]]),
							 np.array([179,upper_bound[1],upper_bound[2]]))
		mask_2 = cv2.inRange(image,
							 np.array([0,lower_bound[1],lower_bound[2]]),upper_bound)
		return (mask_1 | mask_2)
	elif upper_bound[0] > 179:
		mask_1 = cv2.inRange(image,
							 lower_bound,
							 np.array([179,upper_bound[1],upper_bound[2]]))
		mask_2 = cv2.inRange(image,
							 np.array([0,lower_bound[1],lower_bound[2]]),
							 np.array([upper_bound[0] - 180,upper_bound[1],upper_bound[2]]))
		return (mask_1 | mask_2)
	else:
		mask_1 = cv2.inRange(image,lower_bound,upper_bound)
		return mask_1
Example #20
0
def find_squares(img):
    #img = cv2.GaussianBlur(img, (5, 5), 0)
    squares = []

    imgHSV = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    hChannelImg = imgHSV[:,:,0]
    sChannelImg = imgHSV[:,:,1]

    blurredHImg = cv2.GaussianBlur(hChannelImg,(11,11),0,0)
    blurredSImg = cv2.GaussianBlur(sChannelImg,(11,11),0,0)
    hThreshImg = cv2.inRange(blurredHImg,0,10)
    sThreshImg = cv2.inRange(blurredSImg,155,255)
    combImg = cv2.bitwise_and(hThreshImg,sThreshImg)

    for gray in cv2.split(img):
        for thrs in xrange(0, 255, 26):
            if thrs == 0:
                #finds the edges os the square using canny and dilate
                bin = cv2.Canny(gray, 0, 20, apertureSize=5)
                bin = cv2.dilate(bin, None)
            else:
                retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY)
            contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
            for cnt in contours:
                cnt_len = cv2.arcLength(cnt, True)
                cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True)
                if len(cnt) == 4 and cv2.contourArea(cnt) > 1000 and cv2.isContourConvex(cnt):
                    cnt = cnt.reshape(-1, 2)
                    max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)])
                    if max_cos < 0.1:
                        squares.append(cnt)
    return squares
Example #21
0
 def circles(self,cv_image):
     cv_image=cv2.resize(cv_image,dsize=(self.screen['width'],self.screen['height']))
     #if self.blur:
     #    cv_image=cv2.GaussianBlur(cv_image,ksize=[5,5],sigmaX=0)
     
     channels=cv2.split(cv_image)
     channels[0] = cv2.equalizeHist(channels[0])
     channels[1] = cv2.equalizeHist(channels[1])
     #channels[2] = cv2.equalizeHist(channels[2])
     img = cv2.merge(channels, cv_image)
     img=cv2.bilateralFilter(img, -1, 5, 0.1)
     kern = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
     img=cv2.morphologyEx(img, cv2.MORPH_CLOSE, kern)
     hsvImg=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
     luvImg=cv2.cvtColor(img,cv2.COLOR_BGR2LUV)
     gauss = cv2.GaussianBlur(luvImg, ksize=(5,5), sigmaX=10)
     sum = cv2.addWeighted(luvImg, 1.5, gauss, -0.6, 0)
     enhancedImg = cv2.medianBlur(sum, 3)
     ch=cv2.split(enhancedImg)
     mask = cv2.inRange(ch[2],self.highThresh[2],self.lowThresh[2])
     mask1=cv2.inRange(ch[1],self.highThresh[0],self.lowThresh[0])
     mask2=cv2.inRange(ch[2],self.highThresh[1],self.lowThresh[1])
     
    # cv2.imshow(mask)
     #cv2.imshow(mask1)
     #cv2.imshow(mask2)
     mask_out=cv2.cvtColor(mask,cv2.COLOR_GRAY2BGR)
     try:
         self.image_filter_pub.publish(self.bridge.cv2_to_imgmsg(mask_out, encoding="bgr8"))
     except CvBridgeError as e:
         rospy.logerr(e)
def hueBasedTracking(frame):
    #useful for smoothening the images, median of all the pixels under kernel 
    #area and central element is replaced with this median value. 
    #highly effective against salt-and-pepper noise in the images.
    frame = cv2.medianBlur(frame,5)

    #convert color from BGR to HSV
    hsv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    
    #define lower and upper limit of hue range for red color
    lower_red_hue_range = cv2.inRange(hsv_image, np.array([0, 100, 100]), np.array([10, 255, 255]))
    upper_red_hue_range = cv2.inRange(hsv_image, np.array([160, 100, 100]), np.array([179, 255, 255]))

    #Calculates the weighted sum of two arrays.
    red_hue_image = cv2.addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 2.0, 0.0)
    
    #Blurs an image using a Gaussian filter
    #The function convolves the source image with the specified Gaussian kernel
    red_hue_image = cv2.GaussianBlur(red_hue_image, (9, 9), 2, 2)

    #find contours in the red_hue_image formed after weighted adding of lower and upper ranges of red
    cnts = cv2.findContours(red_hue_image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
    center = None

    # only proceed if at least one contour was found
    if len(cnts) > 0:
	    # find the largest contour in the mask, then use it to compute the minimum enclosing circle and centroid
	    c = max(cnts, key=cv2.contourArea)
	    ((x, y), radius) = cv2.minEnclosingCircle(c)
	    M = cv2.moments(c)
	    #gives x and y coordinates of center
	    center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
    return center #returns the centroid for th image
Example #23
0
	def update(self):
		data = self.get_input("In")
		if data == None: return

		model = self.get_combo('Model')
		data = data.convert(model)

		min_val = [self.get_int(label) for label in self.ch_min]
		max_val = [self.get_int(label) for label in self.ch_max]

		min0 = list(min_val)
		min1 = list(min_val)

		max0 = list(max_val)
		max1 = list(max_val)
		flip = False
		for i in range(len(min_val)):
			if min_val[i] > max_val[i]:
				max0[i] = 255
				min1[i] = 0
				flip = True

		gray = cv2.inRange(data.img, np.array(min0), np.array(max0))
		if flip:
			gray1 = cv2.inRange(data.img, np.array(min1), np.array(max1))
			gray = cv2.bitwise_or(gray, gray1)

		self.set_output('Out', ImageGray(gray))
Example #24
0
def procesar(img):#default es rojo
    bilBlur = cv2.bilateralFilter(img,7,75,75)
    hsv = cv2.cvtColor(bilBlur, cv2.COLOR_BGR2HSV)
    mask =  cv2.inRange(hsv,rango['control'][0],rango['control'][1])# + cv2.inRange(hsv,rango['piel'][0],rango['piel'][1]) + cv2.inRange(hsv,rango['rojo'][0],rango['rojo'][1]) 
    #solo la mascara del control si se comenta la suma
    mk1 = cv2.inRange(hsv,rango['control'][0],rango['control'][1])# + cv2.inRange(hsv,rango['piel'][0],rango['piel'][1]) + cv2.inRange(hsv,rango['rojo'][0],rango['rojo'][1]) 
    return mask, mk1
Example #25
0
    def __getMask(self, image):
        """ Get binary mask of the image 
            This algorithm has been referenced from 
            http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html
            """
        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

        lower_blue = np.array([115,50,50])
        upper_blue = np.array([125,255,255])

        lower_red = np.array([-3,50,50])
        upper_red = np.array([3,255,255])

        lower_green = np.array([50,50,50])
        upper_green = np.array([70,255,255])

        maskBlue = cv2.inRange(hsv, lower_blue, upper_blue)
        maskBlue = cv2.blur(maskBlue,(20,20))
        ret, maskBlue = cv2.threshold(maskBlue,127,255,cv2.THRESH_BINARY)

        maskRed = cv2.inRange(hsv, lower_red, upper_red)
        maskRed = cv2.blur(maskRed,(20,20))
        ret, maskRed = cv2.threshold(maskRed, 127, 255,cv2.THRESH_BINARY)

        return maskRed, maskBlue
 def detectRover(self, argFrame):
     frame    = self.frame
     hsvFrame = self.frame
     thresh   = self.frame[:,:,0]
     rGreen = (38,67,155,198,0,255)
     rPink = (165,182,155,192,0,255)
     hsvFrame  = cv2.cvtColor(self.frame.copy(), cv2.COLOR_BGR2HSV)
     thresh = cv2.inRange(hsvFrame.copy(),np.array([rGreen[0],rGreen[2],rGreen[4]]),np.array([rGreen[1],rGreen[3],rGreen[5]]))
     thresh = cv2.medianBlur(thresh.copy(),5)
     thresh = cv2.erode(thresh.copy(), erodeElem)
     #thresh = cv2.erode(thresh.copy(), erodeElem)
     thresh = cv2.dilate(thresh.copy(), dilateElem)
     thresh = cv2.dilate(thresh.copy(), dilateElem)
     _, contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
     if len(contours) != 1:
         return -1
     (x,y,w,h) = cv2.boundingRect(contours[0])
     greenPt = (int((x+x+w)/2),int((y+y+h)/2))
     thresh = cv2.inRange(hsvFrame.copy(),np.array([rPink[0],rPink[2],rPink[4]]),np.array([rPink[1],rPink[3],rPink[5]]))
     thresh = cv2.medianBlur(thresh.copy(),5)
     thresh = cv2.erode(thresh.copy(), erodeElem)
     #thresh = cv2.erode(thresh.copy(), erodeElem)
     thresh = cv2.dilate(thresh.copy(), dilateElem)
     thresh = cv2.dilate(thresh.copy(), dilateElem)
     _, contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
     if len(contours) != 1:
         return -1
     (x,y,w,h) = cv2.boundingRect(contours[0])
     pinkPt = (int((x+x+w)/2),int((y+y+h)/2))
     self.roverPos = (int((greenPt[0]+pinkPt[0])/2),int((greenPt[1]+pinkPt[1])/2))
     angle = getAngle(pinkPt[0],pinkPt[1],greenPt[0],greenPt[1])
     self.roverHeading = 360+angle[2]*-1
     return greenPt, pinkPt
    def execute(self):
        #GETTING THE IMAGES
        imageLeft = self.sensor.getImageLeft()
        imageRight = self.sensor.getImageRight()

        iLhsv = cv2.cvtColor(imageLeft, cv2.COLOR_RGB2HSV)
        iRhsv = cv2.cvtColor(imageRight, cv2.COLOR_RGB2HSV)

        lowpass = (0, 207, 69)
        highpass = (10, 255, 198)

        ILhsvInRangeLeft = cv2.inRange(iLhsv, lowpass, highpass)
        ILhsvInRangeRight = cv2.inRange(iRhsv, lowpass, highpass)

        IlhsvInRange3channelsLeft = np.dstack((ILhsvInRangeLeft, ILhsvInRangeLeft, ILhsvInRangeLeft))
        IlhsvInRange3channelsRight = np.dstack((ILhsvInRangeRight, ILhsvInRangeRight, ILhsvInRangeRight))

        # Add your code here
        print "Runing"

        #EXAMPLE OF HOW TO SEND INFORMATION TO THE ROBOT ACTUATORS
        w = angle2Cams(ILhsvInRangeRight, ILhsvInRangeLeft)
        v = getSpeed(w, 2, 2)
        self.sensor.setV(v)
        print 'speed: ' + str(v) + ', angle: ' + str(w)

        self.sensor.setW(w)

        #SHOW THE FILTERED IMAGE ON THE GUI
        self.setRightImageFiltered(IlhsvInRange3channelsRight)
        self.setLeftImageFiltered(IlhsvInRange3channelsLeft)
	def track_number_callback(self, im, arg):

		pub, msg = arg
		target_pos=(400, 210)
		try:
			bridge = CvBridge()
		        #convert ROS image to opencv matrix
			cv_image = bridge.imgmsg_to_cv2(im, "bgr8")

			hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
		
			lowredl = np.array([0, 0, 255])
			lowredu = np.array([0, 255, 255])

			upredl = np.array([150, 0, 255])
			upredu = np.array([179, 255, 255])	
			
			mlow = cv2.inRange(hsv, lowredl, lowredu)
			mup = cv2.inRange(hsv, upredl, upredu)
			m=cv2.bitwise_or(mlow,mup)
		
			res=cv2.bitwise_and(cv_image,cv_image, mask= m)
		
			res=cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
			retval, binary=cv2.threshold(res,127,255,cv2.THRESH_BINARY)
			binary=toBinary(binary)
		
			red_pos=self.average_pixel_pos(binary)
			print red_pos
		
		except CvBridgeError,  e:
			rospy.loginfo(e)
			raise rospy.ServiceException("Tracking Failed")
Example #29
0
def getColor(img, color):
	hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
	
	h_h = color + 7
	l_h = color - 7
	
	if l_h < 0:
		l_h = 180 + l_h
		h_h = h_h
		h_color = np.array([h_h, 255, 255])
		l_color = np.array([l_h, 100, 50])
		red_color = [np.array([0, 100, 50]), np.array([180, 255, 255])]
		
		img_mask1 = cv2.inRange(hsv, red_color[0], h_color)
		img_mask2 = cv2.inRange(hsv, l_color, red_color[1])
		
		img_mask = img_mask1 + img_mask2
		
	else:
		h_color = np.array([h_h, 255, 255])
		l_color = np.array([l_h, 150, 10])
		
		img_mask = cv2.inRange(hsv, l_color, h_color)

	img_color = cv2.bitwise_and(img, img, mask = img_mask)
	
	return img_color
def CenterOfMass(image):
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    img2 = image[:, :, ::-1].copy()

    lowera = np.array([160, 100, 0])
    uppera = np.array([180, 250, 255])
    lowerb = np.array([0, 100, 0])  # It was 100 instead of 195
    upperb = np.array([5, 250, 255])

    mask1 = cv2.inRange(hsv, lowera, uppera)
    mask2 = cv2.inRange(hsv, lowerb, upperb)

    mask = cv2.add(mask1, mask2)

    kernel = np.ones((5, 5),np.uint8)
    mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
    mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)

    mj=sum(mask)
    mi=sum(np.transpose(mask))
    A=mask.shape
    ni=np.array(range(A[0]))
    nj=np.array(range(A[1]))
    M=sum(sum(mask))
    if sum(mi)==0 or sum(mj)==0:
        print "no ball"
        xcm=0
        ycm=0
    else:
        xcm=np.dot(mj,nj)/sum(mj)
        ycm=np.dot(mi,ni)/sum(mi)

    CM=[ycm,xcm]

    return CM
Example #31
0
    # Capture frames from the camera
    ret, frame = capture.read()

    # Get hand data from the rectangle sub window
    cv2.rectangle(frame, (100, 100), (300, 300), (0, 255, 0), 0)
    crop_image = frame[100:300, 100:300]

    # Apply Gaussian blur
    blur = cv2.GaussianBlur(crop_image, (3, 3), 0)

    # Change color-space from BGR -> HSV
    hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)

    # Create a binary image with where white will be skin colors and rest is black
    mask2 = cv2.inRange(hsv, np.array([2, 0, 0]), np.array([20, 255, 255]))

    # Kernel for morphological transformation
    kernel = np.ones((5, 5))

    # Apply morphological transformations to filter out the background noise
    dilation = cv2.dilate(mask2, kernel, iterations=1)
    erosion = cv2.erode(dilation, kernel, iterations=1)

    # Apply Gaussian Blur and Threshold
    filtered = cv2.GaussianBlur(erosion, (3, 3), 0)
    ret, thresh = cv2.threshold(filtered, 127, 255, 0)

    # Show threshold image
    cv2.imshow("Thresholded", thresh)
Example #32
0
def place_checker(img, checker, i, j, board_x, board_y, square_width, method=1):

    square = img[(board_x+(i-1)*square_width):(board_x+(i)*square_width),
                     (board_y+(j-1)*square_width):(board_y+(j)*square_width)]
                
    if method == 1:
        # simple image placement
        result=checker
        
    elif method == 2:
        # weighted image addition
        #cv.AddWeighted(src1, alpha, src2, beta, gamma, dst) → None
        # dst = src1 * alpha + src2 * beta + gamma
        result = cv2.addWeighted(square, 0.5, checker, 0.5, 0)
        
    elif method == 3:
        # thresholding

        # convert to gray scale
        #cv.cvtColor(src, dst, code) → None
        checkergray = cv2.cvtColor(checker,cv2.COLOR_BGR2GRAY)
        # find brown2 grey color conversion
        brown2pix = np.zeros((1,1,3), dtype=np.uint8)
        brown2pix[0,0] = brown2
        #cv.cvtColor(src, dst, code) → None
        brown2_gry = cv2.cvtColor(brown2pix,cv2.COLOR_BGR2GRAY)
        # threshold checker after graying it with brown2pix as the threshold
        #cv.threshold(src, dst, threshold, maxValue, thresholdType) → None
        r, mask = cv2.threshold(checkergray, brown2_gry[0,0]+5, 255, cv2.THRESH_BINARY_INV)
        # find the inverse of the threshold
        #cv2.bitwise_and(src1, src2[, dst[, mask]]) → dst
        mask_inv = cv2.bitwise_not(mask)
        
        # black out the area of checker in square
        #cv2.bitwise_and(src1, src2[, dst[, mask]]) → dst
        square_mskd = cv2.bitwise_and(square,square,mask = mask_inv)
        # black out the area of square in checker
        #cv2.bitwise_and(src1, src2[, dst[, mask]]) → dst
        checker_mskd = cv2.bitwise_and(checker,checker,mask = mask)
        #cv2.add(src1, src2[, dst[, mask[, dtype]]]) → dst
        result = cv2.add(square_mskd,checker_mskd)
    else:
        # color filtering
        
        #https://stackoverflow.com/questions/10948589/choosing-the-correct-upper-and-lower-hsv-boundaries-for-color-detection-withcv
        #cv.cvtColor(src, dst, code) → None
        checker_hsv = cv2.cvtColor(checker, cv2.COLOR_BGR2HSV)
        # find brown1 hsv color conversion
        brown1pix = np.zeros((1,1,3), dtype=np.uint8)
        brown1pix[0,0] = brown1
        #cv.cvtColor(src, dst, code) → None
        brown1_hsv = cv2.cvtColor(brown1pix,cv2.COLOR_BGR2HSV)
        # find brown2 hsv color conversion
        brown2pix = np.zeros((1,1,3), dtype=np.uint8)
        brown2pix[0,0] = brown2
        #cv.cvtColor(src, dst, code) → None
        brown2_hsv = cv2.cvtColor(brown2pix,cv2.COLOR_BGR2HSV)
        
        """         color1=np.array([min(brown1_hsv[0][0][0],brown2_hsv[0][0][0]),
                         min(brown1_hsv[0][0][1],brown2_hsv[0][0][1]),
                         min(brown1_hsv[0][0][2],brown2_hsv[0][0][2])]) #([4+4,255,89-5]) #([4,255,89])
        color2=np.array([max(brown1_hsv[0][0][0],brown2_hsv[0][0][0]),
                         max(brown1_hsv[0][0][1],brown2_hsv[0][0][1]),
                         max(brown1_hsv[0][0][2],brown2_hsv[0][0][2])]) #([4+4,255,89-5]) #([4,255,89])
        """        
        # cv2.inRange(src, lowerb, upperb[, dst]) → dst
        mask = cv2.inRange(checker_hsv, (0,0,0,), (180,255,253)) #brown1_hsv[0][0], brown2_hsv[0][0])
        mask_inv = cv2.bitwise_not(mask)
        # black out the area of checker in square
        #cv2.bitwise_and(src1, src2[, dst[, mask]]) → dst
        square_mskd = cv2.bitwise_and(square,square,mask = mask_inv)
        # black out the area of square in checker
        #cv2.bitwise_and(src1, src2[, dst[, mask]]) → dst
        checker_mskd = cv2.bitwise_and(checker,checker,mask = mask)
        #cv2.add(src1, src2[, dst[, mask[, dtype]]]) → dst
        result = cv2.add(square_mskd,checker_mskd)

    img[(board_y+(j-1)*square_width):(board_y+(j)*square_width),
        (board_x+(i-1)*square_width):(board_x+(i)*square_width)]=result
Example #33
0
    for i in range(0,8):
        for j in range(0,8):
            # calculate board square locations
            if ((checker_brn_match[(pred_board_y+(j)*square_width):(pred_board_y+(j+1)*square_width),
                (pred_board_x+(i)*square_width):(pred_board_x+(i+1)*square_width)]==0).sum()>0):
                state[i][j]=1
            elif ((checker_crm_match[(pred_board_y+(j)*square_width):(pred_board_y+(j+1)*square_width),
                (pred_board_x+(i)*square_width):(pred_board_x+(i+1)*square_width)]==0).sum()>0):
                state[i][j]=2
            else:
                state[i][j]=0
    return state

# show mask for checkers
checker_hsv = cv2.cvtColor(checker_brn, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(checker_hsv, (0,0,0), (180,255,253))
mask = cv2.normalize(mask,None, alpha=0,beta=1,norm_type=cv2.NORM_MINMAX)
plot_img(mask,False)

# draw read state with a move
state = read_state(img_game)
state[2][2]=0
state[3][3]=1
# draw game
img_game2 = draw_game_state(state, img_board_title)
plot_img(img_game2)


# what's changed in the board?
state = read_state(img_game2)
Example #34
0
 
# load the image
image = cv2.imread(args["image"])
# define the list of boundaries
boundaries = [
	([17, 15, 90], [50, 56, 255]),
	([80, 25, 4], [255, 100, 50]),
	([25, 146, 190], [62, 174, 250]),
	([103, 86, 65], [145, 133, 128])
]



# loop over the boundaries
for (lower, upper) in boundaries:
	# create NumPy arrays from the boundaries
	lower = np.array(lower, dtype = "uint8")
	upper = np.array(upper, dtype = "uint8")
 
	# find the colors within the specified boundaries and apply
	# the mask
	mask = cv2.inRange(image, lower, upper)
	print(lower)
	print(upper)
	output = cv2.bitwise_and(image, image, mask = mask)
 
	# show the images
	cv2.namedWindow("Display Frame",cv2.WINDOW_NORMAL)
	cv2.imshow("Display Frame", np.hstack([image, output]))
	cv2.waitKey(0)
    try:
        ret, frame = cap.read()
        frame = cv2.flip(frame, 1)
        kernel = np.ones((3, 3), np.uint8)

        #define region of interest
        roi = frame[50:350, 50:350]

        cv2.rectangle(frame, (50, 50), (350, 350), (0, 255, 0), 0)
        hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

        # define range of skin color in HSV
        lower_skin = np.array([0, 30, 70], dtype=np.uint8)
        upper_skin = np.array([20, 255, 255], dtype=np.uint8)

        mask = cv2.inRange(hsv, lower_skin, upper_skin)
        mask = cv2.erode(mask, kernel, iterations=2)
        mask = cv2.dilate(mask, kernel, iterations=4)

        mask = cv2.GaussianBlur(mask, (5, 5), 100)

        #find contours
        _, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE,
                                                  cv2.CHAIN_APPROX_SIMPLE)

        #find contour of max area(hand)
        cnt = max(contours, key=lambda x: cv2.contourArea(x))

        #approx the contour a little
        epsilon = 0.0005 * cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, epsilon, True)
Example #36
0
import argparse
import imutils
import cv2

# load the argument parser
ap = argparse.ArgumentParser()
ap.add_argument("-i", ("--image"), help="path to image")
args = vars(ap.parse_args())

# load image
img = cv2.imread(args["image"])

# finding black shapes
lower = np.array([0, 0, 0])
upper = np.array([30, 30, 30])
shape_mask = cv2.inRange(img, lower, upper)

# finding countours
cnts = cv2.findContours(shape_mask.copy(), cv2.RETR_EXTERNAL,
                        cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
print("found {} contours".format(len(cnts)))

# drawing contours
for c in cnts:
    cv2.drawContours(img, [c], -1, (0, 255, 0), 2)

# cv2.imshow('mask', shape_mask)
cv2.imshow("image", img)
cv2.waitKey(0)
Example #37
0
ret, old_frame = cap.read()
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)

# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)
blur = cv2.GaussianBlur(old_gray, (5, 5), 0)
ret3, old_th3 = cv2.threshold(blur, 0, 255,
                              cv2.THRESH_BINARY + cv2.THRESH_OTSU)

while (1):
    # read new frame
    ret, frame = cap.read()
    # convert image to HSV
    imgHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    # threshold the black out
    imgThres = cv2.inRange(imgHSV, (0, 0, 0), (180, 255, 100))

    # convert image to gray
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Otsu's thresholding after Gaussian filtering
    blur = cv2.GaussianBlur(frame_gray, (5, 5), 0)
    ret3, th3 = cv2.threshold(blur, 0, 255,
                              cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    # by subtraction, get the difference between two consequent frames
    img = cv2.add(frame, mask)
    img = frame_gray - old_gray
    img1 = th3 - old_th3
    img2 = old_th3 - th3
    img = cv2.add(img1, img2)
Example #38
0
#lower = np.array([0, 48, 80], dtype="uint8")
#upper = np.array([20, 255, 255], dtype="uint8")
lower = np.array([0, 48, 80])
upper = np.array([20, 255, 255])

img = cv2.imread('img/0.JPG')
height,width = img.shape[:2]

if height>width:
    img = cv2.resize(img, (1280, 1920))
else:
    img = cv2.resize(img, (1920, 1280))

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
skinMask = cv2.inRange(hsv, lower, upper)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))
skinMask = cv2.erode(skinMask, kernel, iterations = 5)
skinMask = cv2.dilate(skinMask, kernel, iterations = 5)
#cv2.imwrite('result/skinmask.jpg',skinMask)
image, contours, h = cv2.findContours(skinMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
maximumArea = 0
bestContour = None
for contour in contours:
    if cv2.contourArea(contour) > 2000:
        approx = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True)
        #cv2.drawContours(img, [approx], -1, (0, 0, 255), 2)

cv2.imwrite('result/skinmask1.jpg',skinMask)# alpha
Example #39
0
	# if we are viewing a video and we did not grab a frame,
	# then we have reached the end of the video
	if args.get("video") and not grabbed:
		break

	# resize the frame, blur it, and convert it to the HSV
	# color space
	frame = imutils.resize(frame, width=600)
	# blurred = cv2.GaussianBlur(frame, (11, 11), 0)
	hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

	# construct a mask for the color "green", then perform
	# a series of dilations and erosions to remove any small
	# blobs left in the mask
	mask = cv2.inRange(hsv, greenLower, greenUpper)
	mask = cv2.erode(mask, None, iterations=2)
	mask = cv2.dilate(mask, None, iterations=2)

	# find contours in the mask and initialize the current
	# (x, y) center of the ball
	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
		cv2.CHAIN_APPROX_SIMPLE)[-2]
	center = None

	# only proceed if at least one contour was found
	if len(cnts) > 0:
		# find the largest contour in the mask, then use
		# it to compute the minimum enclosing circle and
		# centroid
		c = max(cnts, key=cv2.contourArea)
Example #40
0
    def maskLane(self, image):
        # convert image to hsv
        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        # param of irange hsv(white & yellow)
        Hue_white_h = self.hue_white_l
        Hue_white_l = self.hue_white_h
        Saturation_white_h = self.saturation_white_l
        Saturation_white_l = self.saturation_white_h
        Lightness_white_h = self.lightness_white_h
        Lightness_white_l = self.lightness_white_l

        Hue_yellow_h = self.hue_yellow_l
        Hue_yellow_l = self.hue_yellow_h
        Saturation_yellow_h = self.saturation_yellow_l
        Saturation_yellow_l = self.saturation_yellow_h
        Lightness_yellow_h = self.lightness_yellow_h
        Lightness_yellow_l = self.lightness_yellow_l
        # define range of white color in HSV
        lower_white = np.array(
            [Hue_white_h, Saturation_white_h, Lightness_white_l])
        upper_white = np.array(
            [Hue_white_l, Saturation_white_l, Lightness_white_h])

        lower_yellow = np.array(
            [Hue_yellow_h, Saturation_yellow_h, Lightness_yellow_l])
        upper_yellow = np.array(
            [Hue_yellow_l, Saturation_yellow_l, Lightness_yellow_h])
        # Threshold the HSV image to get only white colors
        mask_white = cv2.inRange(hsv, lower_white, upper_white)
        mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow)
        kernel = np.ones((5, 5))
        erosion_white = cv2.erode(mask_white, kernel)
        erosion_yellow = cv2.erode(mask_yellow, kernel)
        Gaussian_white = cv2.GaussianBlur(erosion_white, (5, 5), 0)
        Gaussian_yellow = cv2.GaussianBlur(erosion_yellow, (5, 5), 0)
        # findContours of image
        contours_white, hierarchy_white = cv2.findContours(
            Gaussian_white, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
        contours_yellow, hierarchy_yellow = cv2.findContours(
            Gaussian_yellow, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
        # print contours
        # print(contours_white)
        # print(contours_yellow)
        # draw the contours in origin image
        cv2.drawContours(image, contours_white, -1, (139, 104, 0), 3)
        cv2.drawContours(image, contours_yellow, -1, (139, 104, 0), 3)

        # center of  white  and yellow lane
        white_center = Center()
        yellow_center = Center()
        # try:
        white_x, white_y = self.calculate_average(contours_white[0])
        white_center[0] = white_x
        white_center[1] = white_y
        print("white: ", white_x, white_y)
        is_detect_white = 1
        # except:
        #     is_detect_white = 0
        #     print(contours_white[0])
        #     print("The Camera Can`t Catch The White Lane.")
        # try:
        yellow_x, yellow_y = self.calculate_average(contours_yellow[0])
        yellow_center[0] = yellow_x
        yellow_center[1] = yellow_y
        print("yellow: ", yellow_x, yellow_y)
        is_detect_yellow = 1
        # except:
        #     is_detect_yellow = 0
        #     print(contours_yellow[0])
        #     print("The Camera Can`t Catch The Yellow Lane.")
        # print(contours_white[0])
        # yellow_x, yellow_y = self.calculate_average(contours_yellow[0])
        # Publish Image
        self.pub_image_detect.publish(
            self.cvBridge.cv2_to_imgmsg(image, 'bgr8'))

        # Publish Center
        self.pub_center_white_lane.publish(white_center)
        self.pub_center_yellow_lane.publish(yellow_center)
Example #41
0
    def detect(self, sender: Device, img: np.ndarray,
               plate_angles: Vector2) -> CircleFeature:
        if img is not None:
            # covert to HSV space
            color = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

            # 0 == red
            if self.config.hue is None:
                self.config.hue = 0

            # run through each triplet and perform our masking filter on it.
            # hue_mask coverts the hsv image into a grayscale image with a
            # bandpass applied centered around hue, with width sigma
            hue_mask(
                color,
                self.config.hue,
                self.config.sigma,
                self.config.bandpassGain,
                self.config.maskGain,
            )

            # convert to b&w mask from grayscale image
            mask = cv2.inRange(color, np.array((200, 200, 200)),
                               np.array((255, 255, 255)))

            # expand b&w image with a dialation filter
            mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, self.kernel)

            contours = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                        cv2.CHAIN_APPROX_SIMPLE)[-2]

            if len(contours) > 0:
                contour_peak = max(contours, key=cv2.contourArea)
                ((self.x_obs, self.y_obs),
                 radius) = cv2.minEnclosingCircle(contour_peak)

                # Determine if ball size is the appropriate size
                frameSize = sender.sensor.config.height
                norm_radius = radius / frameSize
                if self.config.ballMin < norm_radius < self.config.ballMax:
                    counter.update("hit", 1, FrequencyCounter)

                    # rotate the center coords into sensor coords
                    # the ball detector uses rotate coordinates, so we must as well
                    rot_center = Vector2(
                        self.calibration.plateXOffset,
                        self.calibration.plateYOffset).rotate(
                            math.radians(-self.calibration.rotation))

                    x_center = (rot_center.x + 0.5) * frameSize
                    y_center = (rot_center.y + 0.5) * frameSize

                    # Convert from pixels to absolute with 0,0 as center of detected plate
                    x = self.x_obs - x_center
                    y = self.y_obs - y_center
                    self.lastDetected = CircleFeature(Vector2(x, y), radius)
                    return self.lastDetected
                else:
                    counter.update("miss", 1, FrequencyCounter)

            counter.update("hit", 0, FrequencyCounter)
            counter.update("miss", 0, FrequencyCounter)

        return CircleFeature()
class image_converter:


    def __init__(self):
        self.image_pub = rospy.Publisher('image_topic_2', Image, queue_size=1)

        self.bridge = CvBridge()
        self.image_sub = rospy.Subscriber('vrep/image', Image, self.callback)

    def callback(self, data):
        global room
        try:
            raw_image = self.bridge.imgmsg_to_cv2(data, 'bgr8')
        except CvBridgeError, e:
            pass #print e
    # Flip image to correct orientation 
        corrected_image = raw_image.copy()
        corrected_image = cv2.flip(corrected_image, 1)
        #cv2.imshow('Corrected', corrected_image)

    #set boundaries for color detection    
        boundaries = [([0, 150, 150], [70, 255, 255])]
        for (lower, upper) in boundaries:  # Loop over the boundaries

    # create NumPy arrays from the boundaries

            lower = np.array(lower, dtype='uint8')
            upper = np.array(upper, dtype='uint8')

    # find the colors within the specified boundaries and apply the mask

            mask = cv2.inRange(corrected_image, lower, upper)
            output = cv2.bitwise_and(corrected_image, corrected_image, mask=mask)
            
            #find countours in the mask and initialize the current (x,y) center of the ball
            cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
            center = None
            #global room
            rospy.Subscriber('/location_monitor', String, callback1)
        
            #print room
            #only proceed if at least one contour was found
            if  room == "Robot is in Room D!" and len(cnts) > 0: #
                
                pub = rospy.Publisher('/vrep/cmd_vel', Twist, queue_size = 10) 
                bool_msg = 0
                pub1.publish(bool_msg)
                #find the largest contour in the mask, then use it to compute the minimum enclosing circule and centroid
                c = max(cnts, key=cv2.contourArea)
                ((x,y,), radius) = cv2.minEnclosingCircle(c)
                M = cv2.moments(c)
                if M["m00"] == 0:
                    M["m00"] = 0.0001
                center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
                #print M #debug
                #only proceed if the radius meets a minimum size
                if radius > 10:
                    #draw circle and centroid on the frame, then update the list of tracked points 
                    cv2.circle(corrected_image, (int(x), int(y)), int(radius),
                        (0, 255, 255), 2)
                    cv2.circle(corrected_image, center, 5, (0, 0, 255), -1)
                    
                    #Implement Control to follow yellow ball
                    ang_err = x_d - x
                    cmd_vel_ang = K_p_x * ang_err
                    distance_err = radius_d - radius
                    cmd_vel_lin = K_p_radius * distance_err
                    if cmd_vel_ang > 1.5:
                        cmd_vel_ang = 1.5
                    if cmd_vel_lin > 2:
                        cmd_vel_lin = 2
                    #rospy.loginfo('x: {}, y: {}, radius: {}, cmd_vel_ang: {}, cmd_vel_lin: {}'.format(x, y, radius, cmd_vel_ang, cmd_vel_lin))

                    vel_msg = Twist()
                    vel_msg.linear.x = cmd_vel_lin
                    vel_msg.angular.z = cmd_vel_ang 
                    pub.publish(vel_msg)                   
            else: 
                bool_msg = 1 
                pub1.publish(bool_msg)


            cv2.imshow('Image', np.hstack([corrected_image, output]))
             



        cv2.waitKey(3)

        try:
            self.image_pub.publish(self.bridge.cv2_to_imgmsg(raw_image,
                                   'bgr8'))
        except CvBridgeError, e:
            pass #print e
cv2.waitKey()

"""
cap = cv2.VideoCapture(0)
startMeasure = time.time()
while (1):
    startTime = time.time()
    _, original = cap.read()

    frame = cv2.resize(original, (400, 400))
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lower_red = np.array([30, 150, 50])
    upper_red = np.array([255, 255, 180])

    mask = cv2.inRange(hsv, lower_red, upper_red)
    res = cv2.bitwise_and(frame, frame, mask=mask)

    kernel = np.ones((5, 5), np.uint8)

    opening = cv2.morphologyEx(frame, cv2.MORPH_OPEN, kernel)
    closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)

    cv2.imshow('Original', frame)
    cv2.imshow('Mask', mask)
    cv2.imshow('Opening', opening)
    cv2.imshow('Closing', closing)

    elapsedTime = time.time() - startTime
    elapsedTotTime = (time.time() - startMeasure)
    #print('function finished in {} ms'.format(float(elapsedTime * 1000)))
Example #44
0
cv2.rectangle(small, ((x1), (y1)), ((x2), (y2)), (0, 0, 255), 2)
human = small[y1:y2, x1:x2]
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

grayimg = cv2.cvtColor(human, cv2.COLOR_BGR2GRAY)
cv2.imshow('human', human)
blurred = cv2.blur(grayimg, (3, 3))
canny = cv2.Canny(blurred, 50, 255)

thresh = 170
im_bw = cv2.threshold(grayimg, thresh, 255, cv2.THRESH_BINARY)[1]

low = np.array([15, 200, 150], dtype="uint8")
up = np.array([32, 229, 183], dtype="uint8")
mask = cv2.inRange(small, low, up)
cv2.imshow('mask', mask)

cv2.imshow("canny:", canny)
cv2.imwrite('canny.jpg', canny)

plt.subplot(121), plt.imshow(human, cmap='gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(canny, cmap='gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()

cv2.imshow("small:", small)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example #45
0
ret, frame = cam.read()

# frame = cv2.resize(frame, dsize = (0, 0), fx = 0.5, fy = 0.5)
#frame = getNextFrame(cam)
cv2.namedWindow('camshift')
cv2.setMouseCallback('camshift', onmouse)
cv2.namedWindow('hist')
cv2.moveWindow('hist', 700, 100)  # Move to reduce overlap

# start processing frames
while True:
    frame = getNextFrame(cam)
    vis = frame.copy()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)  # convert to HSV
    mask = cv2.inRange(hsv, np.array(
        (0., 60., 32.)), np.array(
            (180., 255.,
             255.)))  # eliminate low and high saturation and value values

    if isDragging and selection != None:  # if currently dragging and a good region has been selected
        x0, y0, x1, y1 = selection
        track_window = (x0, y0, x1 - x0, y1 - y0)
        hsv_roi = hsv[
            y0:y1, x0:
            x1]  # access the currently selected region and make a histogram of its hue
        mask_roi = mask[y0:y1, x0:x1]
        hist = cv2.calcHist([hsv_roi], [0], mask_roi, [16], [0, 180])
        cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
        hist = hist.reshape(-1)
        show_hist(hist)

        vis_roi = vis[y0:y1, x0:x1]  # make the selected region visible
Example #46
0
    sharp = cv2.GaussianBlur(dst, (5, 5), sigma)
    sharp = cv2.addWeighted(dst, 1.5, sharp, -0.5, 0)

    # cv2.imshow("orig",orig)
    # cv2.imshow("input",img)
    # cv2.imshow("filter",dst)
    # cv2.imshow("sharp",sharp)

    black_thresh = readParam("black_threshold","IMAGEPROC")
    
    lower = np.array([0,0,0], dtype = "uint8")
    upper = np.array([black_thresh, black_thresh, black_thresh], dtype = "uint8")
 
    # find the colors within the specified boundaries and apply
    # the mask
    mask = cv2.inRange(sharp, lower, upper)
    blackhat = cv2.morphologyEx(mask,cv2.MORPH_BLACKHAT,cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5)))
    # blackhat = cv2.bitwise_not(blackhat)
    # cv2.imshow("blackhat",blackhat)
    mask = cv2.bitwise_xor(mask,blackhat)
    kernel = np.ones((3,3),np.uint8)
    mask = cv2.dilate(mask,kernel,iterations = 1)
    # cv2.imshow("mask",mask)
    # Create a blank black image
    white = np.zeros((mask.shape[0], mask.shape[1], 3), np.uint8)
    # Fill image with red color(set each pixel to red)
    white[:] = (255,255,255)
    mask_bgr = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
    inverted = cv2.bitwise_not(sharp)
    # output = cv2.bitwise_and(white, mask_bgr)
    # output = sharp[~np.array(mask)]   
    hue2Low = cv2.getTrackbarPos('hue2Lower', 'Trackbars')
    hue2Up = cv2.getTrackbarPos('hue2Upper', 'Trackbars')

    Ls = cv2.getTrackbarPos('satLow', 'Trackbars')
    Us = cv2.getTrackbarPos('satHigh', 'Trackbars')

    Lv = cv2.getTrackbarPos('valLow', 'Trackbars')
    Uv = cv2.getTrackbarPos('valHigh', 'Trackbars')

    l_b = np.array([hueLow, Ls, Lv])
    u_b = np.array([hueUp, Us, Uv])

    l_b2 = np.array([hue2Low, Ls, Lv])
    u_b2 = np.array([hue2Up, Us, Uv])

    FGmask = cv2.inRange(hsv, l_b, u_b)
    FGmask2 = cv2.inRange(hsv, l_b2, u_b2)
    FGmaskComp = cv2.add(FGmask, FGmask2)
    cv2.imshow('FGmaskComp', FGmaskComp)
    cv2.moveWindow('FGmaskComp', 0, 300)

    _, contours, _ = cv2.findContours(FGmaskComp, cv2.RETR_EXTERNAL,
                                      cv2.CHAIN_APPROX_SIMPLE)
    # Sortiert die groesste Kontour nach oben
    contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
    # nur die groesseren Konturen anzeigen
    for cnt in contours:
        area = cv2.contourArea(cnt)
        (x, y, w, h) = cv2.boundingRect(cnt)
        if area >= 500:
            # cv2.drawContours(frame, [cnt], 0, (255,0,0),2)
        cv2.rectangle(imagen, (x, y), (x+2, y+2),(255,255,255), 2)

        return (x, y)

#Iniciamos la camara
captura = cv2.VideoCapture(0)

while(1):

    #Capturamos una imagen y la convertimos de RGB -> HSV
    _,imagen = captura.read()
    imagen = cv2.blur(imagen,(3,3))
    hsv = cv2.cvtColor(imagen, cv2.COLOR_BGR2HSV)

    #Crear una mascara con solo los pixeles dentro del rango de los colores
    mask_azul = cv2.inRange(hsv, colores['azul'][0], colores['azul'][1])
    mask_rojo = cv2.inRange(hsv, colores['rojo'][0], colores['rojo'][1])
    mask_verde = cv2.inRange(hsv, colores['verde'][0], colores['verde'][1])
    mask_amarillo = cv2.inRange(hsv, colores['amarillo'][0], colores['amarillo'][1])

    #Eliminamos el ruido
    noise_azul = noise(mask_azul)
    noise_rojo = noise(mask_rojo)
    noise_verde = noise(mask_verde)
    noise_amarillo = noise(mask_amarillo)

    #Mascara con todos los colores
    mask = noise_azul + noise_rojo + noise_verde + noise_amarillo

    #Imagen con solo los colores
    color = cv2.bitwise_and(imagen, imagen, mask= mask)
img_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
'''cap = cv.VideoCapture('5feet.mp4')
while cap.isOpened():
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    if cv.waitKey(0) == ord('q'):
        break'''

# define the list of boundaries
# lower mask (0-10)
lower_red = np.array([0, 10, 10])
upper_red = np.array([55, 255, 255])
mask0 = cv2.inRange(img_hsv, lower_red, upper_red)
#for red color
# upper mask (170-180)
lower_red = np.array([140, 0, 0])
upper_red = np.array([185, 255, 255])
mask1 = cv2.inRange(img_hsv, lower_red, upper_red)

# join my masks
mask = mask0 + mask1

#cv2.imshow('frame',frame)
#cv2.imshow('mask',mask)
#cv2.imshow('res',res)

ret, thresh = cv2.threshold(mask, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
Example #50
0
def main():
    # Telloクラスを使って,droneというインスタンス(実体)を作る
    drone = tello.Tello('', 8889, command_timeout=.01)

    current_time = time.time()  # 現在時刻の保存変数
    pre_time = current_time  # 5秒ごとの'command'送信のための時刻変数

    time.sleep(0.5)  # 通信が安定するまでちょっと待つ

    # トラックバーを作るため,まず最初にウィンドウを生成
    cv2.namedWindow("OpenCV Window")

    # トラックバーのコールバック関数は何もしない空の関数
    def nothing(x):
        pass

    # トラックバーの生成
    cv2.createTrackbar("H_min", "OpenCV Window", 0, 179, nothing)
    cv2.createTrackbar("H_max", "OpenCV Window", 9, 179,
                       nothing)  # Hueの最大値は179
    cv2.createTrackbar("S_min", "OpenCV Window", 128, 255, nothing)
    cv2.createTrackbar("S_max", "OpenCV Window", 255, 255, nothing)
    cv2.createTrackbar("V_min", "OpenCV Window", 128, 255, nothing)
    cv2.createTrackbar("V_max", "OpenCV Window", 255, 255, nothing)

    flag = 0
    #Ctrl+cが押されるまでループ
    try:
        while True:

            # (A)画像取得
            frame = drone.read()  # 映像を1フレーム取得
            if frame is None or frame.size == 0:  # 中身がおかしかったら無視
                continue

            # (B)ここから画像処理
            image = cv2.cvtColor(frame,
                                 cv2.COLOR_RGB2BGR)  # OpenCV用のカラー並びに変換する
            bgr_image = cv2.resize(image, dsize=(480, 360))  # 画像サイズを半分に変更

            hsv_image = cv2.cvtColor(bgr_image,
                                     cv2.COLOR_BGR2HSV)  # BGR画像 -> HSV画像

            # トラックバーの値を取る
            h_min = cv2.getTrackbarPos("H_min", "OpenCV Window")
            h_max = cv2.getTrackbarPos("H_max", "OpenCV Window")
            s_min = cv2.getTrackbarPos("S_min", "OpenCV Window")
            s_max = cv2.getTrackbarPos("S_max", "OpenCV Window")
            v_min = cv2.getTrackbarPos("V_min", "OpenCV Window")
            v_max = cv2.getTrackbarPos("V_max", "OpenCV Window")

            # inRange関数で範囲指定2値化
            bin_image = cv2.inRange(hsv_image, (h_min, s_min, v_min),
                                    (h_max, s_max, v_max))  # HSV画像なのでタプルもHSV並び

            # bitwise_andで元画像にマスクをかける -> マスクされた部分の色だけ残る
            masked_image = cv2.bitwise_and(hsv_image,
                                           hsv_image,
                                           mask=bin_image)

            # ラベリング結果書き出し用に画像を準備
            out_image = masked_image

            # 面積・重心計算付きのラベリング処理を行う
            num_labels, label_image, stats, center = cv2.connectedComponentsWithStats(
                bin_image)

            # 最大のラベルは画面全体を覆う黒なので不要.データを削除
            num_labels = num_labels - 1
            stats = np.delete(stats, 0, 0)
            center = np.delete(center, 0, 0)

            if num_labels >= 1:
                # 面積最大のインデックスを取得
                max_index = np.argmax(stats[:, 4])
                #print max_index

                # 面積最大のラベルのx,y,w,h,面積s,重心位置mx,myを得る
                x = stats[max_index][0]
                y = stats[max_index][1]
                w = stats[max_index][2]
                h = stats[max_index][3]
                s = stats[max_index][4]
                mx = int(center[max_index][0])
                my = int(center[max_index][1])
                #print("(x,y)=%d,%d (w,h)=%d,%d s=%d (mx,my)=%d,%d"%(x, y, w, h, s, mx, my) )

                # ラベルを囲うバウンディングボックスを描画
                cv2.rectangle(out_image, (x, y), (x + w, y + h), (255, 0, 255))

                # 重心位置の座標を表示
                #cv2.putText(out_image, "%d,%d"%(mx,my), (x-15, y+h+15), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 0))
                cv2.putText(out_image, "%d" % (s), (x, y + h + 15),
                            cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 0))

                if flag == 1:
                    a = b = c = d = 0

                    # P制御の式(Kpゲインはとりあえず1.0)
                    dx = 1.0 * (240 - mx)  # 画面中心との差分

                    # 旋回方向の不感帯を設定
                    d = 0.0 if abs(dx) < 50.0 else dx  # ±50未満ならゼロにする

                    d = -d
                    # 旋回方向のソフトウェアリミッタ(±100を超えないように)
                    d = 100 if d > 100.0 else d
                    d = -100 if d < -100.0 else d

                    print('dx=%f' % (dx))
                    drone.send_command('rc %s %s %s %s' %
                                       (int(a), int(b), int(c), int(d)))

            # (X)ウィンドウに表示
            cv2.imshow('OpenCV Window',
                       out_image)  # ウィンドウに表示するイメージを変えれば色々表示できる

            # (Y)OpenCVウィンドウでキー入力を1ms待つ
            key = cv2.waitKey(1)
            if key == 27:  # k が27(ESC)だったらwhileループを脱出,プログラム終了
                break
            elif key == ord('t'):
                drone.takeoff()  # 離陸
            elif key == ord('l'):
                drone.land()  # 着陸
            elif key == ord('w'):
                drone.move_forward(0.3)  # 前進
            elif key == ord('s'):
                drone.move_backward(0.3)  # 後進
            elif key == ord('a'):
                drone.move_left(0.3)  # 左移動
            elif key == ord('d'):
                drone.move_right(0.3)  # 右移動
            elif key == ord('q'):
                drone.rotate_ccw(20)  # 左旋回
            elif key == ord('e'):
                drone.rotate_cw(20)  # 右旋回
            elif key == ord('r'):
                drone.move_up(0.3)  # 上昇
            elif key == ord('f'):
                drone.move_down(0.3)  # 下降
            elif key == ord('1'):
                flag = 1  # 追跡モードON
            elif key == ord('2'):
                flag = 0  # 追跡モードOFF

            # (Z)5秒おきに'command'を送って、死活チェックを通す
            current_time = time.time()  # 現在時刻を取得
            if current_time - pre_time > 5.0:  # 前回時刻から5秒以上経過しているか?
                drone.send_command('command')  # 'command'送信
                pre_time = current_time  # 前回時刻を更新

    except (KeyboardInterrupt, SystemExit):  # Ctrl+cが押されたら離脱
        print("SIGINTを検知")

    drone.send_command('streamoff')
    # telloクラスを削除
    del drone
colors = {'red':(0,0,255), 'green':(0,255,0), 'blue':(255,0,0)}

#capturing frames
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    # grab the current frame
    frame = frame.array
 
    blurred = cv2.GaussianBlur(frame, (11, 11), 0)
    hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
    #for each color in dictionary check object in frame
    for key, value in upper.items():
        # construct a mask for the color from dictionary`1, then perform
        # a series of dilations and erosions to remove any small
        # blobs left in the mask
        kernel = np.ones((9,9),np.uint8)
        mask = cv2.inRange(hsv, lower[key], upper[key])
        mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
        mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
               
        # find contours in the mask and initialize the current
        # (x, y) center of the object
        cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
            cv2.CHAIN_APPROX_SIMPLE)[-2]
        center = None
       
        # only proceed if at least one contour was found
        if len(cnts) > 0:
            # find the largest contour in the mask, then use
            # it to compute the minimum enclosing circle and
            # centroid
            c = max(cnts, key=cv2.contourArea)
    def lane_detection(self, image_bgr):
        # Convert to HSV
        image_hsv = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2HSV)

        # Mask
        global mask_lower_threshold, mask_upper_threshold
        mask1 = cv2.inRange(image_hsv, mask_lower_threshold,
                            mask_upper_threshold)
        # add another mask
        mask2 = cv2.inRange(image_hsv, mask2_lower_threshold,
                            mask2_upper_threshold)
        #final mask
        mask = cv2.bitwise_or(mask1, mask2)
        # Publish mask image
        if self.mask_pub.get_num_connections() > 0:
            self.mask_pub.publish(
                self.bridge.cv2_to_imgmsg(mask, encoding='passthrough'))

        # Canny edge detection
        edges = cv2.Canny(mask, 200, 400)

        # Clear the top half of the edges image
        edges[0:int(float(edges.shape[0]) / 1.5), :] = 0.

        # Publish edges image
        if self.edges_image_pub.get_num_connections() > 0:
            self.edges_image_pub.publish(
                self.bridge.cv2_to_imgmsg(edges, encoding='passthrough'))

        # Detect line segments
        line_segments_tmp = cv2.HoughLinesP(edges, 1, np.pi / 180., 10, None,
                                            8, 4)

        print("line segments tmp")
        print(line_segments_tmp)
        if line_segments_tmp is None:
            print('No line segments detected')
            return [None, None]

        # Remove extra array layer from line_segments_tmp
        line_segments = []

        for line_segment in line_segments_tmp:
            line_segments.append(line_segment[0])

        # Publish line segments image
        if self.line_segments_image_pub.get_num_connections() > 0:
            line_segments_image = plot_line_segments(image_bgr, line_segments)
            self.line_segments_image_pub.publish(
                self.bridge.cv2_to_imgmsg(line_segments_image,
                                          encoding='bgr8'))

        # Combine line segments
        [left_lane,
         right_lane] = functions.average_slope_intercept(line_segments)

        if self.lanes_image_pub.get_num_connections() > 0:
            lanes_image = plot_lanes(image_bgr, left_lane, right_lane)
            self.lanes_image_pub.publish(
                self.bridge.cv2_to_imgmsg(lanes_image, encoding='bgr8'))

        return [left_lane, right_lane]
Example #53
0
likelyGate = []
points = []
while True:
    if not paused:
        ret, frame = cap.read()
    else:
        frame = untampered
    if ret:
        if not paused:
            frame = cv.resize(frame, (0, 0), fx=0.5, fy=0.5)
            blur = cv.GaussianBlur(frame, (5, 5), 0)
            frame_HSV = cv.cvtColor(blur, cv.COLOR_BGR2HSV)
            # frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
            # canny = cv.Canny(frame_gray, 0)
        frame_threshold = cv.inRange(frame_HSV, (low_H, low_S, low_V), (high_H, high_S, high_V))  # low_S ideal = 98

        # frame_threshold = cv.bitwise_not(frame_threshold)
        res = cv.bitwise_and(frame, frame, mask=frame_threshold)
        res2, contours, hierarchy = cv.findContours(frame_threshold, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

        contours.sort(key=heuristic, reverse=True)
        if len(contours) > 1:
            heur0 = heuristic(contours[0])
            heur1 = heuristic(contours[1])

            likelyGate = [{'cont': contours[0], 'heur': heur0}, {'cont': contours[1], 'heur': heur1}]
        untampered = np.copy(frame)
        if contours:
            # likelyGate.append(contours[0])
            # findLikelyGate(likelyGate, contours)
Example #54
0
def gen_frames():
    while True:
        success, ori_img = camera.read()
        if not success:
            break
        else:
            img = cv2.resize(ori_img[:, 160:1120].copy(), (640, 480))
            ret, buffer = cv2.imencode('.jpg', img)
            frame = buffer.tobytes()

            img_roi = img[240:480, :]
            ### dst
            hsv = cv2.cvtColor(img_roi, cv2.COLOR_BGR2HSV)
            lowerb = (60, 0, 0)
            upperb = (179, 100, 120)
            # lowerb, upperb = getTrackbar()
            dst = cv2.inRange(hsv, lowerb, upperb)

            ### Hough
            # gray scaling
            gray = cv2.cvtColor(img_roi, cv2.COLOR_BGR2GRAY)

            # Canny
            blur = cv2.GaussianBlur(gray, ksize=(5, 5), sigmaX=0.0)
            edges = cv2.Canny(blur, 50, 100)

            # LoG filtering, zero crossing
            # LoG = cv2.filter2D(gray, cv2.CV_32F, kernel=logFilter(15))
            # edges = zeroCrossing2(LoG)

            # Hough
            # lines = cv2.HoughLines(edges, rho=1, theta=np.pi / 180.0, threshold=100)
            lines = cv2.HoughLinesP(edges,
                                    rho=1,
                                    theta=np.pi / 180.0,
                                    threshold=90,
                                    minLineLength=100,
                                    maxLineGap=10)

            if lines is None:
                # print(' no lines')
                pass
            else:
                # print(len(lines))
                for line in lines:
                    # rho, theta = line[0]
                    # c = np.cos(theta)
                    # s = np.sin(theta)
                    # x0 = c * rho
                    # y0 = s * rho
                    # x1 = int(x0 + 1000 * (-s))
                    # y1 = int(y0 + 1000 * c)
                    # x2 = int(x0 - 1000 * (-s))
                    # y2 = int(y0 - 1000 * c)
                    x1, y1, x2, y2 = line[0]
                    cv2.line(img, (x1, y1 + 240), (x2, y2 + 240), (0, 0, 255),
                             2)

            # cv2.imshow('origin', ori_img)
            cv2.imshow('frame', img)
            cv2.imshow('Canny', edges)
            cv2.imshow('dst', dst)
            cv2.waitKey(33)

            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
Example #55
0
            option = 999

    # Create Map
    while option == 5:
        # ret, img = cap.read()
        # # img = cv2.flip(img,1)
        # img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        # img = cv2.inRange(img,0,125)
        # img = 255-img
        # img = cv2.medianBlur(img,5)
        # img = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
        # cv2.imshow("image",img)
        ret, img = cap.read()
        # img = cv2.flip(img,1)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        img = cv2.inRange(img, 0, 75)
        # img = 255-img
        img = cv2.medianBlur(img, 5)
        TampmapGame = img.copy()
        # TampmapGame=255-TampmapGame
        # img = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
        newtest = np.zeros(TampmapGame.shape)
        contourmask = temp, contours, hierarchy = cv2.findContours(
            TampmapGame, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        for cnt in contours:
            x, y, w, h = cv2.boundingRect(cnt)
            if (x == 0 or y == 0 or x + w == TampmapGame.shape[0]
                    or y + h == TampmapGame.shape[1]
                    or x == TampmapGame.shape[0] or y == TampmapGame.shape[1]):
                print("1")  # cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
            else:
Example #56
0
def find_circles(camera_handle):
    trig_A, trig_B = False, False
    buzz = 0
    _, frame = camera_handle.read()

    #converting to HSV
    hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
    hue,sat,val = cv2.split(hsv)
   
    # Apply thresholding
    hthreshA = cv2.inRange(np.array(hue),np.array(hminA),np.array(hmaxA))
    sthreshA = cv2.inRange(np.array(sat),np.array(sminA),np.array(smaxA))
    vthreshA = cv2.inRange(np.array(val),np.array(vminA),np.array(vmaxA))

    hthreshB = cv2.inRange(np.array(hue),np.array(hminB),np.array(hmaxB))
    sthreshB = cv2.inRange(np.array(sat),np.array(sminB),np.array(smaxB))
    vthreshB = cv2.inRange(np.array(val),np.array(vminB),np.array(vmaxB))

    # AND h s and v
    trackingA = cv2.bitwise_and(hthreshA,cv2.bitwise_and(sthreshA,vthreshA))

    trackingB = cv2.bitwise_and(hthreshB,cv2.bitwise_and(sthreshB,vthreshB))

    # Some morpholigical filtering
    dilationA = cv2.dilate(trackingA,kernel,iterations = 1)
    closingA = cv2.morphologyEx(dilationA, cv2.MORPH_CLOSE, kernel)
    closingA = cv2.GaussianBlur(closingA,(5,5),0)

    dilationB = cv2.dilate(trackingB,kernel,iterations = 1)
    closingB = cv2.morphologyEx(dilationB, cv2.MORPH_CLOSE, kernel)
    closingB = cv2.GaussianBlur(closingB,(5,5),0)

    # Detect circles using HoughCircles
    circlesA = cv2.HoughCircles(closingA,cv.CV_HOUGH_GRADIENT,2,120,param1=120,param2=50,minRadius=10,maxRadius=0)

    circlesB = cv2.HoughCircles(closingB,cv.CV_HOUGH_GRADIENT,2,120,param1=120,param2=50,minRadius=10,maxRadius=0)
 
    if circlesA is not None:
        no_of_circlesA = len([num for elem in circlesA[0,:] for num in elem]) / 3
        if no_of_circlesA == 1:                                                       # consider only if one circle is detected.
            for i in circlesA[0,:]:
                #draw with Blue
                cv2.circle(frame,(int(round(i[0])),int(round(i[1]))),int(round(i[2])),(255,0,0),5)
                cv2.circle(frame,(int(round(i[0])),int(round(i[1]))),2,(255,0,0),10)
                trig_A = True 
        
    if circlesB is not None:
        no_of_circlesB = len([num for elem in circlesB[0,:] for num in elem]) / 3
        if no_of_circlesB == 1:                                # consider only if one circle is detected.
            for i in circlesB[0,:]:
                #draw circle with red
                cv2.circle(frame,(int(round(i[0])),int(round(i[1]))),int(round(i[2])),(0,0,255),5)
                cv2.circle(frame,(int(round(i[0])),int(round(i[1]))),2,(0,0,255),10)
                trig_B = True       
    #Show the result in frames
    #cv2.imshow('HueComp',hthreshB)
    #cv2.imshow('SatComp',sthreshB)
    #cv2.imshow('ValComp',vthreshB)
    cv2.imshow('closing_object_A',closingA)
    cv2.imshow('closing_object_B',closingB)
    cv2.imshow('tracking',frame)

    return (trig_A, trig_B)
Example #57
0
#Salva n frame nella directory frame
counter = 0
while cap and save:
    ret, frame = cap.read()
    cv2.imwrite('frame/' + str(counter) + 'frame.png', frame)
    counter = counter + 1
    if counter == numFrame:
        save = False

#Processo le foto e per ognuna calcolo il HOUG CIRCLE TRANSFORM Tresholdando ai valori hsv richiesti

    foto = cv2.imread('frame/0frame.png', cv2.IMREAD_COLOR)  #leggo il frame
    hsv = cv2.cvtColor(foto, cv2.COLOR_BGR2HSV)  #converto in hsv

    greyRed = cv2.inRange(
        hsv, lower_red,
        upper_red)  #ottengo la scala di grigi in base al threshold fornito
    greyBlue = cv2.inRange(hsv, lower_blue, upper_blue)
    greyGreen = cv2.inRange(hsv, lower_green, upper_green)

    #Applico hough circle transform settare i parametri in base alle conversioni cm in pixel del sistema di riferimento.
    circlesRed = cv2.HoughCircles(greyRed,
                                  cv2.HOUGH_GRADIENT,
                                  1,
                                  50,
                                  param1=50,
                                  param2=30,
                                  minRadius=20,
                                  maxRadius=200)
    circlesGreen = cv2.HoughCircles(greyGreen,
                                    cv2.HOUGH_GRADIENT,
Example #58
0
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
cap = cv.VideoCapture('slow_traffic_small.mp4')
# take first frame of the video
ret, frame = cap.read()

# setup initial location of window
x, y, width, height = 300, 200, 100, 50
track_window = (x, y, width, height)

# set up the ROI for tracking
roi = frame[y:y + height, x:x + width]
hsv_roi = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
mask = cv.inRange(hsv_roi, np.array((0., 60., 32.)), np.array(
    (180., 255., 255)))
roi_hist = cv.calcHist([hsv_roi], [0], mask, [180], [0, 180])

cv.normalize(roi_hist, roi_hist, 0, 255, cv.NORM_MINMAX)

# Setup the termination criteria, either 10 iteration or move by atleast 1 pt
term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1)
cv.imshow('roi', roi)
while (1):
    ret, frame = cap.read()
    if ret == True:

        hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
        dst = cv.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

        # apply meanshift to get the new location
Example #59
0
    frame = cv2.flip(frame,1)
    l_h = cv2.getTrackbarPos("L - H", "Trackbars")
    l_s = cv2.getTrackbarPos("L - S", "Trackbars")
    l_v = cv2.getTrackbarPos("L - V", "Trackbars")
    u_h = cv2.getTrackbarPos("U - H", "Trackbars")
    u_s = cv2.getTrackbarPos("U - S", "Trackbars")
    u_v = cv2.getTrackbarPos("U - V", "Trackbars")


    img = cv2.rectangle(frame, (425,100),(625,300), (0,255,0), thickness=2, lineType=8, shift=0)

    lower_blue = np.array([l_h, l_s, l_v])
    upper_blue = np.array([u_h, u_s, u_v])
    imcrop = img[102:298, 427:623]
    hsv = cv2.cvtColor(imcrop, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, lower_blue, upper_blue)
    
    cv2.putText(frame, str(img_text[1]), (30, 400), cv2.FONT_HERSHEY_TRIPLEX, 1.5, (0, 255, 0))
    cv2.imshow("test", frame)
    cv2.imshow("mask", mask)
    
    #if cv2.waitKey(1) == ord('c'):
        
#     img_name = "1.png"
    img = cv2.resize(mask, (image_x, image_y))
#     cv2.imwrite(img_name, img)
    img_text = predictor(img)
    print(str(img_text[0]))
        

    if cv2.waitKey(1) == 27:
Example #60
0
def main():
    device = RealSense("1234")
    #print("Color intrinsics: ", device.getcolorintrinsics())
    #print("Depth intrinsics: ", device.getdepthintrinsics())
    # Initiate ORB detector
    orb = cv2.ORB_create()
    flag = 500
    try:
        while True:
            image = device.getcolorstream()
            depth = device.getdepthstream()
            #image = cv2.imread("D:/Users/s_nava02/Desktop/raw_output.png")
            screenshot = image.copy()
            if flag == 0:
                cv2.imwrite("C:/GECCO/raw_output.png", screenshot)
            flag -= 1
            ###################################################
            # def gethandmask(colorframe image):
            ###################################################
            # Convert BGR to HSV
            hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
            # define range of blue color in HSV // (145, 100, 20), (155, 255, 255)
            # Todo: From RCARAP (IDK why it works so differently 'bad')
            #lower_pink = np.array([140, 0.1 * 255, 0.05 * 255])
            #upper_pink = np.array([170, 0.8 * 255, 0.6 * 255])
            # Todo: New approach, still not working as good as javascript RCARAP, it needs to be refined later
            lower_pink = np.array([130, 100, 100])
            upper_pink = np.array([170, 255, 255])
            # Threshold the HSV image to get only blue colors
            mask = cv2.inRange(hsv, lower_pink, upper_pink)
            # Bitwise-AND mask and original image
            # res = cv2.bitwise_and(colorframe, colorframe, mask=mask)
            # remove noise
            # imgray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
            # https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_filtering/py_filtering.html
            blurred = cv2.blur(mask,
                               (5, 5))  # TODO: VERY BASIC, TRY OTHER FILTERS
            # https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html
            # ret, thresholded = cv2.threshold(blurred, 50, 255, 0)  # TODO: VERY BASIC, TRY OTHER THRESHHOLDS
            ret, thresholded = cv2.threshold(blurred, 200, 255,
                                             cv2.THRESH_BINARY)
            # th3 = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
            ######################
            # return tresholded
            ######################
            #cv2.imshow('RealSense', thresholded)

            ###################################################
            # getcontours(thresholded image):
            ###################################################
            mode = cv2.RETR_EXTERNAL  # cv2.RETR_LIST
            method = cv2.CHAIN_APPROX_SIMPLE
            hand_contours = []
            contours, hierarchy = cv2.findContours(thresholded, mode, method)
            # contours = sorted(contours, key=cv2.contourArea)  # TODO: is this really necessary?
            for c in contours:
                # If contours are bigger than a certain area we push them to the array
                if cv2.contourArea(c) > 3000:
                    hand_contours.append(c)
                    #print("contour found")
            #####################
            # return hand_contours
            #####################

            # https://docs.opencv.org/3.4/dd/d49/tutorial_py_contour_features.html
            ###################################################
            # Get Rough Hull
            ###################################################
            # TODO: try to not compute convex hull twice
            # https://stackoverflow.com/questions/52099356/opencvconvexitydefects-on-largest-contour-gives-error
            for cnt in hand_contours:
                hull = cv2.convexHull(cnt)
                index = cv2.convexHull(cnt, returnPoints=False)
                # cv2.drawContours(image, cnt, 0, (255, 255, 0), 2)
                # cv2.drawContours(image, hull_list, i, (0, 255, 0), 2)

                # TODO: different ways of grouping hull points into neigbours/clusters
                # term_crit = (cv2.TERM_CRITERIA_EPS, 30, 0.1)
                # _ret, labels, centers = cv2.kmeans(np.float32(hull[:,0]), 6, None, term_crit, 10, 0)
                # point_tree = spatial.cKDTree(np.float32(hull[:,0]))
                # print("total points: ",len(np.float32(hull_list[i][:,0])), " - Total groups: ", point_tree.size)
                # neigh = NearestNeighbors(n_neighbors=2, radius=0.4)
                # output = neigh.fit(hull[:,0])
                clustering = DBSCAN(eps=10, min_samples=1).fit(hull[:, 0])
                #print(len(clustering.labels_))
                #print(hull_list[i])
                #print(clustering.labels_)
                #print(clustering.components_)
                rhull = np.column_stack((hull[:, 0], index[:, 0]))
                centers = utils.groupPointsbyLabels(rhull, clustering.labels_)
                defects = cv2.convexityDefects(cnt, np.array(centers)[:, 2])
                c = 0
                for p in hull:
                    # print("init ", p, " - ")
                    # cv2.circle(image, tuple(p[0]), 10, id_to_random_color(clustering.labels_[c]))
                    c += 1
                #for p in centers:
                #print("init ", p[0], " - ")
                #cv2.circle(image, (p[0],p[1]), 4, (0, 255, 255))
                #pass
                for p in centers:
                    # cv2.circle(image, (int(p[0]), int(p[1])), 4, (0, 255, 255))
                    pass
                ###############################################################
                # getHullDefectVertices
                ###############################################################
                # get neighbor defect points of each hull point
                hullPointDefectNeighbors = []  # 0: start, 1: end, 2:defect
                print("defects.shape[0]: ", defects.shape[0])
                for x in range(defects.shape[0]):
                    s, e, f, d = defects[x, 0]
                    start = tuple(cnt[s][0])
                    end = tuple(cnt[e][0])
                    far = tuple(cnt[f][0])
                    cv2.line(image, start, end, [0, 255, 0], 1)
                    cv2.circle(image, far, 4, (0, 0, 255))
                    cv2.line(image, start, far, [255, 150, 0], 1)
                    cv2.line(image, end, far, [255, 150, 0], 1)
                    hullPointDefectNeighbors.append(
                        [start, end, far]
                    )  # each defect point (red) has its neihbour points (yellow)
                ###############################################################
                # filterVerticesByAngle
                ###############################################################
                #maxAngleDeg = 60
                maxAngleDeg = math.radians(60)
                i = 0
                fingers = []
                for triple in hullPointDefectNeighbors:
                    cf = triple[0]  # candidate finger
                    rd = triple[2]  # right deflect
                    if i == 0:  # left deflect
                        ld = hullPointDefectNeighbors[
                            len(hullPointDefectNeighbors) - 1][2]
                    else:
                        ld = hullPointDefectNeighbors[i - 1][2]
                    # alternative maths
                    v_cp_ld = (ld[0] - cf[0], ld[1] - cf[1])
                    v_cp_rd = (rd[0] - cf[0], rd[1] - cf[1])
                    beta = angle_between(v_cp_ld, v_cp_rd)
                    print(beta)
                    cv2.circle(image, (cf[0], cf[1]), 4,
                               (0, 0, 255))  # candidate finger: red
                    cv2.circle(image, (rd[0], rd[1]), 4,
                               (255, 0, 0))  # right defect: blue
                    cv2.circle(image, (ld[0], ld[1]), 4,
                               (255, 0, 0))  # left defect: blue
                    if beta < maxAngleDeg:
                        fingers.append(cf)
                    # old maths
                    #if (math.atan2(cf[1] - rd[1], cf[0] - rd[0]) < maxAngleDeg) and (
                    #            math.atan2(cf[1] - ld[1], cf[0] - ld[0]) < maxAngleDeg) and len(fingers) < 5:
                    #    fingers.append(triple[0])
                    i += 1
                print(len(fingers))
                for f in fingers:
                    cv2.circle(image, (f[0], f[1]), 4,
                               (255, 255, 255))  # identified finger: white
                    print("image size: ", image.shape)
                    print("color pixel value of ", f, ":", image[f[0]][f[1]])
                    pass

            # Show images
            cv2.namedWindow("Output Frame", cv2.WND_PROP_FULLSCREEN)
            cv2.setWindowProperty("Output Frame", cv2.WND_PROP_FULLSCREEN,
                                  cv2.WINDOW_NORMAL)
            cv2.imshow('Output Frame', image)
            cv2.waitKey(1)

    finally:
        # Stop streaming
        device.stop()
        pass