def main():
    #make the image
    img = np.zeros((128, 128), np.uint8)
    img[:, :] += 100
    #make the region
    region1 = region_node()
    for i in range(10, 31):
        for j in range(10, 41):
            region1.add_p((i, j), [200])
    his = region_similar.histogram(region1.region_value, 25)
    region1.colour_his = his / his.sum()
    region2 = region_node()
    for i in range(30, 61):
        for j in range(20, 31):
            region2.add_p((i, j), [50])
    his = region_similar.histogram(region2.region_value, 25)
    region2.colour_his = his / his.sum()
    region3 = region_node()
    for i in range(10, 21):
        for j in range(40, 51):
            region3.add_p((i, j), [150])
    his = region_similar.histogram(region3.region_value, 25)
    region3.colour_his = his / his.sum()
    region1.merge(region3)
    region1.merge(region2)
    #draw region
    print "region1's colour_his is:"
    print region1.colour_his
    print "region1's size is:" + str(region1.size)
    cv.rectangle(img, (region1.bounding_box[0][1], region1.bounding_box[0][0]),
                 (region1.bounding_box[1][1], region1.bounding_box[1][0]), 255)
    cv.imshow('region1', img)
    cv.waitKey(0)
    cv.destroyAllWindows()
    '''
def main():
    #make the image
    img = np.zeros((128,128),np.uint8)
    img[:,:] += 100
    #make the region
    region1 = region_node()
    for i in range(10,31):
        for j in range(10,41):
            region1.add_p((i,j),[200])
    his = region_similar.histogram(region1.region_value,25)
    region1.colour_his = his/his.sum()
    region2 = region_node()
    for i in range(30,61):
        for j in range(20,31):
            region2.add_p((i,j),[50])   
    his = region_similar.histogram(region2.region_value,25)
    region2.colour_his = his/his.sum()
    region3 = region_node()
    for i in range(10,21):
        for j in range(40,51):
            region3.add_p((i,j),[150])
    his = region_similar.histogram(region3.region_value,25)
    region3.colour_his = his/his.sum()
    region1.merge(region3)
    region1.merge(region2)
    #draw region
    print "region1's colour_his is:"
    print region1.colour_his
    print "region1's size is:"+str(region1.size)
    cv.rectangle(
            img,
            (region1.bounding_box[0][1],region1.bounding_box[0][0]),
            (region1.bounding_box[1][1],region1.bounding_box[1][0]),
            255)
    cv.imshow('region1',img)
    cv.waitKey(0)
    cv.destroyAllWindows()
    '''
 def initial_set(img, initial_regions, bins, similar_coef, similar_set,
                 region_set, bb_set):
     width = img.shape[1]
     height = img.shape[0]
     img_size = img.size
     for i in range(0, len(initial_regions)):
         pixel = (i // width, i - (i // width) * width)
         color = img[pixel[0], pixel[1]]
         region = disjoin_set.find(initial_regions[i])
         if region_set.has_key(region):
             region_set[region].add_p(pixel, color)
         else:
             region_set[region] = region_node()
             region_set[region].add_p(pixel, color)
     for region in region_set:
         his = region_similar.histogram(region_set[region].region_value,
                                        bins)
         region_set[region].colour_his = his / his.sum()
         bb_set.append(region_set[region].bounding_box)
     for i in range(0, len(initial_regions)):
         raw = i // width
         column = i - raw * width
         if column < width - 1:
             j = i + 1
             region1 = disjoin_set.find(initial_regions[i])
             region2 = disjoin_set.find(initial_regions[j])
             if region1 != region2:
                 selective_search.make_pair(img_size, similar_set,
                                            region_set, similar_coef,
                                            region1, region2)
         if raw < height - 1:
             j = (raw + 1) * width + column
             region1 = disjoin_set.find(initial_regions[i])
             region2 = disjoin_set.find(initial_regions[j])
             if region1 != region2:
                 selective_search.make_pair(img_size, similar_set,
                                            region_set, similar_coef,
                                            region1, region2)
         if raw < height - 1 and column < width - 1:
             j = (raw + 1) * width + (column + 1)
             region1 = disjoin_set.find(initial_regions[i])
             region2 = disjoin_set.find(initial_regions[j])
             if region1 != region2:
                 selective_search.make_pair(img_size, similar_set,
                                            region_set, similar_coef,
                                            region1, region2)
 def initial_set(img,initial_regions,bins,similar_coef,similar_set,region_set,bb_set):
     width = img.shape[1]
     height = img.shape[0]
     img_size = img.size
     for i in range(0,len(initial_regions)):
         pixel = (i//width,i-(i//width)*width)
         color = img[pixel[0],pixel[1]]
         region = disjoin_set.find(initial_regions[i])
         if region_set.has_key(region):
             region_set[region].add_p(pixel,color)
         else:
             region_set[region] = region_node()
             region_set[region].add_p(pixel,color)
     for region in region_set:
         his = region_similar.histogram(
                 region_set[region].region_value,
                 bins)
         region_set[region].colour_his = his/his.sum()
         bb_set.append(region_set[region].bounding_box)
     for i in range(0,len(initial_regions)):
         raw = i//width
         column = i-raw*width
         if column < width-1:
             j = i+1
             region1 = disjoin_set.find(initial_regions[i])
             region2 = disjoin_set.find(initial_regions[j])
             if region1!=region2:
                 selective_search.make_pair(
                         img_size,
                         similar_set,
                         region_set,
                         similar_coef,
                         region1,
                         region2
                         )
         if raw < height-1:
             j = (raw+1)*width+column
             region1 = disjoin_set.find(initial_regions[i])
             region2 = disjoin_set.find(initial_regions[j])
             if region1!=region2:
                 selective_search.make_pair(
                         img_size,
                         similar_set,
                         region_set,
                         similar_coef,
                         region1,
                         region2
                         )
         if raw < height-1 and column < width-1:
             j = (raw+1)*width+(column+1)
             region1 = disjoin_set.find(initial_regions[i])
             region2 = disjoin_set.find(initial_regions[j])
             if region1!=region2:
                 selective_search.make_pair(
                         img_size,
                         similar_set,
                         region_set,
                         similar_coef,
                         region1,
                         region2
                         )