The program is devided into three parts: First, the histograms for the 30 images are calculated and saved in HistList; Then, the index of the best-5 matches for every image are restored in IdxList; At last, a folder is built for every image with the 5 matched images. 

I tried different image dividing methods and different bin numbers. None of them give perfect results. Probably it's because since  number of edge points is not a powerful enough feature to summarize images in the same category and divide images in different sategories.  

it turned out that using less bins may provide better result. I end up using 3 bins.

The distance between 2 histograms are defined as the sum of square errors. Normalizing the histogram will not effect the result here. Because the number of elements in each histogram are the same, which equal to the number of tiles.  