示例#1
0
  def convert(self, lg, choices = 1, adv_match = False, textures = TextureCache(), memory = 0):
    """Given a line graph this chops it into chunks, matches each chunk to the database of chunks and returns a new line graph with these chunks instead of the original. Output will involve heavy overlap requiring clever blending. choices is the number of options it select from the db - it grabs this many closest to the requirements and then randomly selects from them. If adv_match is True then instead of random selection from the choices it does a more advanced match, and select the best match in terms of colour distance from already-rendered chunks. This option is reasonably expensive. memory is how many recently use chunks to remember, to avoid repetition."""
    if memory > (choices - 1):
      memory = choices - 1

    # If we have no data just return the input...
    if self.empty(): return lg
    
    # Check if the indexing structure is valid - if not create it...
    if self.kdtree==None:
      data = numpy.array(map(lambda p: self.feature_vect(p[0], p[1]), self.chunks), dtype=numpy.float)
      self.kdtree = scipy.spatial.cKDTree(data, 4)
      
    # Calculate the radius scaler and distance for this line graph, by calculating the median radius...
    rads = map(lambda i: lg.get_vertex(i)[5], xrange(lg.vertex_count))
    rads.sort()
    median_radius = rads[len(rads)//2]
    radius_mult = 1.0 / median_radius
    
    dist = self.dist * median_radius
    
    # Create the list into which we dump all the chunks that will make up the return...
    chunks = []
    temp = LineGraph()
    
    # List of recently used chunks, to avoid obvious patterns...
    recent = []
    
    # If advanced match we need a Composite of the image thus far, to compare against...
    if adv_match:
      canvas = Composite()
      min_x, max_x, min_y, max_y = lg.get_bounds()
      canvas.set_size(int(max_x+8), int(max_y+8))
    
    # Iterate the line graph, choping it into chunks and matching a chunk to each chop...
    for chain in lg.chains():
      head = 0
      tail = 0
      length = 0.0
        
      while True:
        # Move tail so its long enough, or has reached the end...
        while length<dist and tail+1<len(chain):
          tail += 1
          v1 = lg.get_vertex(chain[tail-1])
          v2 = lg.get_vertex(chain[tail])
          length += numpy.sqrt((v1[0]-v2[0])**2 + (v1[1]-v2[1])**2)

        # Extract a feature vector for this chunk...
        temp.from_vertices(lg, chain[head:tail+1])
        fv = self.feature_vect(temp, median_radius)
        
        # Select a chunk from the database...
        if choices==1:
          selected = self.kdtree.query(fv)[1]
          orig_chunk = self.chunks[selected]
        else:
          options = list(self.kdtree.query(fv, choices)[1])
          options = filter(lambda v: v not in recent, options)
          if not adv_match:
            selected = random.choice(options)
            orig_chunk = self.chunks[selected]
          else:
            cost = 1e64 * numpy.ones(len(options))
            
            for i, option in enumerate(options):
              fn = filter(lambda t: t[0].startswith('texture:'), self.chunks[option][0].get_tags())
              if len(fn)!=0:
                fn = fn[0][0][len('texture:'):]
                tex = textures[fn]
                
                chunk = LineGraph()
                chunk.from_many(self.chunks[option][0])
                chunk.morph_to(lg, chain[head:tail+1])
              
                part = canvas.draw_line_graph(chunk)
                cost[i] = canvas.cost_texture_nearest(tex, part)
            
            selected = options[numpy.argmin(cost)]
            orig_chunk = self.chunks[selected]
        
        # Update recent list...
        recent.append(selected)
        if len(recent)>memory:
          recent.pop(0)

        # Distort it to match the source line graph...
        chunk = LineGraph()
        chunk.from_many(orig_chunk[0])
        chunk.morph_to(lg, chain[head:tail+1])
        
        # Record it for output...
        chunks.append(chunk)
        
        # If advanced matching is on write it out to canvas, so future choices will take it into account...
        if adv_match:
          fn = filter(lambda t: t[0].startswith('texture:'), chunk.get_tags())
          if len(fn)!=0:
            fn = fn[0][0][len('texture:'):]
            tex = textures[fn]

            part = canvas.draw_line_graph(chunk)
            canvas.paint_texture_nearest(tex, part)
         
        # If tail is at the end exit the loop...
        if tail+1 >= len(chain): break
          
        # Move head along for the next chunk...
        to_move = dist * self.factor
        while to_move>0.0 and head+2<len(chain):
          head += 1
          v1 = lg.get_vertex(chain[head-1])
          v2 = lg.get_vertex(chain[head])
          offset = numpy.sqrt((v1[0]-v2[0])**2 + (v1[1]-v2[1])**2)
          length -= offset
          to_move -= offset

    # Return the final line graph...
    ret = LineGraph()
    ret.from_many(*chunks)
    return ret
示例#2
0
文件: generate.py 项目: zoginni/helit
def render(lg,
           border=8,
           textures=TextureCache(),
           cleverness=0,
           radius_growth=3.0,
           stretch_weight=0.5,
           edge_weight=0.5,
           smooth_weight=2.0,
           alpha_weight=1.0,
           unary_mult=1.0,
           overlap_weight=0.0,
           use_linear=True):
    """Given a line_graph this will render it, returning a numpy array that represents an image (As the first element in a tuple - second element is how many graph cut problems it solved.). It will transform the entire linegraph to obtain a suitable border. The cleverness parameter indicates how it merges the many bits - 0 means last layer (stupid), 1 means averaging; 2 selecting a border using max flow; 3 using graph cuts to take into account weight as well."""

    # Setup the compositor...
    comp = Composite()
    min_x, max_x, min_y, max_y = lg.get_bounds()

    do_transform = False
    offset_x = 0.0
    offset_y = 0.0

    if min_x < border:
        do_transform = True
        offset_x = border - min_x

    if min_y < border:
        do_transform = True
        offset_y = border - min_y

    if do_transform:
        hg = numpy.eye(3, dtype=numpy.float32)
        hg[0, 2] = offset_x
        hg[1, 2] = offset_y

        lg.transform(hg)

        max_x += offset_x
        max_y += offset_y

    comp.set_size(int(max_x + border), int(max_y + border))

    # Break the lg into segments, as each can have its own image - draw & paint each in turn...
    lg.segment()
    duplicate_sets = dict()

    for s in xrange(lg.segments):

        slg = LineGraph()
        slg.from_segment(lg, s)
        part = comp.draw_line_graph(slg, radius_growth, stretch_weight)

        done = False
        fn = filter(lambda t: t[0].startswith('texture:'), slg.get_tags())
        if len(fn) != 0: fn = fn[0][0][len('texture:'):]
        else: fn = None

        for pair in filter(lambda t: t[0].startswith('duplicate:'),
                           slg.get_tags()):
            key = pair[0][len('duplicate:'):]
            if key in duplicate_sets: duplicate_sets[key].append(part)
            else: duplicate_sets[key] = [part]

        tex = textures[fn]

        if tex is not None:
            if use_linear:
                comp.paint_texture_linear(tex, part)
            else:
                comp.paint_texture_nearest(tex, part)
            done = True

        if not done:
            comp.paint_test_pattern(part)

    # Bias towards pixels that are opaque...
    comp.inc_weight_alpha(alpha_weight)

    # Arrange for duplicate pairs to have complete overlap, by adding transparent pixels, so graph cuts doesn't create a feather effect...
    if overlap_weight > 1e-6:
        for values in duplicate_sets.itervalues():
            for i, part1 in enumerate(values):
                for part2 in values[i:]:
                    comp.draw_pair(part1, part2, overlap_weight)

    # If requested use maxflow to find optimal cuts, to avoid any real blending...
    count = 0
    if cleverness == 2:
        count = comp.maxflow_select(edge_weight, smooth_weight, maxflow)
    elif cleverness == 3:
        count = comp.graphcut_select(edge_weight, smooth_weight, unary_mult,
                                     maxflow)

    if cleverness == 0:
        render = comp.render_last()
    else:
        render = comp.render_average()

    # Return the rendered image (If cleverness==0 this will actually do some averaging, otherwise it will just create an image)...
    return render, count
示例#3
0
文件: chunk_db.py 项目: zoginni/helit
    def convert(self,
                lg,
                choices=1,
                adv_match=False,
                textures=TextureCache(),
                memory=0):
        """Given a line graph this chops it into chunks, matches each chunk to the database of chunks and returns a new line graph with these chunks instead of the original. Output will involve heavy overlap requiring clever blending. choices is the number of options it select from the db - it grabs this many closest to the requirements and then randomly selects from them. If adv_match is True then instead of random selection from the choices it does a more advanced match, and select the best match in terms of colour distance from already-rendered chunks. This option is reasonably expensive. memory is how many recently use chunks to remember, to avoid repetition."""
        if memory > (choices - 1):
            memory = choices - 1

        # If we have no data just return the input...
        if self.empty(): return lg

        # Check if the indexing structure is valid - if not create it...
        if self.kdtree == None:
            data = numpy.array(map(lambda p: self.feature_vect(p[0], p[1]),
                                   self.chunks),
                               dtype=numpy.float)
            self.kdtree = scipy.spatial.cKDTree(data, 4)

        # Calculate the radius scaler and distance for this line graph, by calculating the median radius...
        rads = map(lambda i: lg.get_vertex(i)[5], xrange(lg.vertex_count))
        rads.sort()
        median_radius = rads[len(rads) // 2]
        radius_mult = 1.0 / median_radius

        dist = self.dist * median_radius

        # Create the list into which we dump all the chunks that will make up the return...
        chunks = []
        temp = LineGraph()

        # List of recently used chunks, to avoid obvious patterns...
        recent = []

        # If advanced match we need a Composite of the image thus far, to compare against...
        if adv_match:
            canvas = Composite()
            min_x, max_x, min_y, max_y = lg.get_bounds()
            canvas.set_size(int(max_x + 8), int(max_y + 8))

        # Iterate the line graph, choping it into chunks and matching a chunk to each chop...
        for chain in lg.chains():
            head = 0
            tail = 0
            length = 0.0

            while True:
                # Move tail so its long enough, or has reached the end...
                while length < dist and tail + 1 < len(chain):
                    tail += 1
                    v1 = lg.get_vertex(chain[tail - 1])
                    v2 = lg.get_vertex(chain[tail])
                    length += numpy.sqrt((v1[0] - v2[0])**2 +
                                         (v1[1] - v2[1])**2)

                # Extract a feature vector for this chunk...
                temp.from_vertices(lg, chain[head:tail + 1])
                fv = self.feature_vect(temp, median_radius)

                # Select a chunk from the database...
                if choices == 1:
                    selected = self.kdtree.query(fv)[1]
                    orig_chunk = self.chunks[selected]
                else:
                    options = list(self.kdtree.query(fv, choices)[1])
                    options = filter(lambda v: v not in recent, options)
                    if not adv_match:
                        selected = random.choice(options)
                        orig_chunk = self.chunks[selected]
                    else:
                        cost = 1e64 * numpy.ones(len(options))

                        for i, option in enumerate(options):
                            fn = filter(lambda t: t[0].startswith('texture:'),
                                        self.chunks[option][0].get_tags())
                            if len(fn) != 0:
                                fn = fn[0][0][len('texture:'):]
                                tex = textures[fn]

                                chunk = LineGraph()
                                chunk.from_many(self.chunks[option][0])
                                chunk.morph_to(lg, chain[head:tail + 1])

                                part = canvas.draw_line_graph(chunk)
                                cost[i] = canvas.cost_texture_nearest(
                                    tex, part)

                        selected = options[numpy.argmin(cost)]
                        orig_chunk = self.chunks[selected]

                # Update recent list...
                recent.append(selected)
                if len(recent) > memory:
                    recent.pop(0)

                # Distort it to match the source line graph...
                chunk = LineGraph()
                chunk.from_many(orig_chunk[0])
                chunk.morph_to(lg, chain[head:tail + 1])

                # Record it for output...
                chunks.append(chunk)

                # If advanced matching is on write it out to canvas, so future choices will take it into account...
                if adv_match:
                    fn = filter(lambda t: t[0].startswith('texture:'),
                                chunk.get_tags())
                    if len(fn) != 0:
                        fn = fn[0][0][len('texture:'):]
                        tex = textures[fn]

                        part = canvas.draw_line_graph(chunk)
                        canvas.paint_texture_nearest(tex, part)

                # If tail is at the end exit the loop...
                if tail + 1 >= len(chain): break

                # Move head along for the next chunk...
                to_move = dist * self.factor
                while to_move > 0.0 and head + 2 < len(chain):
                    head += 1
                    v1 = lg.get_vertex(chain[head - 1])
                    v2 = lg.get_vertex(chain[head])
                    offset = numpy.sqrt((v1[0] - v2[0])**2 +
                                        (v1[1] - v2[1])**2)
                    length -= offset
                    to_move -= offset

        # Return the final line graph...
        ret = LineGraph()
        ret.from_many(*chunks)
        return ret
示例#4
0
def render(lg, border = 8, textures = TextureCache(), cleverness = 0, radius_growth = 3.0, stretch_weight = 0.5, edge_weight = 0.5, smooth_weight = 2.0, alpha_weight = 1.0, unary_mult = 1.0, overlap_weight = 0.0, use_linear = True):
  """Given a line_graph this will render it, returning a numpy array that represents an image (As the first element in a tuple - second element is how many graph cut problems it solved.). It will transform the entire linegraph to obtain a suitable border. The cleverness parameter indicates how it merges the many bits - 0 means last layer (stupid), 1 means averaging; 2 selecting a border using max flow; 3 using graph cuts to take into account weight as well."""

  # Setup the compositor...
  comp = Composite()
  min_x, max_x, min_y, max_y = lg.get_bounds()
  
  do_transform = False
  offset_x = 0.0
  offset_y = 0.0
  
  if min_x<border:
    do_transform = True
    offset_x = border-min_x
    
  if min_y<border:
    do_transform = True
    offset_y = border-min_y
  
  if do_transform:
    hg = numpy.eye(3, dtype=numpy.float32)
    hg[0,2] = offset_x
    hg[1,2] = offset_y
    
    lg.transform(hg)
    
    max_x += offset_x
    max_y += offset_y
  
  comp.set_size(int(max_x+border), int(max_y+border))


  # Break the lg into segments, as each can have its own image - draw & paint each in turn...
  lg.segment()
  duplicate_sets = dict()

  for s in xrange(lg.segments):

    slg = LineGraph()
    slg.from_segment(lg, s)
    part = comp.draw_line_graph(slg, radius_growth, stretch_weight)
    
    done = False
    fn = filter(lambda t: t[0].startswith('texture:'), slg.get_tags())
    if len(fn)!=0: fn = fn[0][0][len('texture:'):]
    else: fn = None
    
    for pair in filter(lambda t: t[0].startswith('duplicate:'), slg.get_tags()):
      key = pair[0][len('duplicate:'):]
      if key in duplicate_sets: duplicate_sets[key].append(part)
      else: duplicate_sets[key] = [part]
    
    tex = textures[fn]
    
    if tex!=None:
      if use_linear:
        comp.paint_texture_linear(tex, part)
      else:
        comp.paint_texture_nearest(tex, part)
      done = True
    
    if not done:
      comp.paint_test_pattern(part)

  
  # Bias towards pixels that are opaque...
  comp.inc_weight_alpha(alpha_weight)
  
  # Arrange for duplicate pairs to have complete overlap, by adding transparent pixels, so graph cuts doesn't create a feather effect...
  if overlap_weight>1e-6:
    for values in duplicate_sets.itervalues():
      for i, part1 in enumerate(values):
        for part2 in values[i:]:
          comp.draw_pair(part1, part2, overlap_weight)
  
  # If requested use maxflow to find optimal cuts, to avoid any real blending...
  count = 0
  if cleverness==2:
    count = comp.maxflow_select(edge_weight, smooth_weight, maxflow)
  elif cleverness==3:
    count = comp.graphcut_select(edge_weight, smooth_weight, unary_mult, maxflow)
  
  if cleverness==0:
    render = comp.render_last()
  else:
    render = comp.render_average()

  # Return the rendered image (If cleverness==0 this will actually do some averaging, otherwise it will just create an image)...
  return render, count