Sobel、Prewitt、Roberts、Canny、Log、分水岭、Itti算法统计米粒数,最优算法计得[best_count]粒米
import cv2 import numpy as np import time from matplotlib import pyplot as plt # 读取米粒图片 img = cv2.imread('millets.jpg', 0) # 定义7种算法 algorithms = ['Sobel', 'Prewitt', 'Roberts', 'Canny', 'Log', 'Watershed', 'Itti'] counts = [] # 定义函数来统计米粒数量 def count_millets(img): # Sobel算法 sobel = cv2.Sobel(img, cv2.CV_64F, 1, 1, ksize=5) # Prewitt算法 prewitt = cv2.Canny(img, 100, 200) # Roberts算法 roberts = cv2.Robert(img, cv2.CV_64F) #Canny算法 canny = cv2.Canny(img, 100, 200) #Log算法 log = cv2.Laplacian(img, cv2.CV_64F, ksize=5) # 分水岭算法 _, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) # Itti算法 itti = cv2.ittisalience(img) return sobel, prewitt, roberts, canny, log, thresh, itti # 统计米粒数量 for i, algorithm in enumerate(algorithms): start_time = time.time() result = count_millets(img) end_time = time.time() counts.append(np.sum(result[i])) plt.subplot(3, 3, i+1) plt.imshow(result[i], cmap='gray') plt.title(algorithm + ' - ' + str(counts[i]) + ' grains') plt.show() # 绘制各算法运行时间 plt.imshow(end_time-start_time) plt.title('Execution Time for each algorithm') plt.show() # 找到粒数最接近500的算法 best_algo_index = np.argmin(np.abs(np.array(counts) - 500)) best_algo = algorithms[best_algo_index] best_count = counts[best_algo_index] print('The best algorithm for counting grains is:', best_algo) print('Total number of grains counted:', best_count) ######[AI写代码神器 | 1023点数解答 | 2024-11-23 14:42:43]
- HTML 中 JavaScript 代码功能分析、优化及注意事项揭秘(字节豆包 | 664点数解答 | 2025-06-17 14:30:03)196
- Sobel、Prewitt、Roberts、Canny、Log、分水岭、Itti算法统计米粒数,最优算法计得[best_count]粒米 (GPT | 1023点数解答 | 2024-11-23 14:42:43)103
- Sobel、Prewitt、Roberts、Canny、Log、分水岭、Itti算法统计米粒数,最优算法揭晓!(GPT | 1370点数解答 | 2024-11-23 14:43:30)178
- 用 Pandas 深度剖析数据:从读取到统计分析全流程实战(GPT | 642点数解答 | 2024-06-30 15:39:49)382
- 用 Pandas 深入分析招聘数据:从数据读取到可视化全流程解析(GPT | 1539点数解答 | 2024-06-30 15:56:50)394
- Python 实现:精确统计两数范围内素数个数,附完整代码与优化思路(阿里通义 | 292点数解答 | 2024-11-28 21:36:29)273
- Python 文件数量统计:检测当前及指定路径文件数并处理错误 (字节豆包 | 179点数解答 | 2024-12-11 13:09:13)220
- Python三关挑战:文件与文件夹数量统计及路径获取(字节豆包 | 493点数解答 | 2024-12-11 13:29:49)236
- 星际编码大赛终极对决:机械星AI与异星人激战“逆序对”统计难题(字节豆包 | 509点数解答 | 2025-04-19 17:33:00)299
- Python实现:输入5个字符统计“0”出现次数及开发注意要点(字节豆包 | 255点数解答 | 2026-01-16 14:23:30)64
- 巧用筛法!计算 N 到 M 间数的最大质因数个数(DeepSeek | 245点数解答 | 2026-01-18 12:43:51)56
- C++高效求解1到n中能同时被2和3整除的数之和(DeepSeek | 1187点数解答 | 2026-04-11 10:03:38)26