var_detection.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @Time : 2024/6/19 0019 下午 12:24
  4. # @Author : liudan
  5. # @File : var_detection.py
  6. # @Software: pycharm
  7. import os
  8. import cv2
  9. from PIL import Image
  10. import numpy as np
  11. import random
  12. # 计算两张图片的方差
  13. def calculate_variance(image):
  14. gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  15. variance = np.var(gray_image)
  16. mean = np.mean(image)
  17. variance = variance / mean
  18. return variance
  19. # 计算两张图的方差差异,ref_image为模板图,query_image为查询图
  20. def calculate_variance_difference(ref_image, query_image):
  21. variance1= calculate_variance(ref_image)
  22. variance2 = calculate_variance(query_image)
  23. variance_diff = abs(variance1 - variance2)
  24. max_variance = max(variance1, variance2)
  25. normalized_diff = variance_diff / max_variance if max_variance != 0 else 0
  26. # diff = np.abs(image1.astype(float) - image2.astype(float)).var()
  27. return normalized_diff
  28. # 计算Recall等,positive_folder正样本文件夹,negative_folder负样本文件夹,ref_image_dir模板图文件夹
  29. def detection(positive_folder, negative_folder, ref_image_dir):
  30. filename = os.listdir(ref_image_dir)
  31. image_files = [f for f in filename if f.endswith(('.jpg', '.jpeg', '.png', '.JPG'))]
  32. image_file =random.choice(image_files) # 随机获得一张模板图
  33. ref_image_path = os.path.join(ref_image_dir,image_file)
  34. # ref_image_path = 'H:\\dataset\\region_samples\\positive_test_img\\A1\\image165246-002_A.jpg'
  35. ref_image = cv2.imread(ref_image_path) # 读取模板图
  36. print(f"{image_file}\n")
  37. true_positives = 0
  38. false_positives = 0
  39. false_negatives = 0
  40. true_negatives = 0
  41. threshold_score = 0.9
  42. # 在正样本中计算Recall等
  43. positive_false = []
  44. for file in os.listdir(positive_folder):
  45. query_image_path = os.path.join(positive_folder, file) # 查询图,来自正样本
  46. query_image = cv2.imread(query_image_path)
  47. variance_score = calculate_variance_difference(ref_image,query_image) # 计算模板图与查询图方差差异
  48. if variance_score < threshold_score:
  49. true_positives += 1
  50. else:
  51. false_negatives += 1
  52. positive_false.append(file)
  53. print(positive_false)
  54. # 在负样本中计算Recall等
  55. negative_false =[]
  56. for file in os.listdir(negative_folder):
  57. query_image_path = os.path.join(negative_folder, file) # 查询图,来自负样本
  58. query_image = cv2.imread(query_image_path)
  59. variance_score = calculate_variance_difference(ref_image,query_image)
  60. if variance_score > threshold_score:
  61. true_negatives += 1
  62. else:
  63. false_positives += 1
  64. negative_false.append((file,variance_score))
  65. print(negative_false)
  66. Accurary = (true_positives + true_negatives) / (true_positives + true_negatives + false_positives + false_negatives)
  67. Precision = true_positives / (true_positives + false_positives)
  68. Recall = true_positives / (true_positives + false_negatives)
  69. F1_score = 2 * (Precision * Recall) / (Precision + Recall)
  70. print(f"Accurary:{Accurary: .2f}")
  71. print(f"Precision: {Precision:.2f}")
  72. print(f"Recall: {Recall:.2f}")
  73. print(f"F1 Score: {F1_score:.2f}")
  74. if __name__ == "__main__":
  75. positive_folder = './data/624_test/positive/Y'
  76. negative_folder = './data/624_test/negative/Y'
  77. ref_image_dir = './data/624_test/positive/Y'
  78. detection(positive_folder, negative_folder, ref_image_dir)