示例#1
0
def save_display(save, display, info, np_img, slide_num, filter_num, display_text, file_text,
                 display_mask_percentage=True):
  """
  Optionally save an image and/or display the image.

  Args:
    save: If True, save filtered images.
    display: If True, display filtered images to screen.
    info: Dictionary to store filter information.
    np_img: Image as a NumPy array.
    slide_num: The slide number.
    filter_num: The filter number.
    display_text: Filter display name.
    file_text: Filter name for file.
    display_mask_percentage: If True, display mask percentage on displayed slide.
  """
  mask_percentage = None
  if display_mask_percentage:
    mask_percentage = mask_percent(np_img)
    display_text = display_text + "\n(" + mask_percentage_text(mask_percentage) + " masked)"
  if slide_num is None and filter_num is None:
    pass
  elif filter_num is None:
    display_text = "S%03d " % slide_num + display_text
  elif slide_num is None:
    display_text = "F%03d " % filter_num + display_text
  else:
    display_text = "S%03d-F%03d " % (slide_num, filter_num) + display_text
  if display:
    util.display_img(np_img, display_text)
  if save:
    save_filtered_image(np_img, slide_num, filter_num, file_text)
  if info is not None:
    info[slide_num * 1000 + filter_num] = (slide_num, filter_num, display_text, file_text, mask_percentage)
def show_subsection(img, sigma, center_x, center_y):
    x_start = int(center_x - sigma)
    x_end = int(center_x + sigma)
    y_start = int(center_y - sigma)
    y_end = int(center_y + sigma)
    crop_img = img[y_start:y_end, x_start:x_end]
    display_img(crop_img)
    img = cv2.rectangle(img, (x_start, y_start), (x_end, y_end), (0, 255, 0),
                        3)
    display_img(img)
def smooth_image(f, curves, img, artist, img_name):

    curveImg = util.mapCurvesToImg(curves, img.shape)
    util.display_img(curveImg, "Current Image", DISPLAY_IMG)
    cv2.imwrite(
        "smoothing/%s/%s/%s_Smoothing_%d.jpg" %
        (artist, img_name, img_name, 0), curveImg)
    """ Iteratively smooth each curve """

    start_time = time.time()
    for i in xrange(SMOOTHING_ROUNDS):
        print("Round %s" % i)
        if i % 5 == 0 and i != 0:
            curveImg = util.mapCurvesToImg_circles(curves, img.shape)
            util.display_img(curveImg, "Post Smoothing %d" % i, DISPLAY_IMG)
            cv2.imwrite(
                "smoothing/%s/%s/%s_Smoothing_%d.jpg" %
                (artist, img_name, img_name, i), curveImg)
        for curve in curves:
            for point in curve:
                gaussian_smoothing_function(curve, point)
    end_time = time.time()
    f.write('Time spent smoothing (s): %s\n' % (end_time - start_time))

    curveImg = util.mapCurvesToImg_circles(curves, img.shape)
    util.display_img(curveImg, "Post Smoothing %s" % SMOOTHING_ROUNDS,
                     DISPLAY_IMG)

    cv2.imwrite(
        "smoothing/%s/%s/%s_Smoothing_%d.jpg" %
        (artist, img_name, img_name, 30), curveImg)
    #util.save_curves_matlab("%s_SMOOTH" % img_name, artist, "curves", curves, img.shape)
    mfc.save_matlab_suite("%s_SMOOTH" % img_name, artist, "curves", curves,
                          img.shape)
    util.save_curves("%s_SMOOTH.txt" % img_name, artist, "curves", curves)
示例#4
0
def get_cluster_points(f, img, shape, img_name, artist, dir="pixelClustering"):

    print("IMAGE SHAPE: (%s, %s)" % (shape[0], shape[1]))

    f.write("Speed factor: %f\n" % SPEED_FACTOR)
    f.write("Stopping Point: %f\n" % STOPPING_POINT)
    f.write("Min Speed: %f\n" % MIN_SPEED)
    f.write("Min Neighbours: %f\n" % MIN_NEIGHBOURS)
    f.write("Neighbourhood Radius: %f\n\n" % NBHD_RADIUS)

    blur = cv2.GaussianBlur(img, (7, 7), 1)
    util.display_img(blur, "Blurred", DISPLAY_IMG)
    sobelx = cv2.Sobel(blur, cv2.CV_64F, 1, 0, ksize=5)
    sobely = cv2.Sobel(blur, cv2.CV_64F, 0, 1, ksize=5)

    scaled_x = cv2.convertScaleAbs(sobelx)
    scaled_y = cv2.convertScaleAbs(sobely)
    cv2.imwrite('%s/%s/%s/%s_sobelx.png' % (dir, artist, img_name, img_name),
                scaled_x)
    cv2.imwrite('%s/%s/%s/%s_sobely.png' % (dir, artist, img_name, img_name),
                scaled_y)

    moving_points, point_map = get_moving_pixels(f, img, sobelx, sobely)
    print("Number of moving points: %s" % len(moving_points))
    f.write("Initial number of moving points: %d\n" % len(moving_points))

    newImg = util.mapToImage(moving_points, img.shape)
    cv2.imwrite(
        '%s/%s/%s/%s_COLOR_%d.png' % (dir, artist, img_name, img_name, 1),
        newImg)
    #util.display_img(newImg, "Mapped from Point Set!", DISPLAY_IMG)

    # MOVEMENT PHASE

    start_time = time.time()
    movement_phase = True
    counter = 0
    while movement_phase:
        counter += 1

        bad_pix = []
        # movement phase
        for i in xrange(len(moving_points)):

            idx = (util.R(moving_points[i].x), util.R(moving_points[i].y))

            x_speed = getMinSpeed(SPEED_FACTOR * moving_points[i].gradx)
            y_speed = getMinSpeed(SPEED_FACTOR * moving_points[i].grady)

            if moving_points[i].stopped: continue
            moving_points[i].x = moving_points[i].x - x_speed
            moving_points[i].y = moving_points[i].y - y_speed

            idx2 = (util.R(moving_points[i].x), util.R(moving_points[i].y))

            # update space in bin
            if idx != idx2:
                point_map[idx].remove(moving_points[i])
                if idx2 in point_map:
                    point_map[idx2].append(moving_points[i])
                else:
                    print("BAD POINT FOUND: (%.3f, %.3f)" %
                          (moving_points[i].x, moving_points[i].y))
                    bad_pix.append(moving_points[i])

        for p in bad_pix:
            moving_points.remove(p)

        # evaluation phase
        for p in moving_points:
            stop = stop_pixel(point_map, p, shape)
            if stop:
                p.stopped = stop
        print("PHASE COMPLETE.")

        newImg2 = util.mapToImage_moving(moving_points, img.shape)
        cv2.imwrite(
            '%s/%s/%s/%s_COLOR_%d.png' % (dir, artist, img_name, img_name,
                                          (counter + 1)), newImg2)
        #util.display_img(newImg2, "Mapped from Point Set %s!" % (counter+1), DISPLAY_IMG

        # Determine whether to break the loop
        stp = get_percent_stopped_pixels(moving_points)
        print("Percentage stopped: %f" % stp)
        f.write("Rep %d. Percentage stopped: %f\n" % (counter, stp))
        if stp > STOPPING_POINT:
            print("STOPPING THE LOOP")
            movement_phase = False

    end_time = time.time()
    moving_points = clean_up(moving_points)
    f.write("\nTOTAL Number of iterations: %d\n" % counter)
    f.write("Time taken (in seconds): %f\n" % (end_time - start_time))
    f.write("Final number of moving points: %d\n" % len(moving_points))

    newImg2 = util.mapToImage(moving_points, img.shape)
    util.display_img(newImg2, "THE FINAL RESULT", DISPLAY_IMG)
    cv2.imwrite(
        '%s/%s/%s/%s_COLOR_%s.png' %
        (dir, artist, img_name, img_name, "FINAL"), newImg2)

    cv2.destroyAllWindows()

    calculate_local_stroke_thickness(f, point_map, moving_points, shape)

    util.save_point_set("%s.txt" % img_name, artist, "pointSet", moving_points)

    return moving_points
示例#5
0
    ]
    images = [
        'test2', 'levesque2', 'koudelka5', 'vidal2', 'fiala_001', 'fiala_010',
        'levesque1', 'vidal6', 'fiala_014', 'test1'
    ]

    artists = ['fiala']
    images = ['fiala_001']

    for i in xrange(len(images)):
        artist = artists[i]
        img_name = images[i]
        orig_name = img_name

        # Load a color image in grayscale
        img = cv2.imread("samples/%s/%s.jpg" % (artist, img_name), 0)
        util.display_img(img, "Initial Image", DISPLAY_IMG)

        img_name = orig_name + "_SCARCE"

        print("Clustering %s, %s" % (artist, img_name))
        util.DW("pixelClustering/%s/%s" % (artist, img_name))
        util.DW("pointSet/%s" % artist)
        util.DW("stats/%s" % artist)

        f = open(
            "%s/%s/%s_pixelClustering_NEW.txt" % ("stats", artist, img_name),
            'w')
        moving_points = get_cluster_points(f, img, img.shape, img_name, artist)
        f.close()
示例#6
0
def extract_topology(f, moving_points, point_map, img, corners, artist,
                     img_name):

    # encode some constants
    f.write("SCALE FACTOR: %d\n" % SCALE_FACTOR)
    f.write("PRUNING RADIUS: %.4f\n" % PRUNING_RADIUS)

    G = create_cluster_graph(f, point_map, moving_points, img.shape)
    """ Compute Minimum Spanning Tree """

    start_time = time.time()
    mst = nx.minimum_spanning_tree(G)
    end_time = time.time()
    print("NUMBER OF MST NODES: %d" % mst.number_of_nodes())
    print("NUMBER OF MST EDGES: %d" % mst.number_of_edges())
    f.write("NUMBER OF MST NODES: %d\n" % mst.number_of_nodes())
    f.write("NUMBER OF MST EDGES: %d\n" % mst.number_of_edges())
    f.write("Time to compute mst (s): %s\n\n" % (end_time - start_time))

    newImg = util.mapToImage(mst.nodes(), img.shape)
    util.thicken_line(newImg)
    util.display_img(newImg, "Post MST", DISPLAY_IMG)
    cv2.imwrite(
        "topology/%s/%s/%s_CompleteGraph.jpg" % (artist, img_name, img_name),
        newImg)
    """ Iterative Pruning """

    f.write("--- ITERATIVE PRUNING ---\n")
    num_spc_pts = len(moving_points)
    prev_spc_pts = num_spc_pts  # avoid infinite loops
    streak_len = 0
    counter = 0
    endpoints = []
    junctions = []
    itr_pruning_start = time.time()
    while num_spc_pts >= len(moving_points) // SCALE_FACTOR:

        counter += 1
        leaf_nodes = [x for x in mst.nodes_iter() if mst.degree(x) == 1]

        for leaf in leaf_nodes:
            total_weight = 0
            curr_node = leaf
            while total_weight < max(PRUNING_RADIUS, curr_node.lsw):
                n = mst.neighbors(curr_node)
                if len(n) != 1: break  # used to be == 0
                e = mst.get_edge_data(curr_node, n[0])
                if total_weight + e['weight'] < max(PRUNING_RADIUS,
                                                    curr_node.lsw):
                    total_weight += e['weight']
                    mst.remove_node(curr_node)
                    curr_node = n[0]
                else:
                    break

        print("NUM LEAF NODES: %d" % len(leaf_nodes))
        print("NUMBER OF PRUNED MST NODES: %d" % mst.number_of_nodes())
        print("NUMBER OF PRUNED MST EDGES: %d" % mst.number_of_edges())

        endpoints = [x for x in mst.nodes_iter() if mst.degree(x) == 1]
        junctions = [x for x in mst.nodes_iter() if mst.degree(x) >= 3]

        num_spc_pts = len(endpoints) + len(junctions)
        print("REP %d: %d points, %d special points" %
              (counter, len(moving_points), num_spc_pts))
        f.write("REP %d: %d points, %d special points\n" %
                (counter, len(moving_points), num_spc_pts))

        mstImg = util.mapToImage(mst.nodes(), img.shape)
        colorImg = mstImg.copy()
        colorImg = cv2.cvtColor(colorImg, cv2.COLOR_GRAY2BGR)

        for endpoint in endpoints:
            cv2.circle(colorImg, (util.R(endpoint.y), util.R(endpoint.x)), 3,
                       (0, 0, 255), -1)

        for junction in junctions:
            cv2.circle(colorImg, (util.R(junction.y), util.R(junction.x)), 3,
                       (255, 0, 0), -1)

        util.display_img(colorImg, "Post MST Pruning %d" % counter,
                         DISPLAY_IMG)
        cv2.imwrite(
            "topology/%s/%s/%s_Pruned_%d.jpg" %
            (artist, img_name, img_name, counter), colorImg)

        # AVOID INFINITE LOOPS
        if prev_spc_pts == num_spc_pts:
            if streak_len < 2:
                prev_spc_pts = num_spc_pts
                streak_len += 1
            else:
                break
        else:
            prev_spc_pts = num_spc_pts

    itr_pruning_end = time.time()
    print("\n--- FINAL POINTSET ---")
    print("%d points, %d special points\n" %
          (len(moving_points), (len(endpoints) + len(junctions))))
    f.write("\n--- FINAL POINTSET ---\n")
    f.write("%d points, %d special points\n" %
            (len(moving_points), (len(endpoints) + len(junctions))))
    f.write("Pruning time (in s): %s\n\n" %
            (itr_pruning_end - itr_pruning_start))

    mstImg = util.mapToImage(mst.nodes(), img.shape)
    colorImg = mstImg.copy()
    colorImg = cv2.cvtColor(colorImg, cv2.COLOR_GRAY2BGR)

    for corner in corners:
        x, y = corner.ravel()
        cv2.circle(colorImg, (x, y), 3, (0, 255, 0), -1)

    cv2.imwrite(
        "topology/%s/%s/%s_CORNERS_mst.jpg" % (artist, img_name, img_name),
        colorImg)

    f.write("NUMBER OF Connected Components: %d\n" %
            nx.number_connected_components(mst))

    loner_nodes = [x for x in mst.nodes_iter() if mst.degree(x) == 0]
    mst.remove_nodes_from(loner_nodes)
    mst.remove_nodes_from(endpoints)
    mst.remove_nodes_from(junctions)

    junction_endpoints = endpoints + junctions
    all_pairs = np.transpose([
        np.tile(junction_endpoints, len(junction_endpoints)),
        np.repeat(junction_endpoints, len(junction_endpoints))
    ])
    print("Number of pairs: %d" % len(all_pairs))
    f.write("Number of endpoint-junction pairs: %d\n" % len(all_pairs))

    print("NUMBER OF CC's: %d" % nx.number_connected_components(mst))
    f.write("NEW NUMBER OF Connected Components: %d\n" %
            nx.number_connected_components(mst))
    components_subgraphs = nx.connected_component_subgraphs(mst)
    point_pairs = []
    counter = 0
    cc_start_time = time.time()
    for comp in components_subgraphs:
        counter += 1
        endpoints = [x for x in comp.nodes_iter() if comp.degree(x) == 1]
        if len(endpoints) != 2: continue

        min_dist = 1000
        pair = []
        for p0, p1 in all_pairs:
            dist1 = util.pointDist(p0, endpoints[0]) + util.pointDist(
                p1, endpoints[1])
            dist2 = util.pointDist(p0, endpoints[1]) + util.pointDist(
                p1, endpoints[0])
            if dist1 < min_dist:
                min_dist = dist1
                pair = (p0, p1)
            if dist2 < min_dist:
                min_dist = dist2
                pair = (p1, p0)
        point_pairs.append(pair)

        testImg = img.copy()
        testImg = cv2.cvtColor(testImg, cv2.COLOR_GRAY2BGR)
        for node in comp.nodes_iter():
            cv2.circle(testImg, (util.R(node.y), util.R(node.x)), 3,
                       (0, 255, 0), -1)
        cv2.circle(testImg, (util.R(pair[0].y), util.R(pair[0].x)), 3,
                   (0, 0, 255), -1)
        cv2.circle(testImg, (util.R(pair[1].y), util.R(pair[1].x)), 3,
                   (0, 0, 255), -1)
        util.display_img(testImg, "Test", False)
        if len(comp.nodes()) > MIN_NODES_DETECTED:
            cv2.imwrite(
                "topology/%s/%s/%s_DetectedCurve_%d.jpg" %
                (artist, img_name, img_name, counter), testImg)

    cc_end_time = time.time()
    print("NUMBER OF POINT PAIRS: %d\n" % len(point_pairs))
    f.write("NUMBER OF POINT PAIRS: %d\n" % len(point_pairs))
    f.write("Time to Find Point Pairs (in s): %s\n\n" %
            (cc_end_time - cc_start_time))
    util.save_buddy_points("%s.txt" % img_name, artist, "buddyPoints",
                           point_pairs)

    f.write(" --- Calculating Paths --- \n")

    curves = []
    counter = 0
    curve_start = time.time()
    for pair in point_pairs:
        counter += 1
        path = nx.dijkstra_path(G, pair[0], pair[1])

        curves.append(path)
        print("Length of path: %d" % len(path))
        f.write("Length of path: %d\n" % len(path))

        testImg = img.copy()
        testImg = cv2.cvtColor(testImg, cv2.COLOR_GRAY2BGR)
        for node in path:
            cv2.circle(testImg, (util.R(node.y), util.R(node.x)), 3,
                       (0, 0, 255), -1)
        util.display_img(testImg, "Test", False)
        if len(path) > MIN_NODES_ACTUAL:
            cv2.imwrite(
                "topology/%s/%s/%s_ActualCurve_%d.jpg" %
                (artist, img_name, img_name, counter), testImg)

    curve_end = time.time()
    util.save_curves("%s.txt" % img_name, artist, "curves", curves)
    f.write("\nTime to Seperate Curves (in s): %s\n\n" %
            (curve_end - curve_start))
示例#7
0
    'fiala_021', 'levesque3', 'test3'
]

artists = ['couture', 'koudelka']
images = ['couture1', 'koudelka6']

for i in xrange(len(images)):

    artist = artists[i]
    img_name = images[i]

    print("Vectorizing %s" % img_name)

    # Load a color image in grayscale
    img = cv2.imread("samples/%s/%s.jpg" % (artist, img_name), 0)
    util.display_img(img, "Initial Image", False)

    img_name += "_170421_FINAL"

    # ensure that required directories are created
    util.DW("pixelClustering/%s/%s" % (artist, img_name))
    util.DW("pointSet/%s" % artist)
    util.DW("stats/%s" % artist)
    util.DW("curves/%s" %
            artist)  # I think you only need this in a next step...
    util.DW("topology/%s/%s" % (artist, img_name))
    util.DW("buddyPoints/%s" % artist)
    util.DW("smoothing/%s/%s" % (artist, img_name))
    util.DW("vectorized/%s" % artist)

    f = open("%s/%s/%s_FULL.txt" % ("stats", artist, img_name), 'w')