#!/bin/bash if [ "$#" -ne 2 ]; then echo "Usage: $0 " exit 1 fi get_coordinates() { local file=$1 local coordinates coordinates=$(awk '/left/ {left=$2} /top/ {top=$2} /right/ {right=$2} /bottom/ {bottom=$2} END {print left, top, right, bottom}' "$file") echo $coordinates } # 计算矩形重叠度 calculate_overlap() { local x1=$1 y1=$2 x2=$3 y2=$4 local x3=$5 y3=$6 x4=$7 y4=$8 local overlap_left overlap_right overlap_top overlap_bottom local overlap_width overlap_height overlap_area local area_A area_B union_area jaccard_similarity local overlap_ratio_A overlap_ratio_B # 计算重叠区域的边界 overlap_left=$(echo "scale=2; if ($x1 > $x3) $x1 else $x3" | bc) overlap_right=$(echo "scale=2; if ($x2 < $x4) $x2 else $x4" | bc) overlap_bottom=$(echo "scale=2; if ($y1 > $y3) $y1 else $y3" | bc) overlap_top=$(echo "scale=2; if ($y2 < $y4) $y2 else $y4" | bc) #echo "Debug: overlap_left=$overlap_left, overlap_right=$overlap_right, overlap_bottom=$overlap_bottom, overlap_top=$overlap_top" # 计算重叠区域的面积 if [ "$(echo "$overlap_left < $overlap_right" | bc)" -eq 1 ] && [ "$(echo "$overlap_bottom < $overlap_top" | bc)" -eq 1 ]; then overlap_width=$(echo "$overlap_right - $overlap_left" | bc) overlap_height=$(echo "$overlap_top - $overlap_bottom" | bc) overlap_area=$(echo "$overlap_width * $overlap_height" | bc) else overlap_area=0 fi #echo "Debug: overlap_width=$overlap_width, overlap_height=$overlap_height, overlap_area=$overlap_area" # 计算矩形 A 和 B 的面积 area_A=$(echo "scale=2; ($x2 - $x1) * ($y2 - $y1)" | bc) area_B=$(echo "scale=2; ($x4 - $x3) * ($y4 - $y3)" | bc) #echo "Debug: area_A=$area_A, area_B=$area_B" # 计算重叠比例 overlap_ratio_A=$(echo "scale=2; if ($area_A > 0) $overlap_area / $area_A else 0" | bc) overlap_ratio_B=$(echo "scale=2; if ($area_B > 0) $overlap_area / $area_B else 0" | bc) #echo "Debug: overlap_ratio_A=$overlap_ratio_A, overlap_ratio_B=$overlap_ratio_B" # 计算 Jaccard 相似度 union_area=$(echo "scale=2; $area_A + $area_B - $overlap_area" | bc) jaccard_similarity=$(echo "scale=2; if ($union_area > 0) $overlap_area / $union_area else 0" | bc) #echo "Debug: union_area=$union_area, jaccard_similarity=$jaccard_similarity" # 输出结果 printf "The area of overlapping region: %.2f\n" "$overlap_area" printf "The area of rectangle A: %.2f\n" "$area_A" printf "The area of rectangle B: %.2f\n" "$area_B" printf "Overlap ratio (rectangle A): %.2f\n" "$overlap_ratio_A" printf "Overlap ratio (rectangle B): %.2f\n" "$overlap_ratio_B" printf "Jaccard Similarity: %.2f\n" "$jaccard_similarity" # 判断 Jaccard 相似度 if [ "$(echo "$jaccard_similarity >= 0.95" | bc)" -eq 1 ]; then echo "NNsample test Pass" return 0 else echo "NNsample test Fail" return 1 fi } file1=$1 file2=$2 coords1=$(get_coordinates "$file1") coords2=$(get_coordinates "$file2") read -r left1 top1 right1 bottom1 <<< "$coords1" read -r left2 top2 right2 bottom2 <<< "$coords2" echo "Coordinates from file 1: left=$left1, top=$top1, right=$right1, bottom=$bottom1" echo "Coordinates from file 2: left=$left2, top=$top2, right=$right2, bottom=$bottom2" calculate_overlap "$left1" "$top1" "$right1" "$bottom1" "$left2" "$top2" "$right2" "$bottom2"