126 lines
4.5 KiB
Python
Executable File
126 lines
4.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import argparse
|
|
import cv2
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
import os
|
|
from test.sky_ratio_analyzer import analyze_sky_ratio as simple_analyze
|
|
from utils.bar.advanced_sky_analyzer import analyze_sky_ratio as advanced_analyze
|
|
from utils.gray_sky_analyzer import analyze_gray_sky_ratio
|
|
|
|
def compare_methods(image_path, save_result=False):
|
|
"""
|
|
比较不同方法分析同一张图片的结果
|
|
|
|
参数:
|
|
image_path: 图片路径
|
|
save_result: 是否保存结果图像
|
|
"""
|
|
# 读取图片
|
|
img = cv2.imread(image_path)
|
|
if img is None:
|
|
raise ValueError(f"无法读取图片: {image_path}")
|
|
|
|
# 获取图片文件名(不带路径和扩展名)
|
|
filename = os.path.splitext(os.path.basename(image_path))[0]
|
|
|
|
# 记录结果
|
|
results = {}
|
|
|
|
# 使用简单阈值法
|
|
simple_ratio = simple_analyze(image_path, threshold=180, debug=False)
|
|
results["简单阈值法"] = simple_ratio
|
|
|
|
# 使用高级方法
|
|
# 阈值法
|
|
threshold_ratio = advanced_analyze(image_path, method="threshold", debug=False)
|
|
results["高级阈值法"] = threshold_ratio
|
|
|
|
# K均值聚类法
|
|
kmeans_ratio = advanced_analyze(image_path, method="kmeans", debug=False)
|
|
results["K均值聚类法"] = kmeans_ratio
|
|
|
|
# 梯度法
|
|
gradient_ratio = advanced_analyze(image_path, method="gradient", debug=False)
|
|
results["梯度法"] = gradient_ratio
|
|
|
|
# 灰色天空专用方法
|
|
gray_sky_ratio = analyze_gray_sky_ratio(image_path, debug=False)
|
|
results["灰色天空专用方法"] = gray_sky_ratio
|
|
|
|
# 显示结果比较
|
|
plt.figure(figsize=(10, 6))
|
|
|
|
# 显示原图
|
|
plt.subplot(121)
|
|
plt.title("原始图片")
|
|
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
|
plt.axis('off')
|
|
|
|
# 显示不同方法的结果比较
|
|
plt.subplot(122)
|
|
methods = list(results.keys())
|
|
ratios = [results[method] * 100 for method in methods] # 转换为百分比
|
|
|
|
# 绘制条形图
|
|
bars = plt.barh(methods, ratios, color='skyblue')
|
|
plt.xlabel('天空占比 (%)')
|
|
plt.title('不同方法的天空占比分析结果')
|
|
|
|
# 在条形图上添加具体数值
|
|
for i, bar in enumerate(bars):
|
|
plt.text(bar.get_width() + 1, bar.get_y() + bar.get_height()/2,
|
|
f'{ratios[i]:.2f}%', va='center')
|
|
|
|
plt.tight_layout()
|
|
|
|
# 保存结果
|
|
if save_result:
|
|
result_dir = "results"
|
|
os.makedirs(result_dir, exist_ok=True)
|
|
output_path = os.path.join(result_dir, f"{filename}_comparison.jpg")
|
|
plt.savefig(output_path, dpi=300, bbox_inches='tight')
|
|
print(f"比较结果已保存至: {output_path}")
|
|
|
|
plt.show()
|
|
|
|
return results
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description='天空区域占比分析')
|
|
parser.add_argument('image_path', help='图片路径')
|
|
parser.add_argument('--method', choices=['simple', 'advanced', 'gray_sky', 'compare'],
|
|
default='compare', help='分析方法')
|
|
parser.add_argument('--advanced_method', choices=['threshold', 'kmeans', 'gradient'],
|
|
default='threshold', help='高级分析的具体方法')
|
|
parser.add_argument('--debug', action='store_true', help='显示处理过程图像')
|
|
parser.add_argument('--save', action='store_true', help='保存处理结果图像')
|
|
args = parser.parse_args()
|
|
|
|
try:
|
|
if args.method == 'simple':
|
|
ratio = simple_analyze(args.image_path, debug=args.debug)
|
|
print(f"简单阈值法天空区域占比: {ratio:.2%}")
|
|
|
|
elif args.method == 'advanced':
|
|
ratio = advanced_analyze(args.image_path, method=args.advanced_method,
|
|
debug=args.debug, save_result=args.save)
|
|
print(f"使用{args.advanced_method}方法的高级分析天空区域占比: {ratio:.2%}")
|
|
|
|
elif args.method == 'gray_sky':
|
|
ratio = analyze_gray_sky_ratio(args.image_path, debug=args.debug, save_result=args.save)
|
|
print(f"灰色天空专用方法天空区域占比: {ratio:.2%}")
|
|
|
|
elif args.method == 'compare':
|
|
results = compare_methods(args.image_path, save_result=args.save)
|
|
print("各方法天空区域占比结果:")
|
|
for method, ratio in results.items():
|
|
print(f" - {method}: {ratio:.2%}")
|
|
|
|
except Exception as e:
|
|
print(f"错误: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
main() |