Example #1
0
 def __call__(self, arr, aux=None):
     shape = arr.shape
     if self.move is not None:
         center = random_center(shape, self.move)
         arr_ret = crop(arr, center, self.size)
         angle = np.random.randint(4, size=3)
         arr_ret = rotation(arr_ret, angle=angle)
         axis = np.random.randint(4) - 1
         arr_ret = reflection(arr_ret, axis=axis)
         # order = np.random.permutation(3)  # generate order
         # arr_ret = reorder(arr_ret, order)
         # zoom_rate = 0.8 + (1.15 - 0.8) * np.random.rand()
         # (arr_ret, _) = resize(arr_ret, [1., 1., 1.], new_spacing=[zoom_rate] * 3)
         # zoomed_center = arr_ret.shape // 2
         # arr_ret = crop_at_zyx_with_dhw(arr_ret, zoomed_center, self.size, 0)
         arr_ret = np.expand_dims(arr_ret, axis=-1)
         if aux is not None:
             aux_ret = crop(aux, center, self.size)
             aux_ret = rotation(aux_ret, angle=angle)
             aux_ret = reflection(aux_ret, axis=axis)
             # aux_ret = reorder(aux_ret, order=order)
             # (aux_ret, _) = resize(aux_ret, [1., 1., 1.], new_spacing=[zoom_rate] * 3)
             # aux_ret = crop_at_zyx_with_dhw(aux_ret, zoomed_center, self.size, 0)
             aux_ret = np.expand_dims(aux_ret, axis=-1)
             return arr_ret, aux_ret
         return arr_ret
     else:
         center = np.array(shape) // 2
         arr_ret = crop(arr, center, self.size)
         arr_ret = np.expand_dims(arr_ret, axis=-1)
         if aux is not None:
             aux_ret = crop(aux, center, self.size)
             aux_ret = np.expand_dims(aux_ret, axis=-1)
             return arr_ret, aux_ret
         return arr_ret
Example #2
0
def corr2d(img, kernel):
    """
        Args:
        img   : nested list (int), image.
        kernel: nested list (int), kernel.
        
        Returns:
        img_conv: nested list (int), image.
        """
    padding_layer = len(kernel) // 2
    img = utils.zero_pad(img, padding_layer,
                         padding_layer)  # padding the image

    row_count = len(img)
    col_count = len(img[0])

    # output image having same size as the input image
    img_corr = []

    zeros = [0] * (col_count - 2 * padding_layer)

    for i in range(row_count - 2 * padding_layer):
        img_corr.insert(0, [0 for value in enumerate(zeros)])

    kernel_h = len(kernel)
    kernel_w = len(kernel[0])

    img = np.asarray(img, dtype=np.float32)
    kernel = np.asarray(kernel, dtype=np.float32)

    for i in range(row_count - kernel_h + 1):
        for j in range(col_count - kernel_w + 1):

            mult_result = utils.elementwise_mul(
                utils.crop(img, i, i + kernel_h, j, j + kernel_w), kernel)

            sqr_img = utils.elementwise_mul(
                utils.crop(img, i, i + kernel_h, j, j + kernel_w),
                utils.crop(img, i, i + kernel_h, j, j + kernel_w))

            sqr_ker = utils.elementwise_mul(kernel, kernel)

            sum = 0
            sum_sqr_img = 0
            sum_sqr_ker = 0

            for p in range(len(mult_result)):
                for q in range(len(mult_result[p])):
                    sum += mult_result[p][q]
                    sum_sqr_img += sqr_img[p][q]
                    sum_sqr_ker += sqr_ker[p][q]

            img_corr[i][j] = sum / np.sqrt(sum_sqr_img * sum_sqr_ker)

    img_corr = [list(row) for row in img_corr]

    return img_corr
Example #3
0
 def gen_sim(self, cls, ifcrop=True, cropsize=(240, 120)):
     [iA, iB] = self.choice(cls, 2)
     A = self.getd(iA)
     B = self.getd(iB)
     if (ifcrop):
         A = crop(A, cropsize[0], cropsize[1])
         B = crop(B, cropsize[0], cropsize[1])
     # print A.shape, B.shape
     AB = np.concatenate((A, B), axis=1)
     # if (ifcrop): return crop(AB,cropsize[0],cropsize[1])
     return AB
Example #4
0
 def gen_sim(self, cls, ifcrop=True, cropsize=(240,120)):
     [iA, iB] = self.choice(cls, 2)
     A = self.getd(iA)
     B = self.getd(iB)
     if (ifcrop):
         A = crop(A,cropsize[0],cropsize[1])
         B = crop(B,cropsize[0],cropsize[1])
     # print A.shape, B.shape
     AB = np.concatenate((A,B),axis=1)
     # if (ifcrop): return crop(AB,cropsize[0],cropsize[1])
     return AB
def convolve2d(img, kernel):
    """Convolves a given image and a given kernel.

    Steps:
        (1) flips the either the img or the kernel.
        (2) pads the img or the flipped img.
            this step handles pixels along the border of the img,
            and makes sure that the output img is of the same size as the input image.
        (3) applies the flipped kernel to the image or the kernel to the flipped image,
            using nested for loop.

    Args:
        img: nested list (int), image.
        kernel: nested list (int), kernel.

    Returns:
        img_conv: nested list (int), image.
    """
    # TODO: implement this function.
    # raise NotImplementedError

    kernel_flip = utils.flip2d(kernel)
    img_pad = utils.zero_pad(img,1,1)

    kernel_row = len(kernel)
    kernel_col = len(kernel[0])

    # img_conv = np.zeros(np.shape(img_pad))

    image_ = copy.deepcopy(img_pad)
    # print(kernel_flip_y)

    # for row_index,row_value in enumerate(img_pad[1:-1]):
    #     for col_index, col_value in enumerate(row_value[1:-1]):
    #        sum_ = 0 
    #        for i in range(-1,2):
    #            for j in range(-1,2):
    #                sum_ += img_pad[row_index+i][col_index+j]*kernel_flip_y[1+i][1+j]
    #        image_[row_index][col_index]= sum_   


    for row_index, row_value in enumerate(img_pad[:-2]):
        for col_index,col_val in enumerate(row_value[:-2]):
            img_temp = utils.crop(img_pad,row_index,(row_index+kernel_row),col_index,(col_index+kernel_col))
            imp_temp_1 = utils.elementwise_mul(img_temp,kernel)
            img_conv_sum = pixel_conv_sum(imp_temp_1)
            image_[row_index+1][col_index+1] = img_conv_sum


    img_conv = image_
    img_conv = utils.crop(img_conv,1,257,1,257)
    # print(f'The Type for convo is {type(img_conv)}')
    return img_conv
Example #6
0
 def gen_dif(self, cls, ifcrop=True, cropsize=(240,120)):
     [iA] = self.choice(cls, 1)
     [iB] = self.choice(self.rand_not_cls(cls), 1)
     A = self.getd(iA)
     B = self.getd(iB)
     # print A.shape, B.shape
     if (ifcrop):
         A = crop(A,cropsize[0],cropsize[1])
         B = crop(B,cropsize[0],cropsize[1])
     # print A.shape, B.shape
     AB = np.concatenate((A,B),axis=1)
     # if (ifcrop): return crop(AB,cropsize[0],cropsize[1])
     return AB
Example #7
0
 def gen_dif(self, cls, ifcrop=True, cropsize=(240, 120)):
     [iA] = self.choice(cls, 1)
     [iB] = self.choice(self.rand_not_cls(cls), 1)
     A = self.getd(iA)
     B = self.getd(iB)
     # print A.shape, B.shape
     if (ifcrop):
         A = crop(A, cropsize[0], cropsize[1])
         B = crop(B, cropsize[0], cropsize[1])
     # print A.shape, B.shape
     AB = np.concatenate((A, B), axis=1)
     # if (ifcrop): return crop(AB,cropsize[0],cropsize[1])
     return AB
Example #8
0
def get_hands(frame, guitar):
    # Compute pickhand and frethand crop using guitar model
    scale = frame.shape[1]/1920.0
    pickhand_coords = [[int(scale*i) for i in [   0,  520,  800, 1080]]] #DEBUG
    frethand_coords = [[int(scale*i) for i in [ 800,  300, 1800,  900]]] #DEBUG

    # Interpret hand areas to get hand models for both hands
    pickhand = to_handmodel(utils.crop(frame, pickhand_coords), 'dn')
    frethand = to_handmodel(utils.crop(frame, frethand_coords), 'up')
    
    # Uncrop hands to larger frame
    pickhand = map(lambda p: utils.reposition(p, pickhand_coords), pickhand)
    frethand = map(lambda p: utils.reposition(p, frethand_coords), frethand)
    return (pickhand, frethand)
Example #9
0
def get_hands(frame, guitar):
    # Compute pickhand and frethand crop using guitar model
    scale = frame.shape[1] / 1920.0
    pickhand_coords = [[int(scale * i) for i in [0, 520, 800, 1080]]]  #DEBUG
    frethand_coords = [[int(scale * i) for i in [800, 300, 1800, 900]]]  #DEBUG

    # Interpret hand areas to get hand models for both hands
    pickhand = to_handmodel(utils.crop(frame, pickhand_coords), 'dn')
    frethand = to_handmodel(utils.crop(frame, frethand_coords), 'up')

    # Uncrop hands to larger frame
    pickhand = map(lambda p: utils.reposition(p, pickhand_coords), pickhand)
    frethand = map(lambda p: utils.reposition(p, frethand_coords), frethand)
    return (pickhand, frethand)
Example #10
0
def match(img, template):
    """Locates the template, i.e., a image patch, in a large image using template matching techniques, i.e., NCC.

    Args:
        img: nested list (int), image that contains character to be detected.
        template: nested list (int), template image.

    Returns:
        x (int): row that the character appears (starts from 0).
        y (int): column that the character appears (starts from 0).
        max_value (float): maximum NCC value.
    """
    # TODO: implement this function.
    patching_rows = len(img) - len(template) + 1
    patching_cols = len(img[0]) - len(template[0]) + 1

    max_value = 0
    x = 0
    y = 0
    for i in range(patching_rows):
        for j in range(patching_cols):
            xmin = i
            xmax = i + len(template)
            ymin = j
            ymax = j + len(template[0])
            patch = utils.crop(img, xmin, xmax, ymin, ymax)
            current_temp = norm_xcorr2d(patch, template)
            if (max_value < current_temp):
                max_value = current_temp
                x = i
                y = j

    return x, y, max_value
Example #11
0
def convolve2d(img, kernel):
    """Convolves a given image and a given kernel.

    Steps:
        (1) flips the either the img or the kernel.
        (2) pads the img or the flipped img.
            this step handles pixels along the border of the img,
            and makes sure that the output img is of the same size as the input image.
        (3) applies the flipped kernel to the image or the kernel to the flipped image,
            using nested for loop.

    Args:
        img: nested list (int), image.
        kernel: nested list (int), kernel.

    Returns:
        img_conv: nested list (int), image.
    """
    # TODO: implement this function.
    # raise NotImplementedError
    flipped_img = utils.flip2d(img)
    convoluted_img = copy.deepcopy(flipped_img)

    padded_img = utils.zero_pad(convoluted_img, 1, 1)

    for i in range(len(convoluted_img)):
        for j in range(len(convoluted_img[0])):
            convoluted_img[i][j] = utils.add_matrix(
                utils.elementwise_mul(
                    utils.crop(padded_img, i, i + 3, j, j + 3), kernel))

    return utils.flip2d(convoluted_img)
Example #12
0
def plates_detection_test():
    import platesdetector
    plts_detector = platesdetector.PlatesDetector()

    IMAGE = 'data/images/my_extras/one_car_plates2.jpg'
    im_name = 'one_car_plates2'
    im_reader = video_reader.Reader(IMAGE)

    ret, frame = im_reader.read()

    image_h, image_w, _ = frame.shape

    if image_h > 1080:
        frame = cv2.resize(frame, None, fx=0.5, fy=0.5)

    plates_detection_successful, plates_box, confidence = \
        plts_detector.detect_plates(frame)

    print(plates_box)

    plates_crop = crop(frame, plates_box)

    cv2.imshow('[INFO, platesdetector]: plates_image', plates_crop)

    cv2.waitKeyEx(0)

    i = 0
    cv2.imwrite(im_name + str(i) + '.jpg', plates_crop)
Example #13
0
def extractStamps(image, stampLocations):
    result = []
    for rect in stampLocations:
        lu = rect[0]
        rd = rect[1]
        result.append(utils.crop(image, lu, rd))
    return result
Example #14
0
def convolve2d(img, kernel):
    """Convolves a given image and a given kernel.

    Steps:
        (1) flips the kernel
        (2) pads the image # IMPORTANT
            this step handles pixels along the border of the image, and ensures that the output image is of the same size as the input image
        (3) calucates the convolved image using nested for loop

    Args:
        img: nested list (int), image.
        kernel: nested list (int), kernel.

    Returns:
        img_conv: nested list (int), convolved image.
    """
    # TODO: implement this function.
    flipped_kernel = utils.flip2d(kernel)
    pad_size = int((len(kernel) - 1) / 2)
    padded_img = utils.zero_pad(img, pad_size, pad_size)
    filtered_img = copy.deepcopy(img)

    for i, row in enumerate(img):
        for j, pix in enumerate(row):
            patch = utils.crop(padded_img, i, i + 2 * pad_size + 1, j,
                               j + 2 * pad_size + 1)
            patch = utils.elementwise_mul(patch, kernel)
            filtered_img[i][j] = sum([sum(l) for l in patch])

    return filtered_img
Example #15
0
def main(frame):
    # Get the guitar's POI in the frame
    guitar = get_guitar( utils.crop( frame, locate.guitar(frame) ) )
    # Write status
    sys.stderr.write(errorstring.format(guitar))
    # Return overlaid guitar attributes on frame
    return display(frame, guitar)
Example #16
0
def detect(img, template, threshold):
    img = normalize(img)
    template = normalize(template)

    # final = img
    coordinates = []
    # img_c = task1.detect_edges(img, template)
    x = len(img)
    y = len(img[0])
    xt = len(template) // 2
    yt = len(template)
    for i in range(xt, x - yt):
        for j in range(yt, y - xt):
            temp_img = utils.crop(img, (i - xt), (i + xt + 1), (j - yt),
                                  (j + yt + 1))
            ss = ssd(temp_img, template)
            if ss < threshold:
                coordinates.append([i, j])
    """Detect a given character, i.e., the character in the template image.

    Args:
        img: nested list (int), image that contains character to be detected.
        template: nested list (int), template image.

    Returns:
        coordinates: list (tuple), a list whose elements are coordinates where the character appears.
            format of the tuple: (x (int), y (int)), x and y are integers.
            x: row that the character appears (starts from 0).
            y: column that the character appears (starts from 0).
    """
    # TODO: implement this function.
    # raise NotImplementedError
    return coordinates
Example #17
0
def telemetry(sid, data):
    # The current steering angle of the car
    steering_angle = data["steering_angle"]

    # The current throttle of the car
    throttle = data["throttle"]

    # The current speed of the car
    speed = data["speed"]

    # The current image from the center camera of the car
    imgString = data["image"]
    image = Image.open(BytesIO(base64.b64decode(imgString)))
    image_array = np.asarray(image)

    image_array = utils.crop(image_array, 0.35, 0.1)
    image_array = utils.resize(image_array, new_dim=(64, 64))

    transformed_image_array = image_array[None, :, :, :]

    # This model currently assumes that the features of the model are just the images. Feel free to change this.

    steering_angle = float(model.predict(transformed_image_array,
                                         batch_size=1))
    # The driving model currently just outputs a constant throttle. Feel free to edit this.
    throttle = 0.3

    print('{:.5f}, {:.1f}'.format(steering_angle, throttle))

    send_control(steering_angle, throttle)
Example #18
0
def inference(image, model, n, p):
    '''
    Performs inference.
    '''
    # read the image
    x = cv2.imread(image, 0)
    print(x.shape)
    # resize
    x = cv2.resize(x, (28 * n, 28), cv2.INTER_AREA)
    # get the crops
    x = np.vstack([i[None] for i in crop(x, n, 28)])
    # convert to a 4D tensor
    x = x.reshape(n, 28, 28, 1)
    # normalize it
    x = x.astype('float32')
    x /= 255

    #perform the prediction
    model = load_model(args.model)
    out = model.predict_on_batch(x)

    # Add the precision
    output = map(str, np.argmax(out, -1).tolist())
    output = ''.join(output)
    if p == 0: output = output + '.'
    elif p == n: output = '.' + output
    else: output = output[:-p] + '.' + output[-p:]
    print(output)
def corr_func(img, template):
    r1 = len(img)
    c1 = len(img[0])
    r2 = len(template)
    c2 = len(template[0])
    rows = r1 - r2 + 1
    cols = c1 - c2 + 1

    corr = np.ones(shape=(rows, cols))

    val1 = subtract(template, cal_mean(template))
    meanTemplate_Square = np.multiply((val1), val1)
    meanTemplateSquare_Sum = addAllElements(meanTemplate_Square)

    for i in range(rows):
        for j in range(cols):
            cropped_img = utils.crop(img, i, i + r2, j, j + c2)
            cropped_img_mean = subtract(cropped_img, cal_mean(cropped_img))

            numerator = task1.multiply_add(cropped_img_mean, val1)
            crop_sum = task1.multiply_add(cropped_img_mean, cropped_img_mean)
            mulValue = crop_sum * meanTemplateSquare_Sum
            denominator = mulValue**(1 / 2)
            if (denominator != 0):
                corr[i][j] = numerator / denominator
            else:
                corr[i][j] = 0
    return corr
Example #20
0
def convolve2d(img, kernel):
    """Convolves a given image and a given kernel.

    Steps:
        (1) flips either the img or the kernel.
        (2) pads the img or the flipped img.
            this step handles pixels along the border of the img,
            and makes sure that the output img is of the same size as the input image.
        (3) applies the flipped kernel to the image or the kernel to the flipped image,
            using nested for loop.

    Args:
        img: nested list (int), image.
        kernel: nested list (int), kernel.

    Returns:
        img_conv: nested list (int), image.
    """
    # TODO: implement this function.
    flipped_img = utils.flip2d(img)
    padded_img = utils.zero_pad(flipped_img, 1, 1)
    img_conv = np.zeros((len(img), len(img[0])))
    for i in range(1, len(padded_img)-2):
        for j in range(1, len(padded_img[0])-2):
            img_sec = utils.crop(padded_img, i-1, i+2, j-1, j+2)
            prod = utils.elementwise_mul(kernel, img_sec)
            res = 0
            for g in range(len(prod)):
                for h in range(len(prod[0])):
                    res += prod[g][h]
            img_conv[i-1][j-1] = res
    img_conv = utils.flip2d(img_conv)
    # raise NotImplementedError
    return img_conv
Example #21
0
def match(img, template):
    """Locates the template, i.e., a image patch, in a large image using template matching techniques, i.e., NCC.

    Args:
        img: nested list (int), image that contains character to be detected.
        template: nested list (int), template image.

    Returns:
        x (int): row that the character appears (starts from 0).
        y (int): column that the character appears (starts from 0).
        max_value (float): maximum NCC value.
    """
    # TODO: implement this function.
    #Initialization
    max_ncc = 0
    x_pos = 0
    y_pos = 0

    for a in range(len(img)):
        for b in range(len(img[0])):
            patch = utils.crop(img, a, a + len(template), b,
                               b + len(template))  # Cropping the patch
            ncc_value = norm_xcorr2d(patch, template)
            if ncc_value >= max_ncc:  # Calculating max ncc value
                max_ncc = ncc_value
                x_pos = a
                y_pos = b

    return x_pos, y_pos, max_ncc  # Returning x position, y position and NCC value
Example #22
0
    def showImage(self):
        if self.image_mat is None:
            return

        if self.is_edit_mode:
            display_img_mat = draw_border(self.image_mat, self.corners)
        else:
            self.final_mat = crop(self.image_mat, self.corners)
            display_img_mat = self.final_mat

        # Convert image_matrix to QPixmap
        self.image = convert_ndarray_to_QPixmap(display_img_mat)

        # scale the image to display
        self.image = self.image.scaled(self.image_label.size(),
                                       Qt.KeepAspectRatio,
                                       Qt.SmoothTransformation)

        # get scale ratio
        original_height: int = display_img_mat.shape[0]
        self.scale_ratio: float = original_height / self.image.height()

        # show the image on screen
        self.image_label.setPixmap(self.image)

        # Update corners
        for i in range(4):
            self.corner_labels[i].setText(str(self.corners[i]))
def match(img, template):
    """Locates the template, i.e., a image patch, in a large image using template matching techniques, i.e., NCC.

    Args:
        img: nested list (int), image that contains character to be detected.
        template: nested list (int), template image.

    Returns:
        x (int): row that the character appears (starts from 0).
        y (int): column that the character appears (starts from 0).
        max_value (float): maximum NCC value.
    """
    # TODO: implement this function.
    # raise NotImplementedError
    # raise NotImplementedError
    x_temp_len = len(template)  # rows : 24
    y_temp_len = len(template[0])  # columns : 26
    max_value = 0
    xmax = x_temp_len
    xmin = 0
    while xmax < len(img):
        ymin = 0
        ymax = y_temp_len
        while ymax < len(img[0]):
            patch = utils.crop(img, xmin, xmax, ymin, ymax)
            ncc_val = norm_xcorr2d(patch, template)
            if ncc_val > max_value:
                max_value = ncc_val
                x = xmin
                y = ymin
            ymin = ymin + 1
            ymax = ymax + 1
        xmin = xmin + 1
        xmax = xmax + 1
    return x, y, max_value
Example #24
0
def detectTimeViaSegmentation(timeRegion, symbols, debug):
    ret, threshold = cv2.threshold(timeRegion, 190, 255, 0)
    threshold = cv2.bitwise_not(threshold)
    contourImage, contours, hierarchy = cv2.findContours(
        threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    digitSymbolNames = ['A', 'P', 'M'] + map(lambda nr: "{}".format(nr),
                                             range(10))
    digitMap = {key: symbols.get(key) for key in digitSymbolNames}

    largeContours = [x for x in contours if cv2.contourArea(x) > 100]
    # the '0' will yield two contours, therefore;
    largeContours = removeContoursEncapsulatedByContours(largeContours, debug)
    if debug: displayContoursOnGrey(contourImage, largeContours)

    hits = []
    for largeContour in largeContours:
        margin = 5
        x, y, w, h = cv2.boundingRect(largeContour)
        lu = (x - margin, y - margin)
        rd = (x + w + margin, y + h + margin)
        charCrop = crop(timeRegion, lu, rd)
        foundSymbols = matchSymbols(charCrop, digitMap, debug)
        symbol, count = mostFrequentSymbol(foundSymbols)
        hits.append((symbol, lu))

    hits.sort(key=lambda hitSymbol: hitSymbol[1][0])  # sort by x
    if debug: print "sorted hits: {}".format(hits)
    result = ''.join([hit[0] for hit in hits])
    return result
    def process(self, frame):
        orig_image = frame.copy()

        rois = self.face_detector.infer((frame, ))
        if self.QUEUE_SIZE < len(rois):
            log.warning(
                'Too many faces for processing. Will be processed only {} of {}'
                .format(self.QUEUE_SIZE, len(rois)))
            rois = rois[:self.QUEUE_SIZE]

        landmarks = self.landmarks_detector.infer((frame, rois))
        face_identities, unknowns = self.face_identifier.infer(
            (frame, rois, landmarks))
        if self.allow_grow and len(unknowns) > 0:
            for i in unknowns:
                # This check is preventing asking to save half-images in the boundary of images
                if rois[i].position[0] == 0.0 or rois[i].position[1] == 0.0 or \
                    (rois[i].position[0] + rois[i].size[0] > orig_image.shape[1]) or \
                    (rois[i].position[1] + rois[i].size[1] > orig_image.shape[0]):
                    continue
                crop_image = crop(orig_image, rois[i])
                name = self.faces_database.ask_to_save(crop_image)
                if name:
                    id = self.faces_database.dump_faces(
                        crop_image, face_identities[i].descriptor, name)
                    face_identities[i].id = id

        return [rois, landmarks, face_identities]
Example #26
0
def extractRoiRectFromStamp(stamp, debug):
    circle = detectCirclesViaFiltered(stamp, 1, debug)[0]
    lu = (circle[0] - circle[2], circle[1] - circle[2])
    rd = (circle[0] + circle[2], circle[1] + circle[2])
    roiRect = utils.crop(stamp, lu, rd)
    if debug: utils.display(roiRect, "rect")
    return roiRect
Example #27
0
def match(img, template):
    """Locates the template, i.e., a image patch, in a large image using template matching techniques, i.e., NCC.

    Args:
        img: nested list (int), image that contains character to be detected.
        template: nested list (int), template image.

    Returns:
        x (int): row that the character appears (starts from 0).
        y (int): column that the character appears (starts from 0).
        max_value (float): maximum NCC value.
    """
    # TODO: implement this function.
    # raise NotImplementedError
    final_x_cor = -1
    final_y_cor = -1
    final_max = -1
    for i, row in enumerate(img):
        for j, num in enumerate(row):
            if i > len(img) - len(template) or j > len(img[0]) - len(
                    template[0]):
                continue
            imgfoc = utils.crop(img, i, i + len(template), j,
                                j + len(template[0]))
            ncc = norm_xcorr2d(imgfoc, template)
            if ncc > final_max:
                final_x_cor = i
                final_y_cor = j
                final_max = ncc
    return final_x_cor, final_y_cor, final_max
    raise NotImplementedError
Example #28
0
def test_crop_v():
    img = read_test_image('dog.jpg')
    aspect_ratio = 3 / 4
    img, _ = utils.crop(img, aspect_ratio)
    w, h = img.size
    assert w / h == pytest.approx(aspect_ratio)
    img.save("result_crop_v.jpg")
def convolve2d(img, kernel):
    """Convolves a given image and a given kernel.

    Steps:
        (1) flips the kernel
        (2) pads the image # IMPORTANT
            this step handles pixels along the border of the image, and ensures that the output image is of the same size as the input image
        (3) calucates the convolved image using nested for loop

    Args:
        img: nested list (int), image.
        kernel: nested list (int), kernel.

    Returns:
        img_conv: nested list (int), convolved image.
    """
    # TODO: implement this function.
    kl = len(kernel)
    pad = int(len(kernel) / 2)
    kernel = utils.flip2d(kernel)
    img_padded = utils.zero_pad(img, pad, pad)
    for x, row in enumerate(img):
        for y, num in enumerate(row):
            if (x < len(img[0]) - (round(len(kernel) / 2))
                    or y < len(img) - (round(len(kernel) / 2))):
                imgfoc = utils.elementwise_mul(
                    kernel, utils.crop(img_padded, x, x + kl, y, y + kl))
                sumele = 0
                for i in range(len(imgfoc)):
                    sumele += sum(subl[i] for subl in imgfoc)
                img[x][y] = sumele
    return img
    raise NotImplementedError
def correlate(img, kernel):
    height = len(img)
    width = len(img[0])
    k_height = len(kernel)
    k_width = len(kernel[0])
    img_corr = []
    temp_mean = mean(kernel)
    temp_total = 0
    for p in range(k_height):
        for q in range(k_width):
            temp_total += (kernel[p][q] - temp_mean)**2

    for i in range(height - k_height + 1):
        row = []
        for j in range(width - k_width + 1):
            c = utils.crop(img, i, i + k_height, j, j + k_width)
            c_mean = mean(c)
            c_total = total = 0
            for p in range(k_height):
                for q in range(k_width):
                    c_total += (c[p][q] - c_mean)**2
                    total += (c[p][q] - c_mean) * (kernel[p][q] - temp_mean)
            if c_total == 0 or temp_total == 0:
                total = 0
            else:
                total = total / ((c_total**0.5) * (temp_total**0.5))
            row.append(total)
        img_corr.append(row)
    #raise NotImplementedError
    return img_corr
Example #31
0
def main(frame):
    # Get the guitar's POI in the frame
    guitar = get_guitar(utils.crop(frame, locate.guitar(frame)))
    # Write status
    sys.stderr.write(errorstring.format(guitar))
    # Return overlaid guitar attributes on frame
    return display(frame, guitar)
Example #32
0
def getChoiceValue(img):
  rows = split(img, r= 5)

  result = []

  for i, r in enumerate(rows):
    rows[i] = crop(r)
    
    subrows = split(rows[i], r=5)

    for sr in subrows:
      cols = split(sr, c=4)

      wp = []

      for col in cols:
          wp.append(countWhitePoint(col))

      ind = wp.index(max(wp))
      res = "A"
      if ind == 1:
        res = "B"
      if ind == 2:
        res = "C"
      if ind == 3:
        res = "D"

      result.append(res)
  return result
def telemetry(sid, data):
    if data:
        # The current steering angle of the car
        steering_angle = data["steering_angle"]
        # The current throttle of the car
        throttle = data["throttle"]
        # The current speed of the car
        speed = data["speed"]
        # The current image from the center camera of the car
        imgString = data["image"]
        image = Image.open(BytesIO(base64.b64decode(imgString)))
        image_array = np.asarray(image)
        ### Add check model input size
        if model.input_shape == (None, 64, 64, 3):
            image_array = crop(image_array)
            image_array = resize(image_array, 64, 64)
        steering_angle = float(
            model.predict(image_array[None, :, :, :], batch_size=1))

        throttle = controller.update(float(speed))

        print(steering_angle, throttle)
        send_control(steering_angle, throttle)

        # save frame
        if args.image_folder != '':
            timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3]
            image_filename = os.path.join(args.image_folder, timestamp)
            image.save('{}.jpg'.format(image_filename))
    else:
        # NOTE: DON'T EDIT THIS.
        sio.emit('manual', data={}, skip_sid=True)
Example #34
0
def match(img, template):
    """Locates the template, i.e., a image patch, in a large image using template matching techniques, i.e., NCC.

    Args:
        img: nested list (int), image that contains character to be detected.
        template: nested list (int), template image.

    Returns:
        x (int): row that the character appears (starts from 0).
        y (int): column that the character appears (starts from 0).
        max_value (float): maximum NCC value.
    """
    template_x = len(template)
    template_y = len(template[0])

    maximum = -1
    max_x = -1
    max_y = -1

    for x, val_x in enumerate(img[:-template_x]):
        for y, val_y in enumerate(val_x[:-template_y]):
            patch = utils.crop(img, x, x + template_x, y, y + template_y)
            NCC = norm_xcorr2d(patch, template)
            if NCC >= maximum:
                maximum = NCC
                max_x = x
                max_y = y

    return max_x, max_y, maximum
Example #35
0
def find_pipes(pipe, background):
    # first pipe, this takes ~2-2.5 ms to complete, so can do further cropping to reduce
    # time, see TODO

    res0, min_val0, max_val0, top_left0, bottom_right0 = find_object(pipe, background)
    top_left0 = shift(top_left0, (0, BIRD_EDGE))
    bottom_right0 = shift(bottom_right0, (0, BIRD_EDGE))
    
    width = int(PIPE_SEPARATION_WIDTH * RESIZE)
    # for finding the second pipe, looks up and down, then finds the closest match
    min_x = top_left0[0] - DELTA + width
    max_x = bottom_right0[0] + DELTA + width
    min_y = top_left0[1] - DELTA
    max_y = bottom_right0[1] + DELTA

    crop_bg1 = crop(background, min_x, max_x, min_y, max_y)

    if crop_bg1.shape[0] > pipe.shape[0] and crop_bg1.shape[1] > pipe.shape[1]:
        res1, min_val1, max_val1, top_left1, bottom_right1 = find_object(pipe, crop_bg1)
        corner = min_x, min_y
        top_left1 = shift(corner, top_left1)
        bottom_right1 = shift(corner, bottom_right1)
    else:
        max_val1 = 0
    
    min_x = top_left0[0] - DELTA - width
    max_x = bottom_right0[0] + DELTA - width
    min_y = top_left0[1] - DELTA
    max_y = bottom_right0[1] + DELTA

    crop_bg2 = crop(background, min_x, max_x, min_y, max_y)

    if crop_bg2.shape[0] > pipe.shape[0] and crop_bg2.shape[1] > pipe.shape[1]:
        res2, min_val2, max_val2, top_left2, bottom_right2 = find_object(pipe, crop_bg2)
        corner = min_x, min_y
        top_left2 = shift(corner, top_left2)
        bottom_right2 = shift(corner, bottom_right2)
    else:
        max_val2 = 0

    
    if max_val1 > max_val2:
        return top_left0, bottom_right0, top_left1, bottom_right1
    else:
        return top_left0, bottom_right0, top_left2, bottom_right2
Example #36
0
    def __call__(self, data):
        num = data.shape[0]
        shape = self.net.blobs['data'].data.shape
        crop_data = np.zeros([num,shape[1],shape[2],shape[3]],np.uint8)
        for i in xrange(num):
            crop_data[i] = crop(data[i],shape[2],shape[3])

        self.net.blobs['data'].reshape(*crop_data.shape)
        return self.net.forward(data=crop_data)['feat'].reshape(num,-1).copy()
Example #37
0
    def run_algo(self, x, y, w, h, i, crop_whole_sequence):
        """
        """
        tif_paths = ast.literal_eval(self.cfg['param']['tif_paths'])
        if crop_whole_sequence:
            for k in xrange(1, len(tif_paths)+1):
                utils.crop(os.path.join(self.input_dir, tif_paths[k-1]),
                        os.path.join(self.work_dir, 'crop_%02d.tif' % k), x, y,
                        w, h)
                utils.qauto(os.path.join(self.work_dir, 'crop_%02d.tif' % k),
                        os.path.join(self.work_dir, 'crop_%02d.png' % k))
        else:
            utils.crop(os.path.join(self.input_dir, tif_paths[i-1]),
                    os.path.join(self.work_dir, 'crop_%02d.tif' % i), x, y, w, h)
            utils.qauto(os.path.join(self.work_dir, 'crop_%02d.tif' % i),
                    os.path.join(self.work_dir, 'crop_%02d.png' % i))

        return
Example #38
0
    def execute(self, img, context):
        gray = utils.gray(utils.crop(img, self.NUM_RECT))
        type = cv2.THRESH_BINARY | cv2.THRESH_OTSU
        ret, bin = cv2.threshold(gray, 60, 255, type)
        #cv2.imshow('tyoshi', bin)
        #cv2.waitKey(1)

        context.clear()
        context['tyoshi'] = utils.text(bin, "+0123456789")
Example #39
0
from utils import (load, take, show, bgr, image, like, bounds,
channels, crop, scale, color, avail, colorPicker)
from proto import alias, sharpen, group, find, edge, center, distance
from PIL import Image

print "# fast stuff"
img = load('samples/abstract/colors.png')
#b = take()
show(img)
b, g, r = bgr(img)
img = image(b,b,b)
test = like(img)
bound = bounds(b)
channel = channels(b)
coord = (0,0,50,50)
closer = crop(img, coord)
bigger = scale(closer, 2.0)
eyedrop = color(img, 0, 30)
pallet = avail(img)
colorPicker(img,0,30)

print "# slow stuff"
res1 = alias(img, .3)
res2 = sharpen(img, .3)
blob1 = group(img)
mask = Image.new("RGB", (50, 10), "white")
blob3 = find(img,mask,(3,3))
coords1 = edge(img)
coords2 = center(blob1)
dist = distance(0,3)
Example #40
0
 def _gen_dif(self, idx, ifcrop=True, cropsize=(240,120)):
     A = self.getA(idx)
     B = self.getB(self._rand_not_idx(idx))
     AB = np.concatenate((A,B),axis=1)
     if (ifcrop): return crop(AB, cropsize[0], cropsize[1])
     else: return AB