def process_image(path):
    im = Image.open(path)
    edges = cv2.Canny(np.asarray(im), 100, 200)

    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda (i, x1, y1, x2, y2): (x2 - x1) * (y2 - y1))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        print '%s -> (no text!)' % path
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    text_im = im.crop(crop)
    return text_im
コード例 #2
0
def process_image(path, out_path):

    orig_im = Image.open(path)
    scale, im = downscale_image(orig_im)

    edges = cv2.Canny(np.asarray(im), 100, 200)

    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    _, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                              cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda i_x1_y1_x2_y2: (i_x1_y1_x2_y2[3] - i_x1_y1_x2_y2[
        1]) * (i_x1_y1_x2_y2[4] - i_x1_y1_x2_y2[2]))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        print('%s -> (no text!)' % path)
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    crop = [int(x / scale)
            for x in crop]  # upscale to the original image size.

    draw = ImageDraw.Draw(im)
    c_info = props_for_contours(contours, edges)
    i = 0
    for c in c_info:
        i += 1
        this_crop = c['x1'], c['y1'], c['x2'], c['y2']
        t = orig_im.crop((c['x1'], c['y1'], c['x2'], c['y2']))
        t.show()
        tex = pytesseract.image_to_string(t)
        if tex:
            print("{}:".format(i))
            print(pytesseract.image_to_string(t))
        # t.show()
        draw.rectangle(this_crop, outline='blue')
    draw.rectangle(crop, outline='red')
    im.save(out_path)
    draw.text((50, 50), path, fill='red')
    orig_im.save(out_path)
    im.show()
    text_im = orig_im.crop(crop)
    text_im.save(out_path)
    print('%s -> %s' % (path, out_path))
コード例 #3
0
ファイル: recorte.py プロジェクト: llemonS/matchtemplate
def process_image(path, out_path):
    orig_im = Image.open(path)
    scale, im = downscale_image(orig_im)
    edges = cv2.Canny(np.asarray(im), 100, 200)
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda i_x1_y1_x2_y2: (i_x1_y1_x2_y2[3] - i_x1_y1_x2_y2[1]) * (i_x1_y1_x2_y2[4] - i_x1_y1_x2_y2[2]))
    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)
    edges = 255 * (edges > 0).astype(np.uint8)
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered
    contours = find_components(edges)
    if len(contours) == 0:
        print('%s -> (no text!)' % path)
        return
    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)
    crop = [int(x / scale) for x in crop]  
    text_im = orig_im.crop(crop)
    text_im.save(out_path)
    print('%s -> %s' % (path, out_path))
コード例 #4
0
def reduce_noise_edges(im):
    structuring_element = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
    opening = cv2.morphologyEx(im, cv2.MORPH_OPEN, structuring_element)
    maxed_rows = rank_filter(opening, -4, size=(1, 20))
    maxed_cols = rank_filter(opening, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(opening, maxed_rows), maxed_cols)
    return debordered
コード例 #5
0
def crop_text_from_image(image):
    image = Image.open(image)

    scale, new_image = downscale(image)
    edges = cv2.Canny(np.asarray(new_image), 100, 200)
    ret, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                                cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda i_x1_y1_x2_y2: (i_x1_y1_x2_y2[3] - i_x1_y1_x2_y2[
        1]) * (i_x1_y1_x2_y2[4] - i_x1_y1_x2_y2[2]))
    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        print('%s -> (no text!)' % path)
        return image

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)
    crop = [int(x / scale) for x in crop]

    text_im = image.crop(crop).convert('RGB')
    return text_im
コード例 #6
0
def test_size_footprint_both_set():
    # test for input validation, expect user warning when
    # size and footprint is set
    with suppress_warnings() as sup:
        sup.filter(UserWarning, "ignoring size because footprint is set")
        arr = np.random.random((10, 20, 30))
        rank_filter(arr, 5, size=2, footprint=np.ones((1, 1, 10), dtype=bool))
コード例 #7
0
def process_image(path, out_path):

    orig_im = Image.open(path)
    scale, im = downscale_image(orig_im)

    edges = cv2.Canny(np.asarray(im), 100, 200)

    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda i_x1_y1_x2_y2: (i_x1_y1_x2_y2[3] - i_x1_y1_x2_y2[
        1]) * (i_x1_y1_x2_y2[4] - i_x1_y1_x2_y2[2]))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        print('%s -> (no text!)' % path)
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    crop = [int(x / scale)
            for x in crop]  # upscale to the original image size.

    #draw = ImageDraw.Draw(im)
    #c_info = props_for_contours(contours, edges)
    #for c in c_info:
    #    this_crop = c['x1'], c['y1'], c['x2'], c['y2']
    #    draw.rectangle(this_crop, outline='blue')
    #draw.rectangle(crop, outline='red')
    #im.save(out_path)
    #draw.text((50, 50), path, fill='red')
    #orig_im.save(out_path)
    #im.show()
    text_im = orig_im.crop(crop)
    text_im = text_im.convert('RGB')
    bytesim = cv2.cvtColor(np.asarray(text_im), cv2.COLOR_BGR2GRAY)
    ret, bytesim = cv2.threshold(np.asarray(bytesim), 127, 255,
                                 cv2.THRESH_BINARY)
    if img_estim(bytesim, 127) == 'dark':
        ret, bytesim = cv2.threshold(np.asarray(text_im), 127, 255,
                                     cv2.THRESH_BINARY_INV)
    text_im = Image.fromarray(bytesim)
    text_im.save(out_path)
    print('%s -> %s' % (path, out_path))
コード例 #8
0
def remove_lines(_original_image, image, rank, kernel_size):
    maxed_rows = rank_filter(image,
                             rank,
                             size=(kernel_size[0], kernel_size[1]))
    maxed_cols = rank_filter(image,
                             rank,
                             size=(kernel_size[1], kernel_size[0]))
    return numpy.minimum(numpy.minimum(image, maxed_rows), maxed_cols)
コード例 #9
0
def extract_lattice(img, filter_length=30, rank=1, size_close=5, **kwargs):
    """ Actual lattice extraction """

    maxed_rows = rank_filter(img, -rank, size=(1, filter_length))
    maxed_cols = rank_filter(img, -rank, size=(filter_length, 1))
    filtered = np.maximum(np.maximum(img, maxed_rows), maxed_cols)
    lattice = np.minimum(maxed_rows, maxed_cols)

    return lattice
コード例 #10
0
ファイル: test_plot.py プロジェクト: anvlason/dtm_py
def dtm_rank(data, size):
    rmin = int(size[0] * size[1] * 0.05)
    rmax = int(size[0] * size[1] * 0.95)
    print "flter param:", rmin, rmax, size[0], size[1]
    rX = filters.rank_filter(data, rmin, (size[0], size[1]))
    rY = filters.rank_filter(data, rmin, (size[1], size[0]))
    rX = filters.rank_filter(rX, rmax, (size[0], size[1]))
    rY = filters.rank_filter(rY, rmax, (size[1], size[0]))

    return (rX + rY) / 2.0
コード例 #11
0
ファイル: test_plot.py プロジェクト: anvlason/dtm_py
def dtm_rank(data,size):
    rmin = int(size[0]*size[1]*0.05)
    rmax = int(size[0]*size[1]*0.95)
    print "flter param:", rmin,rmax,size[0],size[1]
    rX = filters.rank_filter(data,rmin,(size[0],size[1]))
    rY = filters.rank_filter(data,rmin,(size[1],size[0]))
    rX = filters.rank_filter(rX,rmax,(size[0],size[1]))
    rY = filters.rank_filter(rY,rmax,(size[1],size[0]))

    return (rX+rY)/2.0
コード例 #12
0
def process_image(path, out_path):
    #read image
    orig_im = Image.open(path)
    #downscale image to less than 2048
    scale, im = downscale_image(orig_im)
    #find gray scale image to find contours in it
    edges = cv2.Canny(np.asarray(im), 100, 200)
    #find contours in image
    contours = cv2.findContours(edges.copy(), cv2.RETR_TREE,
                                cv2.CHAIN_APPROX_SIMPLE)
    contours = imutils.grab_contours(contours)

    #return coordinates of all boundind rectangles of contours with area greater than half the total image's area
    #also returns index of what contour it represents
    borders = find_border_components(contours, edges)
    #sort rectangles found based on their area
    borders.sort(key=lambda i_x1_y1_x2_y2: (i_x1_y1_x2_y2[3] - i_x1_y1_x2_y2[
        1]) * (i_x1_y1_x2_y2[4] - i_x1_y1_x2_y2[2]))
    #variable that will store contour for minimum area contour rectangle out of all found in previous step
    border_contour = None
    if len(borders):
        #find contour with min area bounding rectangle using index returned
        border_contour = contours[borders[0][0]]
        #
        edges = remove_border(border_contour, edges)
    edges = 255 * (edges > 0).astype(np.uint8)
    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered
    contours = find_components(edges.copy())
    if len(contours) == 0:
        print('%s -> (no text!)' % path)
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    crop = [int(x / scale)
            for x in crop]  # upscale to the original image size.

    # draw = ImageDraw.Draw(im)
    # c_info = props_for_contours(contours, edges)
    # for c in c_info:
    #    this_crop = c['x1'], c['y1'], c['x2'], c['y2']
    #    draw.rectangle(this_crop, outline='blue')
    # draw.rectangle(crop, outline='red')
    # im.save(out_path)
    # draw.text((50, 50), path, fill='red')
    # orig_im.save(out_path)
    # im.show()
    text_im = orig_im.crop(crop)
    text_im.save(out_path)
    print('%s -> %s' % (path, out_path))
コード例 #13
0
def process_image(path, out_path):
    orig_im = Image.open(path)
    scale, im = downscale_image(orig_im)

    edges = cv2.Canny(np.asarray(im), 100, 200)
    print("edges")
    print(edges)

    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    print(contours)
    print("edges")
    print(edges)
    borders = find_border_components(contours, edges)
    print("borders")
    print(borders)
    # exit()
    # borders.sort(key=lambda (i, x1, y1, x2, y2): (x2 - x1) * (y2 - y1))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        print('%s -> (no text!)' % path)
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    crop = [int(x / scale) for x in crop]  # upscale to the original image size.
    #draw = ImageDraw.Draw(im)
    #c_info = props_for_contours(contours, edges)
    #for c in c_info:
    #    this_crop = c['x1'], c['y1'], c['x2'], c['y2']
    #    draw.rectangle(this_crop, outline='blue')
    #draw.rectangle(crop, outline='red')
    #im.save(out_path)
    #draw.text((50, 50), path, fill='red')
    #orig_im.save(out_path)
    #im.show()
    text_im = orig_im.crop(crop)
    text_im.save(out_path)
    print('%s -> %s' % (path, out_path))
コード例 #14
0
ファイル: crop.py プロジェクト: alambert14/cavr-mosaic
def process_image(path, out_path_lat, out_path_long):
    orig_im = Image.open(path)
    scale, im = downscale_image(orig_im)

    edges = cv2.Canny(np.asarray(im), 100, 200)

    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda (i, x1, y1, x2, y2): (x2 - x1) * (y2 - y1))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        print '%s -> (no text!)' % path
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    crop = [int(x / scale) for x in crop]  # upscale to the original image size.
    #draw = ImageDraw.Draw(im)
    #c_info = props_for_contours(contours, edges)
    #for c in c_info:
    #    this_crop = c['xx1'], c['y1'], c['x2'], c['y2']
    #    draw.rectangle(this_crop, outline='blue')
    #draw.rectangle(crop, outline='red')
    #im.save(out_path)
    #draw.text((50, 50), path, fill='red')
    #orig_im.save(out_path)
    #im.show()
    text_im = orig_im.crop(crop)
    cropped_im = crop_half(text_im)
    cropped_im = crop_thin(cropped_im)
    cropped_im_lat = crop_lat(cropped_im)
    cropped_im_long = crop_long(cropped_im)
    cropped_im_lat.save(out_path_lat)
    cropped_im_long.save(out_path_long)
    print '%s -> %s' % (path, out_path_lat)
    print '%s -> %s' % (path, out_path_long)
コード例 #15
0
def process_image(path, out_path):
    orig_im = Image.open(path)
    scale, im = downscale_image(orig_im)
    # TODO: it's better to convolve the image with gaussian blur
    # like: cv2.GaussianBlur(gray, (5, 5), 0)
    edges = cv2.Canny(np.asarray(im), 100, 200)

    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    _, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                              cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    print("OK")
    borders.sort(key=lambda i, x1, y1, x2, y2: (x2 - x1) * (y2 - y1))
    print("OK")
    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered.astype(np.int32)
    print(edges.shape)

    contours = find_components(edges)
    if len(contours) == 0:
        print('%s -> (no text!)' % path)
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    crop = [int(x / scale)
            for x in crop]  # upscale to the original image size.
    #show borders
    draw = ImageDraw.Draw(im)
    c_info = props_for_contours(contours, edges)
    for c in c_info:
        this_crop = c['x1'], c['y1'], c['x2'], c['y2']
        draw.rectangle(this_crop, outline='blue')
    draw.rectangle(crop, outline='red')
    im.save(out_path)
    draw.text((50, 50), path, fill='red')
    orig_im.save(out_path)
    im.show()
    text_im = orig_im.crop(crop)
    text_im.save(out_path)
    print('%s -> %s' % (path, out_path))
コード例 #16
0
ファイル: include.py プロジェクト: anvlason/dtm_py
def dtm_rank2(data, size, tr):
    rmin = int(size[0] * size[1] * 0.05)
    rmax = int(size[0] * size[1] * 0.95)
    print "flter param:", rmin, rmax, size[0], size[1]
    rX = filters.rank_filter(data, rmin, (size[0], size[1]))
    rY = filters.rank_filter(data, rmin, (size[1], size[0]))
    rX = filters.rank_filter(rX, rmax, (size[0], size[1]))
    rY = filters.rank_filter(rY, rmax, (size[1], size[0]))
    diff = data - ((rX + rY) / 2.0)
    out = data[:, :]
    mask = diff > tr
    out[mask] = data[mask] - diff[mask]
    return out
コード例 #17
0
ファイル: minmax_flt.py プロジェクト: anvlason/dtm_py
def dtm_rank2i(data,size,tr):
    rmin = int(size[0]*size[1]*0.95)
    rmax = int(size[0]*size[1]*0.05)
    print "invert flter param:", rmin,rmax,size[0],size[1]
    rX = filters.rank_filter(data,rmin,(size[0],size[1]))
    rY = filters.rank_filter(data,rmin,(size[1],size[0]))
    rX = filters.rank_filter(rX,rmax,(size[0],size[1]))
    rY = filters.rank_filter(rY,rmax,(size[1],size[0]))
    diff = data-((rX+rY)/2.0)
    out = np.copy(data)#data[:,:]
    mask = diff>tr
    out[mask]=data[mask]+diff[mask]
    return out
コード例 #18
0
def process_image_without_save(im, scale=1):
    # edges = cv2.Canny(np.asarray(im), 100, 200)
    im = cv2.bilateralFilter(im, 9, 25, 175)
    edges = cv2.Canny(im, 100, 200)
    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    _, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                              cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda (i, x1, y1, x2, y2): (x2 - x1) * (y2 - y1))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        # print '%s -> (no text!)' % path
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    if scale != 1:
        crop = [int(x / scale)
                for x in crop]  # upscale to the original image size.
    # draw = ImageDraw.Draw(im)
    # c_info = props_for_contours(contours, edges)
    # for c in c_info:
    #    this_crop = c['x1'], c['y1'], c['x2'], c['y2']
    #    draw.rectangle(this_crop, outline='blue')
    # draw.rectangle(crop, outline='red')
    # im.save(out_path)
    # draw.text((50, 50), path, fill='red')
    # orig_im.save(out_path)
    # im.show()
    # text_im = im.crop(crop)
    # Crop from x, y, w, h -> 100, 200, 300, 400
    # NOTE: its img[y: y + h, x: x + w] and *not* img[x: x + w, y: y + h]
    # text_im = im[crop[1]: crop[3], crop[0]: crop[2]]
    # return text_im
    return crop
コード例 #19
0
def process_crop_image(img):
    orig_im = Image.fromarray(img)
    scale, im = downscale_image(orig_im)

    edges = cv2.Canny(np.asarray(im), 100, 200)

    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda i_x1_y1_x2_y2: (i_x1_y1_x2_y2[3] - i_x1_y1_x2_y2[
        1]) * (i_x1_y1_x2_y2[4] - i_x1_y1_x2_y2[2]))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        return img

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    crop = [int(x / scale)
            for x in crop]  # upscale to the original image size.

    #draw = ImageDraw.Draw(im)
    #c_info = props_for_contours(contours, edges)
    #for c in c_info:
    #    this_crop = c['x1'], c['y1'], c['x2'], c['y2']
    #    draw.rectangle(this_crop, outline='blue')
    #draw.rectangle(crop, outline='red')
    #im.save(out_path)
    #draw.text((50, 50), path, fill='red')
    #orig_im.save(out_path)
    #im.show()

    text_im = orig_im.crop(crop)
    output = np.array(text_im)
    return output
コード例 #20
0
 def process_image(img):
     orig_im = img
     
     scale, im = downscale_image(orig_im)
     
     edges = cv2.Canny(np.asarray(im), 100, 200)        
     
     #print edges
     
     _, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
     borders = find_border_components(contours, edges)
     borders.sort(key=lambda (i, x1, y1, x2, y2): (x2-x1)*(y2-y1))        
     border_contour = None
     
     if len(borders):
         borders_contour = contours[borders[0][0]]
         edges = remove_border(borders_contour, edges)
     
     edges = 255 * (edges > 0).astype(np.uint8)
     
     maxed_rows = rank_filter(edges, -4, size=(1, 20))
     maxed_cols = rank_filter(edges, -4, size=(20, 1))
     debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
     edges = debordered
     
     contours = find_components(edges)
     if len(contours) == 0:
         print "No Text!"
         return
     
     crop = find_optimal_components_subset(contours, edges)
     crop = pad_crop(crop, contours, edges, border_contour)
     
     crop = [int(x / scale) for x in crop]
     
     text_im = orig_im.crop(crop)
     text_im.save("cropped.png")
    # text_im.ConvertToMono(255, 255, 255)
     text = image_to_string(text_im)
     if args.t:
         fo = open(args.t, "a")
         fo.write(text + "\nEnd of Slide\n\n")
         fo.close()
     else:
         fo = open("notes.txt", "a")
         fo.write(text + "\nEnd of Slide\n\n");
         fo.close()
     
     print "\nI think your notes say:\n\n"
     print(text)
コード例 #21
0
def process_image(path, out_path):
    orig_im = Image.open(path)
    scale, im = downscale_image(orig_im)

    edges = cv2.Canny(np.asarray(im), 100, 200)

    # Dilating image before finding a border.
    major = cv2.__version__.split('.')[0]
    if major == '3':
        ret, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                                    cv2.CHAIN_APPROX_SIMPLE)
    else:
        contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                               cv2.CHAIN_APPROX_SIMPLE)

    borders = find_border_components(contours, edges)

    borders.sort(key=lambda i_x1_y1_x2_y2: (i_x1_y1_x2_y2[3] - i_x1_y1_x2_y2[
        1]) * (i_x1_y1_x2_y2[4] - i_x1_y1_x2_y2[2]))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_b(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove  borders using a rank filter.

    max_rows = rank_filter(edges, -4, size=(1, 20))
    max_cols = rank_filter(edges, -4, size=(20, 1))
    deborder = np.minimum(np.minimum(edges, max_rows), max_cols)

    edges = deborder
    contours = find_comp(edges)

    if len(contours) == 0:
        print('%s -> (no text!)' % path)
        return

    crop = optimal_comp(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    # upscale to the original image size.
    crop = [int(x / scale) for x in crop]

    text_im = orig_im.crop(crop)
    text_im.save(out_path)
    print('%s -> %s' % (path, out_path))
コード例 #22
0
ファイル: gdal_read.py プロジェクト: anvlason/dtm_py
def dtm_rank2(data,size,tr):
    datat = np.copy(data)
    data = None
    rmin = int(size[0]*size[1]*0.05)
    rmax = int(size[0]*size[1]*0.95)
#    print "flter param:", rmin,rmax,size[0],size[1]
    rX = filters.rank_filter(datat,rmin,(size[0],size[1]))
    rY = filters.rank_filter(datat,rmin,(size[1],size[0]))
    rX = filters.rank_filter(rX,rmax,(size[0],size[1]))
    rY = filters.rank_filter(rY,rmax,(size[1],size[0]))
    diff = datat-((rX+rY)/2.0)
    out = datat[:,:]
    mask = diff>tr
    out[mask]=datat[mask]-diff[mask]
    return out
コード例 #23
0
ファイル: cerena_grid_utils.py プロジェクト: Taffsigg/GEOMS2
def order_statistics_filter(grid,window_size=(3,3,3),statistics_type='median',rank=1):
    filtered = grid.copy()
    if statistics_type=='minimum':
        scifilt.minimum_filter(grid,window_size,None,filtered, mode='nearest')
    elif statistics_type=='maximum':
        scifilt.maximum_filter(grid,window_size,None,filtered, mode='nearest')
    elif statistics_type=='median':
        scifilt.median_filter(grid,window_size,None,filtered, mode='nearest')
    elif statistics_type[:-2]=='percentile' or statistics_type[:-2]=='per':
        per = np.int(statistics_type[-2:])
        scifilt.percentile_filter(grid,per,window_size,None,filtered, mode='nearest')
    elif statistics_type=='rank':
        scifilt.rank_filter(grid,rank,window_size,None,filtered, mode='nearest')
    return filtered
        
    return filtered
コード例 #24
0
ファイル: test_filters.py プロジェクト: purusharths/NumCpp
def test_rankFilter1d():
    for mode in modes.keys():
        size = np.random.randint(1000, 2000, [
            1,
        ]).item()
        cShape = NumCpp.Shape(1, size)
        cArray = NumCpp.NdArray(cShape)
        data = np.random.randint(100, 1000, [
            size,
        ]).astype(float)
        cArray.setArray(data)
        kernalSize = 0
        while kernalSize % 2 == 0:
            kernalSize = np.random.randint(5, 15)
        rank = np.random.randint(0, kernalSize - 1, [
            1,
        ]).item()
        constantValue = np.random.randint(0, 5, [
            1,
        ]).item()  # only actaully needed for constant boundary condition
        dataOutC = NumCpp.rankFilter1d(
            cArray, kernalSize, rank, modes[mode],
            constantValue).getNumpyArray().flatten()
        dataOutPy = filters.rank_filter(data,
                                        rank,
                                        footprint=np.ones([
                                            kernalSize,
                                        ]),
                                        mode=mode,
                                        cval=constantValue)
        assert np.array_equal(dataOutC, dataOutPy)
コード例 #25
0
def do_cfar(amp, quantile=0.65):
    # threshold = 1.3 * rank_filter(amp, rank=int(np.ceil(1 * 32 * quantile)), size=(32, 1))
    # 200 x 17
    footprint = [
        [0, 1, 0],
        [0, 1, 0],
        [0, 1, 0],
        [0, 1, 0],
        [0, 1, 0],
        [0, 1, 0],
        [0, 1, 0],
        [0, 1, 0],
        [1, 1, 1],
        [0, 1, 0],
        [0, 1, 0],
        [0, 1, 0],
        [0, 1, 0],
        [0, 1, 0],
        [0, 1, 0],
        [0, 1, 0],
        [0, 1, 0],
    ]

    filter_length = 10

    for k in range(3):
        threshold = 0.9 * rank_filter(amp[k, ...], rank=int(np.ceil(1 * filter_length * quantile)),
                                      size=(filter_length, 1))
        # threshold = 1.3*rank_filter(amp[k, ...], rank=int(np.count_nonzero(footprint)*quantile), footprint=footprint)
        # threshold = rank_filter(peaks, rank=-1, size=(3, 5))
        amp[k, ...] = amp[k, ...] * (amp[k, ...] > threshold)
    return amp
コード例 #26
0
ファイル: test_filters.py プロジェクト: purusharths/NumCpp
def test_rankFilter():
    for mode in modes.keys():
        shape = np.random.randint(100, 200, [
            2,
        ]).tolist()
        cShape = NumCpp.Shape(shape[0], shape[1])  # noqa
        cArray = NumCpp.NdArray(cShape)
        data = np.random.randint(100, 1000, shape)  # noqa
        cArray.setArray(data)
        kernalSize = 0
        while kernalSize % 2 == 0:
            kernalSize = np.random.randint(5, 15)
        rank = np.random.randint(0, kernalSize**2 - 1, [
            1,
        ]).item()
        constantValue = np.random.randint(0, 5, [
            1,
        ]).item()  # only actaully needed for constant boundary condition
        dataOutC = NumCpp.rankFilter(cArray, kernalSize, rank, modes[mode],
                                     constantValue).getNumpyArray()
        dataOutPy = filters.rank_filter(data,
                                        rank,
                                        size=kernalSize,
                                        mode=mode,
                                        cval=constantValue)
        assert np.array_equal(dataOutC, dataOutPy)
コード例 #27
0
def process_image(img):
    orig_im = img
    #orig_im = cv2.cvtColor(np.asarray(orig_im, dtype=np.uint8), cv2.COLOR_BGR2GRAY)
    # orig_im = cv2.fromarray(orig_im)
    scale, im = downscale_image(PIL.Image.fromarray(orig_im))

    edges = cv2.Canny(np.asarray(im), 100, 200)

    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    _, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda b: (b[3] - b[1]) * (b[4] - b[2]))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    crop = [int(x / scale) for x in crop]  # upscale to the original image size.
    draw = ImageDraw.Draw(im)
    c_info = props_for_contours(contours, edges)
    for c in c_info:
       this_crop = c['x1'], c['y1'], c['x2'], c['y2']
       draw.rectangle(this_crop, outline='blue')
    draw.rectangle(crop, outline='red')
    #im.save(out_path)
    #draw.text((50, 50), path, fill='red')
    #orig_im.save(out_path)
    #im.show()

    text_im = PIL.Image.fromarray(orig_im).crop(crop)
    return text_im
コード例 #28
0
ファイル: pre_img_pan.py プロジェクト: rohitdhore/ocr
def preprocess_image(path):

    # orig_im = Image.open(path)
    orig_im = path
    scale, im = downscale_image(orig_im)

    edges = cv2.Canny(np.asarray(im), 100, 200)

    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda i_x1_y1_x2_y2: (i_x1_y1_x2_y2[3] - i_x1_y1_x2_y2[
        1]) * (i_x1_y1_x2_y2[4] - i_x1_y1_x2_y2[2]))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        print('%s -> (no text!)' % path)
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    crop = [int(x / scale)
            for x in crop]  # upscale to the original image size.
    #Start
    draw = ImageDraw.Draw(im)
    c_info = props_for_contours(contours, edges)
    for c in c_info:
        this_crop = c['x1'], c['y1'], c['x2'], c['y2']

    text_im = orig_im.crop(crop)
    # text_im.show()
    return text_im
コード例 #29
0
def dtm_v3(data, rank, shape, tr):
    tdata = np.copy(data)
    data = None
    ranked = filters.rank_filter(tdata, rank, shape)
    diff = tdata - ranked
    tdata[diff > tr] = np.nan
    lerp = filters.gaussian_filter(interp(tdata), 1.2)
    return lerp
コード例 #30
0
ファイル: gdal_read.py プロジェクト: anvlason/dtm_py
def dtm_v3(data,rank,shape,tr):
    tdata = np.copy(data)
    data=None
    ranked = filters.rank_filter(tdata,rank,shape)
    diff = tdata - ranked
    tdata[diff>tr]=np.nan
    lerp = filters.gaussian_filter(interp(tdata),1.2)
    return lerp
コード例 #31
0
def process_image(path, out_path):
    orig_im = Image.open(path)
    im = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    scale, im = downscale_image(im)

    edges = cv2.Canny(im, 100, 200)

    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    _, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda i_x1_y1_x2_y2: (i_x1_y1_x2_y2[3] - i_x1_y1_x2_y2[1]) * (i_x1_y1_x2_y2[4] - i_x1_y1_x2_y2[2]))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -5, size=(1, 20))
    maxed_cols = rank_filter(edges, -5, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        print('%s -> (no text!)' % path)
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    crop = [int(x / scale) for x in crop]  # upscale to the original image size.

    # draw and show cropped rectangle area in the original image
    rgb_im = orig_im.convert('RGB')
    draw = ImageDraw.Draw(rgb_im)
    draw.rectangle(crop, outline='red')
    rgb_im.show()

    text_im = orig_im.crop(crop)
    text_im.show()
    text_im.save(out_path)
    print('%s -> %s' % (path, out_path))
コード例 #32
0
def process_image(path, out_path):
    orig_im = Image.open(path)
    scale, im = downscale_image(orig_im)

    edges = cv2.Canny(np.asarray(im), 100, 200)

    image1, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                                   cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda (i, x1, y1, x2, y2): (x2 - x1) * (y2 - y1))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)
    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered
    contours, img3 = find_components(edges)
    if len(contours) == 0:
        print '%s -> (no text!)' % path
        return

    height, width = img3.shape
    edges2 = cv2.Canny(img3, 50, 150, apertureSize=3)
    Image.fromarray(edges2).show()
    lines2 = cv2.HoughLinesP(edges2,
                             1,
                             np.pi / 180,
                             100,
                             minLineLength=width / 10.0,
                             maxLineGap=20)
    angle = 0.0
    for linp in lines2[0]:
        angle += np.arctan2(linp[2] - linp[0], linp[3] - linp[1])
        # cv2.imwrite('images/autorotate/houghlines.jpg', img3)

    imgFinal = cv2.imread(path, 0)
    deskewed_image = ndimage.rotate(imgFinal, angle / len(lines2[0]))
    cv2.imwrite(out_path, deskewed_image)
コード例 #33
0
def crop_image(image):
    '''Crop the IMAGE, and return the croped version.'''

    orig_im = Image.fromarray(image)
    scale, im = downscale_image(orig_im)

    edges = cv2.Canny(np.asarray(im), 100, 200)
    debugger.save_img(edges, 'edges')
    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    temp, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                                 cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    debugger.display('crop_image: borders: ', borders)
    try:
        borders.sort(
            key=lambda area: (area[3] - area[1]) * (area[4] - area[2]))
    except TypeError as e:
        print(e)
        sys.exit('Cannot crop the image, exit now.')

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = 255 * (debordered > 0).astype(np.uint8)

    contours = find_components(edges)

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    # upscale to the original image size.
    crop = [int(x / scale) for x in crop]
    text_im = np.array(orig_im.crop(crop))

    return text_im
コード例 #34
0
def Page_Crop(path, out_path):
    orig_im = Image.open(path)
    orig_im = rotation_fixing(orig_im)
    plt.imshow(orig_im)
    plt.show()
    scale, im = downscale_image(orig_im)
    edges = cv2.Canny(np.asarray(im), 100, 200)
    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    Y = borders
    borders = sorted(
        borders, key=lambda Y: (Y[3] - Y[1]) * (Y[4] - Y[2])
    )  # #{i, x1, y1, x2, y2} (x2 - x1) * (y2 - y1))  (i, x1, y1, x2, y2) : (x2 - x1) * (y2 - y1)

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)
    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        print('%s -> (no text!)' % path)
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)
    crop = [int(x / scale)
            for x in crop]  # upscale to the original image size.

    text_im = orig_im.crop(crop)
    #text_im.save(out_path)
    plt.imshow(text_im)
    plt.show()
    print('Saved %s -> %s' % (path, out_path))
コード例 #35
0
def process_image2(path, out_path):
    orig_im = Image.open(path)
    scale, im = downscale_image(orig_im)

    edges = cv2.Canny(np.asarray(im), 100, 200)

    image1, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                                   cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda (i, x1, y1, x2, y2): (x2 - x1) * (y2 - y1))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)
    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered
    contours, img3 = find_components(edges)
    if len(contours) == 0:
        print '%s -> (no text!)' % path
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    crop = [int(x / scale)
            for x in crop]  # upscale to the original image size.
    draw = ImageDraw.Draw(im)
    c_info = props_for_contours(contours, edges)
    for c in c_info:
        this_crop = c['x1'], c['y1'], c['x2'], c['y2']
        draw.rectangle(this_crop, outline='blue')
    draw.rectangle(crop, outline='red')
    im.show()
    text_im = orig_im.crop(crop)
    text_im.save(out_path)
コード例 #36
0
def process_image(frame):
    orig_im = frame.copy()
    scale, im = downscale_image(orig_im)

    edges = cv2.Canny(np.asarray(im), 100, 200)

    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    _, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(
        key=lambda i_x1_y1_x2_y2: (i_x1_y1_x2_y2[3] - i_x1_y1_x2_y2[1]) * (i_x1_y1_x2_y2[4] - i_x1_y1_x2_y2[2]))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    crop = [int(x / scale) for x in crop]  # upscale to the original image size.
    print(crop)

    #cuts and shows the selected text area
    text_im = orig_im[crop[1]:(crop[1]+crop[3]),crop[0]:(crop[0]+crop[2])]
    cv2.imshow('Corte',text_im)
    text_string = text_recog(text_im)
    speech(text_string)
コード例 #37
0
ファイル: crop_morphology.py プロジェクト: Jmuccigr/scripts
def process_image(path, out_path):
    print ('Starting...')
    orig_im = cv2.imread(path)


    im = cv2.cvtColor(orig_im, cv2.COLOR_BGR2GRAY)
    scale, gray = downscale_image(im)
    params = auto_canny (gray)
    edges = cv2.Canny(np.asarray(gray), params[0], params[1])

#   TODO: dilate image _before_ finding a border. This is crazy sensitive!
    contours,hierachy=cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda i_x1_y1_x2_y2: (i_x1_y1_x2_y2[3] - i_x1_y1_x2_y2[1]) * (i_x1_y1_x2_y2[4] - i_x1_y1_x2_y2[2]))
    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

#     Remove ~1px borders using a rank filter.
#     Leave horizontal lines - JM
#     maxed_cols = rank_filter(edges, -4, size=(20, 1))
#     debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    debordered = np.minimum(edges, maxed_rows)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        print('%s -> (no text!)' % path)
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)
    crop = [int(x / scale) for x in crop]  # upscale to the original image size.

    #draw = ImageDraw.Draw(im)
    #c_info = props_for_contours(contours, edges)
    #for c in c_info:
    #    this_crop = c['x1'], c['y1'], c['x2'], c['y2']
    #    draw.rectangle(this_crop, outline='blue')
    #draw.rectangle(crop, outline='red')
    #im.save(out_path)
    #draw.text((50, 50), path, fill='red')
    #orig_im.save(out_path)
    #im.show()
    img = Image.open(path)
    text_im = img.crop(crop)
    text_im.save(out_path)
    print('%s -> %s' % (path, out_path))
コード例 #38
0
ファイル: gdal_read.py プロジェクト: anvlason/dtm_py
def dtm_krauss_2015_rank(dsm,ps,minf_r,t_ps):
    nx,ny = dsm.shape
    scale = t_ps/ps
    nnx = int(nx/scale+0.5)
    nny = int(ny/scale+0.5)
    scale_int = int(scale+0.5)
    print scale
    print scale_int
    rmin = int(minf_r*minf_r*0.05)
#    minf = filters.minimum_filter(dsm,minf_r)
    minf = filters.rank_filter(dsm,rmin,(minf_r,minf_r))
    dwn = cv2.resize(minf,(nny,nnx),interpolation = cv2.INTER_NEAREST)
#    dwn = scipy.misc.imresize(minf,(nnx,nny),interp='cubic')
    dwn_o = ndimage.grey_opening(dwn,5)
    dwn_g = filters.gaussian_filter(dwn_o,2.5)
#    return scipy.misc.imresize(dwn_g,(nx,ny),interp='cubic')
    return cv2.resize(dwn_g,(ny,nx),interpolation = cv2.INTER_CUBIC)
コード例 #39
0
ファイル: mask.py プロジェクト: CJ-Wright/diffpy.srxplanar
 def brightPixelMask(self, pic, size=5, r=1.2):
     '''
     pixels with much higher intensity compare to adjacent pixels will be masked,
     this mask is used when there are some bright spots/pixels whose intensity is higher 
     than its neighbors but not too high. Only use this on a very good powder averaged 
     data. Otherwise it may mask wrong pixels. 
     
     This mask has similar functions as 'selfcorr' function. However, this mask will only 
     consider pixels' local neighbors pixels and tend to mask more pixels. While 'selfcorr' 
     function compare one pixel to other pixels in same bin.
     
     :param pic: 2d array, image array to be processed
     :param size: int, size of local area to test if a pixel is a bright pixel
     :param r: float, a threshold for masked pixels   
     
     :return: 2d array of boolean, 1 stands for masked pixel
     '''
     rank = snf.rank_filter(pic, -size, size)
     ind = snm.binary_dilation(pic > rank * r, np.ones((3, 3)))
     return ind
コード例 #40
0
ファイル: modis_waterfrac.py プロジェクト: openstreams/SGR
def detwfrac(swir21):
    """

    :param swir21:
    :return:
    """
    water_ref=0.008 # assumed
    kernel=7
    mbuffer=(kernel-1)/2
    row = swir21.shape[0]
    col = swir21.shape[1]
    target=np.zeros((row+2*mbuffer,col+2*mbuffer))
    row = target.shape[0]
    col = target.shape[1]
    target[mbuffer-1:row-1-mbuffer,mbuffer-1:col-1-mbuffer] = swir21
    perc=round(0.95*(kernel)**2.0) - 2 # finds perc value
    dry_ref = filters.rank_filter(np.maximum(0,target),int(perc),footprint=np.ones((kernel,kernel)))
    watf = np.minimum(1.0,np.maximum(0.0, (target - dry_ref)/(water_ref - dry_ref)))
    watf[target < 0.01] = 1.0
    waterfrac = watf[mbuffer-1:row-1-mbuffer,mbuffer-1:col-1-mbuffer]

    return waterfrac
コード例 #41
0
ファイル: utils.py プロジェクト: tjlane/miniana
def preprocess_image(raw_image, threshold=0.0025, sigma=1.0, minf_size=1,
                     rank_size=1, sobel=True):
    """
    Applies an edge filter followed by a noise reduction filter. Very good
    at locating powder rings and filtering everything else out.
    
    Parameters
    ----------
    raw_image : ndarray
        An image to find the edges of
        
    Returns
    -------
    binary_image : ndarray, np.bool
        A binary image, with "1" where there are powder rings/strong edges
    """
    
    # flatten the image into a two-D array and later re-process it
    # convert to cheetah-like format
    if raw_image.shape == (4,16,185,194):
        non_flat_img = True
        image = np.zeros((1480, 1552), dtype=np.float) # flat image
        for i in range(8):
            for j in range(4):
                x_start = 185 * i
                x_stop  = 185 * (i+1)
                y_start = 388 * j
                y_stop  = 388 * (j+1)
                
                two_by_one = np.hstack(( raw_image[j,i*2,:,:], raw_image[j,i*2+1,:,:])).astype(np.float)
                image[x_start:x_stop,y_start:y_stop] = two_by_one
                
    elif len(raw_image.shape) == 2:
        non_flat_img = False
        image = raw_image.astype(np.float)
        
    else:
        raise ValueError('`raw_image` should be 2d or shape-(4,16,185,194), got'
                         ': %s' % str(raw_image.shape))
    
    # apply rank filter & gaussian filter
    if rank_size > 2:
        image = filters.rank_filter(image, -1, size=rank_size)
    if sigma > 0.1:
        image = filters.gaussian_filter(image, sigma=sigma)
    
    image -= image.min()
    assert image.min() == 0
    assert image.max() > 0
    
    # threshold
    image = (image > (image.max() * threshold))
    
    if minf_size > 2:
        image = filters.minimum_filter(image, size=minf_size)
    if sobel:
        image = np.abs(filters.sobel(image, 0)) + np.abs(filters.sobel(image, 1))    
    
    if non_flat_img:
        image = read.enforce_raw_img_shape( image.astype(np.bool) )
    else:
        image = image.astype(np.bool)
        
    return image
コード例 #42
0
ファイル: max_flt_gdal.py プロジェクト: anvlason/dtm_py
            (array2d <= np.roll(array2d, -1, 0)) &
            (array2d <= np.roll(array2d,  1, 1)) &
            (array2d <= np.roll(array2d, -1, 1)))

data_scaled=data#[0::5,0::5]
#data_min = data_scaled
#mask_min = local_minima(data_scaled)
#data_scaled[~mask_min] = np.NAN

#remove small holes
maxit = 3
for it in range(0,maxit):
    data_open = ndimage.grey_closing(data_scaled,size=(3,3))
print "small holes filled"
#data_open = ndimage.grey_opening(data_scaled,size=(3,3))
data_open = filters.rank_filter(data_open,3,3)
#data_open = filters.gaussian_filter(data_open,sigma=5/2.0)
#data_open[np.isnan(data_open)]=-9999

#zfy = float(data.shape[0])/data_open.shape[0]
#zfx = float(data.shape[1])/data_open.shape[1]

#data_out = scipy.misc.imresize(data_open,(data.shape[0],data.shape[1]),interp='bilinear').astype(np.float_)
#data_out=ndimage.interpolation.zoom(data_open,(zfy,zfx))

#local_min v2
#set bad_matches to nan
#bad_matches=energy>0.7
#data[bad_matches]=np.NAN
#resample 5x down
data_dwn=np.zeros(data.shape)
コード例 #43
0
ファイル: canny.py プロジェクト: loveheaven/ocropy
def processImage(blur, lowthreshold=200, highThreshold=450, tag=""):
    edges = cv2.Canny(blur, lowthreshold, highThreshold)
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(edges, contours, -1, (255, 255, 255), -1)
    cv2.imwrite("edges%s.png" % tag, edges)
    #    (_, img) = cv2.threshold(edges, 110, 255, cv2.THRESH_BINARY)
    #    cv2.imwrite('edges3%s.png' % tag,img)
    #    scale = psegutils.estimate_scale(img)
    #    binary = remove_hlines(img, blur, scale)
    #    cv2.imwrite('edges4%s.png' % tag,binary*255)
    #    binary = remove_vlines(binary, blur, scale)
    #    edges=binary*255
    #    cv2.imwrite('edges2%s.png' % tag,edges)

    #    minLineLength = 70
    #    maxLineGap = 50
    #    height, width = edges.shape
    #    theta = 0
    #    lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
    #    if lines is not None:
    #        for x1,y1,x2,y2 in lines[0]:
    #            if x2 == x1:
    #                cv2.line(edges,(x1,0),(x1,height),(0,0,0),2)
    #                theta = 0
    #            else:
    #                tanTheta = float((y2-y1))/(x2-x1)
    #                tmp = np.arctan2(y2-y1, x2-x1) * 180/np.pi
    #                if tmp != 0.0 and tmp != 90.0:
    #                    theta=math.fabs(tmp)
    #                else:
    #                    theta = 0
    #                print theta, y2-y1, x2-x1
    #                ystart=int((0-x1)*tanTheta+y1)
    #                yend = int((width-x1)*tanTheta + y1)
    #                cv2.line(edges,(0,ystart),(width,yend),(0,0,0),2)
    #        cv2.imwrite('houghlines5%s.png' % tag,edges)
    #        if theta > 85 and theta < 95:
    #            theta = 90 - theta
    #        elif theta < 5:
    #            theta = -theta
    #        elif theta > 175:
    #            theta = 180 - theta
    #        print tag,theta
    #        if theta != 0:
    #            blur = rotate(blur, theta)
    #            cv2.imwrite('rotate%s.png' % tag,blur)
    #
    #            edges = cv2.Canny(blur,200,450)
    #            contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    #            cv2.drawContours(edges,contours,-1,(255,255,255),1)
    #            cv2.imwrite('edges%s.png' % tag,edges)
    #
    #            minLineLength = 70
    #            maxLineGap = 50
    #            height, width = edges.shape
    #            theta = 0
    #            lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
    #            for x1,y1,x2,y2 in lines[0]:
    #                if x2 == x1:
    #                    cv2.line(edges,(x1,0),(x1,height),(0,0,0),2)
    #                else:
    #                    tanTheta = float((y2-y1))/(x2-x1)
    #                    theta = np.arctan2(y2-y1, x2-x1) * 180/np.pi
    #                    ystart=int((0-x1)*tanTheta+y1)
    #                    yend = int((width-x1)*tanTheta + y1)
    #                    cv2.line(edges,(0,ystart),(width,yend),(0,0,0),2)
    #            cv2.imwrite('houghlines5%s.png' % tag,edges)

    #    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 1))
    #    edges = cv2.erode(edges, kernel, iterations = 1)
    #    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 3))
    #    edges = cv2.erode(edges, kernel, iterations = 1)

    maxed_rows = rank_filter(edges, -4, size=(1, 30))  # vertical
    maxed_cols = rank_filter(edges, -4, size=(30, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)  #
    edges = debordered
    cv2.imwrite("edges1%s.png" % tag, edges)
    return edges
コード例 #44
0
ファイル: cerena_grid_utils.py プロジェクト: armatita/GEOMS2
def moving_window_atribute(data,atribute='mean',window=(3,3,3),percentile=50,rank=1,clip_limits=(0,1),fisher=True,border_mode='nearest'):
    #reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’
    # minimum,maximum,median,percentile,rank,mean,variance,std,clip,sum,product,peak2peak,signal2noise,skewness
    # kurtosis
    if atribute == 'minimum':
        atrib = data.copy()
        scifilt.minimum_filter(data,window,None,atrib, mode=border_mode)
        return atrib
    elif atribute == 'maximum':
        atrib = data.copy()
        scifilt.maximum_filter(data,window,None,atrib, mode=border_mode)
        return atrib
    elif atribute == 'median':
        atrib = data.copy()
        scifilt.median_filter(data,window,None,atrib, mode=border_mode)
        return atrib
    elif atribute == 'percentile':
        atrib = data.copy()
        scifilt.percentile_filter(data,percentile,window,None,atrib, mode=border_mode)
        return atrib
    elif atribute == 'rank':
        atrib = data.copy()
        scifilt.rank_filter(data,rank,window,None,atrib, mode=border_mode)
        return atrib
    elif atribute == 'mean':
        atrib = data.copy()
        blocks = atrib.shape
        for i in xrange(blocks[0]):
            for j in xrange(blocks[1]):
                for k in xrange(blocks[2]):
                    atrib[i,j,k] = data[np.clip(i-window[0],0,blocks[0]):i+window[0]+1,np.clip(j-window[1],0,blocks[1]):j+window[1]+1,np.clip(k-window[2],0,blocks[2]):k+window[2]+1].mean()
        return atrib
    elif atribute == 'variance':
        atrib = data.copy()
        blocks = atrib.shape
        for i in xrange(blocks[0]):
            for j in xrange(blocks[1]):
                for k in xrange(blocks[2]):
                    atrib[i,j,k] = data[np.clip(i-window[0],0,blocks[0]):i+window[0]+1,np.clip(j-window[1],0,blocks[1]):j+window[1]+1,np.clip(k-window[2],0,blocks[2]):k+window[2]+1].var()
        return atrib
    elif atribute == 'std':
        atrib = data.copy()
        blocks = atrib.shape
        for i in xrange(blocks[0]):
            for j in xrange(blocks[1]):
                for k in xrange(blocks[2]):
                    atrib[i,j,k] = data[np.clip(i-window[0],0,blocks[0]):i+window[0]+1,np.clip(j-window[1],0,blocks[1]):j+window[1]+1,np.clip(k-window[2],0,blocks[2]):k+window[2]+1].std()
        return atrib
    elif atribute == 'clip':
        atrib = data.copy()
        blocks = atrib.shape
        for i in xrange(blocks[0]):
            for j in xrange(blocks[1]):
                for k in xrange(blocks[2]):
                    m = data[np.clip(i-window[0],0,blocks[0]):i+window[0]+1,np.clip(j-window[1],0,blocks[1]):j+window[1]+1,np.clip(k-window[2],0,blocks[2]):k+window[2]+1].flatten()
                    l0 = np.percentile(m,clip_limits[0])
                    l1 = np.percentile(m,clip_limits[1])
                    m.clip(l0,l1)
                    atrib[i,j,k] = np.percentile(m,50)
        return atrib
    elif atribute == 'sum':
        atrib = data.copy()
        blocks = atrib.shape
        for i in xrange(blocks[0]):
            for j in xrange(blocks[1]):
                for k in xrange(blocks[2]):
                    atrib[i,j,k] = data[np.clip(i-window[0],0,blocks[0]):i+window[0]+1,np.clip(j-window[1],0,blocks[1]):j+window[1]+1,np.clip(k-window[2],0,blocks[2]):k+window[2]+1].sum()
        return atrib
    elif atribute == 'product':
        atrib = data.copy()
        blocks = atrib.shape
        for i in xrange(blocks[0]):
            for j in xrange(blocks[1]):
                for k in xrange(blocks[2]):
                    atrib[i,j,k] = data[np.clip(i-window[0],0,blocks[0]):i+window[0]+1,np.clip(j-window[1],0,blocks[1]):j+window[1]+1,np.clip(k-window[2],0,blocks[2]):k+window[2]+1].prod()
        return atrib
    elif atribute == 'peak2peak':
        atrib = data.copy()
        blocks = atrib.shape
        for i in xrange(blocks[0]):
            for j in xrange(blocks[1]):
                for k in xrange(blocks[2]):
                    atrib[i,j,k] = data[np.clip(i-window[0],0,blocks[0]):i+window[0]+1,np.clip(j-window[1],0,blocks[1]):j+window[1]+1,np.clip(k-window[2],0,blocks[2]):k+window[2]+1].ptp()
        return atrib
    elif atribute == 'signal2noise':
        atrib = data.copy()
        blocks = atrib.shape
        for i in xrange(blocks[0]):
            for j in xrange(blocks[1]):
                for k in xrange(blocks[2]):
                    m = data[np.clip(i-window[0],0,blocks[0]):i+window[0]+1,np.clip(j-window[1],0,blocks[1]):j+window[1]+1,np.clip(k-window[2],0,blocks[2]):k+window[2]+1].mean()
                    v = data[np.clip(i-window[0],0,blocks[0]):i+window[0]+1,np.clip(j-window[1],0,blocks[1]):j+window[1]+1,np.clip(k-window[2],0,blocks[2]):k+window[2]+1].std()
                    atrib[i,j,k] = m/v
        return atrib
    elif atribute == 'skewness':
        atrib = data.copy()
        blocks = atrib.shape
        for i in xrange(blocks[0]):
            for j in xrange(blocks[1]):
                for k in xrange(blocks[2]):
                    m = data[np.clip(i-window[0],0,blocks[0]):i+window[0]+1,np.clip(j-window[1],0,blocks[1]):j+window[1]+1,np.clip(k-window[2],0,blocks[2]):k+window[2]+1].flatten()
                    v = st.skew(m)
                    atrib[i,j,k] = v
        return atrib
    elif atribute == 'kurtosis':
        atrib = data.copy()
        blocks = atrib.shape
        for i in xrange(blocks[0]):
            for j in xrange(blocks[1]):
                for k in xrange(blocks[2]):
                    m = data[np.clip(i-window[0],0,blocks[0]):i+window[0]+1,np.clip(j-window[1],0,blocks[1]):j+window[1]+1,np.clip(k-window[2],0,blocks[2]):k+window[2]+1].flatten()
                    v = st.kurtosis(m,fisher=fisher)
                    atrib[i,j,k] = v
        return atrib
    else:
        return False
コード例 #45
0
ファイル: test_PerkoAtAll.py プロジェクト: anvlason/dtm_py
gauss256 = data-mean_convoluted(data,86,86)#filters.gaussian_filter(lm,2.5,truncate=2) #(w - 1)/2 = int(truncate*sigma + 0.5)#trunc=sigma/((w-1)/2)+0.5
merge = (gauss8+gauss16+gauss32+gauss64+gauss128+gauss256)/6.0
"""
#maskk=out-packet
#out[maskk>2]=packet[maskk>2]
maxit = 30

tr = 0.5#(5.0,4.0,3.0,2.5,2.0)

for it in range(0,maxit):
    print "itrration ", it+1
    packet = packet_mean(out)
    maskk=out-packet
#    out[maskk>tr[it]]=np.NAN
#    maskk[maskk>tr[it]]=np.NAN
    minf=filters.rank_filter(out,45,11)#filters.minimum_filter(out,11)
    minf=filters.gaussian_filter(minf,3)
    out[maskk>tr]=minf[maskk>tr]
#    out[maskk>tr]=filters.gaussian_filter(out[maskk>tr],3)
#    out[maskk>tr[it]]=minf[maskk>tr[it]]
#    out=fill(out)
#    out[maskk>tr[it]]=packet[maskk>tr[it]]
    print "tr value =", tr#[it]
    print "mean value =", np.mean(out)
    
#mm=np.zeros(maskk.shape)
#mm[maskk>1.2]=1
#open_square = ndimage.binary_opening(mm)
#reconstruction = ndimage.binary_fill_holes(mm)
#reconstruction=fill(out,