基于遗传算法的自动拼图(四)

拼图的主函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
## 参数处理 
## 参数依次为:输入的拼图,迭代次数,种群大小,一小块拼图的尺寸,是否显示每次迭代完最适应的拼图的解决方案,保存拼图结果图片
## default则为默认参数
def parse_arguments():
parser = argparse.ArgumentParser(description="A Genetic based solver for jigsaw puzzles")
parser.add_argument("--image", type=str, default="2.jpg", help="Input image.")
parser.add_argument("--generations", type=int, default=GENERATIONS, help="Num of generations.")
parser.add_argument("--population", type=int, default=POPULATION, help="Size of population.")
parser.add_argument("--size", type=int, help="Single piece size in pixels.")
parser.add_argument("--verbose", action="store_true",default=True,help="Show best individual after each generation.")
parser.add_argument("--save", action="store_true", help="Save puzzle result as image.")
return parser.parse_args()

if __name__ == "__main__":
args = parse_arguments()
image = cv2.imread(args.image)
#在OpenCV中,图像不是用常规的RGB颜色通道来存储的,它们用的是BGR顺序
# 当读取一幅图像后,默认的是BGR
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

if args.size is not None:
piece_size = args.size
else:
detector = SizeDetector(image)
piece_size = detector.detect_piece_size()

print("\n=== Population: {}".format(args.population))
print("=== Generations: {}".format(args.generations))
print("=== Piece size: {} px".format(piece_size))

# Let the games begin! And may the odds be in your favor!
start = time()
algorithm = GeneticAlgorithm(image, piece_size, args.population, args.generations)
solution = algorithm.start_evolution(args.verbose)
end = time()

print("\n=== Done in {0:.3f} s".format(end - start))

solution_image = solution.to_image()
solution_image_name = args.image.split(".")[0] + "_solution.jpg"

if args.save:
cv2.imwrite(solution_image_name, solution_image)
print("=== Result saved as '{}'".format(solution_image_name))

print("=== Close figure to exit")
show_image(solution_image, "Solution")

plot画图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Plot(object):

def __init__(self, image, title="Initial problem"):
aspect_ratio = image.shape[0] / float(image.shape[1])

width = 8
height = width * aspect_ratio
#定义一个图像窗口,没有边框
fig = plt.figure(figsize=(width, height), frameon=False)

# Let image fill the figure
ax = plt.Axes(fig, [0., 0., 1., .9])
ax.set_axis_off()
fig.add_axes(ax)

self._current_image = ax.imshow(image, aspect="auto", animated=True)
self.show_fittest(image, title)

def show_fittest(self, image, title):
##添加标题在正上方
plt.suptitle(title, fontsize=20)
self._current_image.set_data(image)
plt.draw()

# Give pyplot 0.05s to draw image
plt.pause(0.05)

函数参考链接

https://www.jianshu.com/p/dcecaf62da71
https://blog.csdn.net/hjxu2016/article/details/77833336

threshold函数的使用

https://blog.csdn.net/u012566751/article/details/77046445
https://blog.csdn.net/keith_bb/article/details/54617625

findContours函数

https://blog.csdn.net/qq_18343569/article/details/47982045
https://www.jianshu.com/p/4bc3349b4611
https://blog.csdn.net/dcrmg/article/details/51987348

bisect函数

数组二分算法
https://www.cnblogs.com/skydesign/archive/2011/09/02/2163592.html

腐蚀与膨胀

http://ex2tron.top/2017/12/19/Python-OpenCV%E6%95%99%E7%A8%8B12%EF%BC%9A%E8%85%90%E8%9A%80%E4%B8%8E%E8%86%A8%E8%83%80/

bitwise_not函数

https://blog.csdn.net/u011028345/article/details/77278467

matplotlib的axes使用

https://ask.hellobi.com/blog/yuguiyang1990/9252
https://blog.csdn.net/Feynman1999/article/details/80280556

max中运用lambda表达式

1
2
a={'28':1,'33':4,'44':5}
t=max(a,key=a.get) #key后面的是函数名
1
Out[1]: '44'

https://code.i-harness.com/zh-CN/q/1172fb3
http://zhoufeng1989.github.io/Key-parameter-in-max-min-sorted/

python下划线

https://blog.csdn.net/hudiedd/article/details/12581485