Logo
Section 1 / 4

通过编程理解图像

学习如何根据给定的一组图像识别星系的类型,并编写程序根据星系图像判断其类型。

导入

通过编程理解图像

学习如何根据给定的一组图像识别星系的类型,并编写程序根据星系图像判断其类型。

一幅螺旋星系的图像

两个列表的比较

1def compare_lists(list_a, list_b): 2 """ 3 逐元素比较两个列表,若完全相同则返回True,否则返回False。 4 5 参数: 6 list_a (list): 要比较的第一个列表。 7 list_b (list): 要比较的第二个列表。 8 9 返回值: 10 bool: 若列表完全相同则返回True,否则返回False。 11 """ 12 # 在此处编写你的代码 13 pass 14 15def main(): 16 17 # 定义主列表 18 main_list = [1, 2, 3, 4, 5] 19 20 # 定义测试列表 21 test_list_1 = [5, 4, 3, 2, 1] 22 test_list_2 = [1, 2, 3, 4, 5] 23 24 # 比较 main_list 与 test_list_1 25 comparison_result = compare_lists(main_list, test_list_1) 26 if comparison_result == True: 27 print("test_list_1 和 main_list 相同。") 28 else: 29 print("test_list_1 和 main_list 不同。") 30 31 # 比较 main_list 与 test_list_2 32 comparison_result = compare_lists(main_list, test_list_2) 33 if comparison_result == True: 34 print("test_list_2 和 main_list 相同。") 35 else: 36 print("test_list_2 和 main_list 不同。") 37 38main()

两个二维列表的比较

1def compare_2d(list_a, list_b): 2 """ 3 用于逐元素比较两个二维列表的函数。 4 5 参数: 6 list_a (list): 第一个二维列表。 7 list_b (list): 第二个二维列表。 8 9 返回值: 10 bool: 若所有元素都相等则返回True,否则返回False。 11 """ 12 if list_a == list_b: 13 return True 14 else: 15 return False 16 17def main(): 18 19 # 定义主二维列表 20 main_list_2d = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 21 22 # 定义两个测试二维列表 23 test_list_2d_1 = [[4, 5, 6], [7, 8, 9], [10, 11, 12]] 24 test_list_2d_2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 25 26 # 将主列表与第一个测试列表进行比较 27 comparison_result = compare_2d(main_list_2d, test_list_2d_1) 28 if comparison_result: 29 print("这两个二维列表完全相同") 30 else: 31 print("这两个二维列表不相等") 32 33 # 将主列表与第二个测试列表进行比较 34 comparison_result = compare_2d(main_list_2d, test_list_2d_2) 35 if comparison_result: 36 print("这两个二维列表完全相同") 37 else: 38 print("这两个二维列表不相等") 39 40main()

图像作为像素集合
同一图像表示为数字的二维列表

1import matplotlib.pyplot as plt 2 3def compare_2d(list_a, list_b): 4 # 遍历 list_a 中的每一行 5 for i in range(len(list_a)): 6 # 遍历该行中的每个元素 7 for j in range(len(list_a[i])): 8 # 检查元素是否相等 9 if list_a[i][j] == list_b[i][j]: 10 pass 11 else: 12 # 若不相等,则返回 False 13 return False 14 # 若所有元素均相等,则返回 True 15 return True 16 17# 打开两张图像 18original_image = open_image_bw('galaxy_m61.png') 19comparison_image = open_image_bw('galaxy_m61.png') 20 21# 使用二维列表比较方法比较两张图像 22comparison_result = compare_2d(original_image, comparison_image) 23 24# 输出比较结果 25if comparison_result == True: 26 print("该图像包含一个螺旋星系。") 27else: 28 print("该图像不包含螺旋星系。") 29 30# 保存并显示被比较的两张图像 31plt.imsave('./output/original.png', original_image, cmap=plt.cm.gray_r) 32plt.imsave('./output/comparison.png', comparison_image, cmap=plt.cm.gray_r)
1import matplotlib.pyplot as plt 2 3def compare_2d(list_a, list_b): 4 5 # 遍历 list_a 中的每一行 6 for i in range(len(list_a)): 7 # 遍历该行中的每个元素 8 for j in range(len(list_a[i])): 9 # 检查元素是否相等 10 if list_a[i][j] == list_b[i][j]: 11 pass 12 else: 13 # 若不相等,则返回 False 14 return False 15 16 # 若所有元素均相等,则返回 True 17 return True 18 19# 以黑白模式打开两张图像用于比较 20original_image = open_image_bw('galaxy_m61.png') 21comparison_image = open_image_bw('m61_no_star.png') 22 23# 比较代表图像的两个二维列表 24comparison_result = compare_2d(original_image, comparison_image) 25 26# 输出比较结果 27if comparison_result == True: 28 print("该图像包含一个螺旋星系。") 29else: 30 print("该图像不包含螺旋星系。") 31 32# 保存并显示被比较的两张图像 33plt.imsave('./output/original.png', original_image, cmap=plt.cm.gray_r) 34plt.imsave('./output/comparison.png', comparison_image, cmap=plt.cm.gray_r)
1import matplotlib.pyplot as plt 2 3def compare_2d(list_a, list_b): 4 5 # 遍历 list_a 中的每一行 6 for i in range(len(list_a)): 7 # 遍历该行中的每个元素 8 for j in range(len(list_a[i])): 9 # 检查元素是否相等 10 if list_a[i][j] == list_b[i][j]: 11 pass 12 else: 13 # 若不相等,则返回 False 14 return False 15 16 # 若所有元素均相等,则返回 True 17 return True 18 19# 以黑白模式打开两张图像用于比较 20original_image = open_image_bw('galaxy_m61.png') 21comparison_image = open_image_bw('m61_dark.png') 22 23# 比较代表图像的两个二维列表 24comparison_result = compare_2d(original_image, comparison_image) 25 26# 输出比较结果 27if comparison_result == True: 28 print("该图像包含一个螺旋星系。") 29else: 30 print("该图像不包含螺旋星系。") 31 32# 保存并显示被比较的两张图像 33plt.imsave('./output/original.png', original_image, cmap=plt.cm.gray_r) 34plt.imsave('./output/comparison.png', comparison_image, cmap=plt.cm.gray_r)

人类是如何学习的?