num_pixel.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @Time : 2024/6/20 0020 下午 1:44
  4. # @Author : liudan
  5. # @File : 11111.py
  6. # @Software: pycharm
  7. #!/usr/bin/env python
  8. # -*- coding: utf-8 -*-
  9. # @Time : 2024/6/20 0020 下午 1:44
  10. # @Author : liudan
  11. # @File : 11111.py
  12. # @Software: pycharm
  13. import os
  14. import cv2
  15. import cv2 as cv
  16. import numpy as np
  17. import json
  18. import os
  19. THRESHOLD=0.15
  20. def otsu_binarize(image):
  21. gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  22. gray_image = cv2.equalizeHist(gray_image)
  23. # ret1, mask1 = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY +cv2.THRESH_OTSU)
  24. ret2, mask =cv2.threshold(gray_image,60, 255, cv2.THRESH_BINARY)
  25. # mask =mask1 & mask2
  26. kernel = np.ones((3, 3), np.uint8)
  27. mask = cv2.dilate(mask, kernel, iterations=3)
  28. mask = cv2.erode(mask, kernel, iterations=3)
  29. return mask
  30. def read_json_file(json_path):
  31. with open(json_path, 'r', encoding='utf-8') as f:
  32. data = json.load(f)
  33. coordinates_dict = {}
  34. for shape in data['shapes']:
  35. shape_name = shape['label']
  36. points = shape['points']
  37. rounded_points = [[int(round(x)), int(round(y))] for x, y in points]
  38. coordinates_dict[shape_name] = rounded_points
  39. return coordinates_dict
  40. def pixel_calculation(ref_image_dir, query_positive_image_dir, query_negative_image_dir, json_path):
  41. true_positives = 0
  42. false_positives = 0
  43. false_negatives = 0
  44. true_negatives = 0
  45. false_negatives_list = []
  46. false_positives_list = []
  47. for ref_file in os.listdir(ref_image_dir):
  48. ref_image_path = os.path.join(ref_image_dir, ref_file)
  49. ref_image = cv2.imread(ref_image_path)
  50. ostu_ref_image = otsu_binarize(ref_image)
  51. for query_file in os.listdir(query_positive_image_dir):
  52. query_image_path = os.path.join(query_positive_image_dir, query_file)
  53. query_image = cv2.imread(query_image_path)
  54. ostu_query_image = otsu_binarize(query_image)
  55. coordinates = read_json_file(json_path)
  56. num_pixel_list = []
  57. for _, points in coordinates.items():
  58. x1, y1 = points[0]
  59. x2, y2 = points[1]
  60. region_ref = ostu_ref_image[y1:y2, x1:x2]
  61. region_query = ostu_query_image[y1:y2, x1:x2]
  62. num_pixel_region_ref = round((np.sum(region_ref == 0) / (region_ref.shape[0] * region_ref.shape[1])),2)
  63. num_pixel_region_query = round((np.sum(region_query == 0) / (region_query.shape[0] * region_query.shape[1])),2)
  64. num_pixel_list.append(num_pixel_region_query)
  65. flag = any(element <= THRESHOLD for element in num_pixel_list)
  66. if not flag:
  67. true_positives += 1
  68. else:
  69. false_negatives += 1
  70. false_negatives_list.append((query_file, ref_file))
  71. # cv2.imwrite(os.path.join(query_positive_image_dir, f'{query_file}_img.jpg'),ostu_query_image)
  72. print(f"positive ={num_pixel_list}")
  73. for query_file in os.listdir(query_negative_image_dir):
  74. query_image_path = os.path.join(query_negative_image_dir, query_file)
  75. query_image = cv2.imread(query_image_path)
  76. ostu_query_image = otsu_binarize(query_image)
  77. # cv2.imwrite(os.path.join(query_negative_image_dir, '1.jpg'), ostu_query_image)
  78. coordinates = read_json_file(json_path)
  79. num_pixel_list = []
  80. for _, points in coordinates.items():
  81. x1, y1 = points[0]
  82. x2, y2 = points[1]
  83. region_ref = ostu_ref_image[y1:y2, x1:x2]
  84. region_query = ostu_query_image[y1:y2, x1:x2]
  85. num_pixel_region_ref = round((np.sum(region_ref == 0) / (region_ref.shape[0] * region_ref.shape[1])), 2)
  86. num_pixel_region_query = round((np.sum(region_query == 0) / (region_query.shape[0] * region_query.shape[1])), 2)
  87. num_pixel_list.append(num_pixel_region_query)
  88. flag = any(element <= THRESHOLD for element in num_pixel_list)
  89. if flag:
  90. true_negatives += 1
  91. else:
  92. false_positives += 1
  93. false_positives_list.append((query_file, ref_file))
  94. cv2.imwrite(os.path.join(query_negative_image_dir, f'{query_file}_img.jpg'), ostu_query_image)
  95. print(f"negtive ={num_pixel_list}")
  96. Accurary = (true_positives + true_negatives) / (true_positives + true_negatives + false_positives + false_negatives)
  97. # Precision = true_positives / (true_positives + false_positives)
  98. Recall = true_negatives / (true_negatives + false_positives)
  99. Precision = true_negatives / (true_negatives + false_negatives)
  100. # Recall = true_positives / (true_positives + false_negatives)
  101. F1_score = 2 * (Precision * Recall) / (Precision + Recall)
  102. print(false_negatives_list)
  103. print(false_positives_list)
  104. # print(file_name)
  105. print(f"Accurary:{Accurary: .4f}")
  106. print(f"Precision: {Precision:.4f}")
  107. print(f"Recall: {Recall:.4f}")
  108. print(f"F1 Score: {F1_score:.4f}")
  109. if __name__ == "__main__":
  110. # ref_image_dir = 'H:\\dataset\\region_samples\\positive_test_img\\R_ref'
  111. # query_positive_image_dir = 'H:\\dataset\\region_samples\\positive_test_img\\R_pos'
  112. # query_negative_image_dir = 'H:\\dataset\\region_samples\\positive_test_img\\R_neg'
  113. # json_path = 'H:\\dataset\\region_samples\\positive_test_img\\R_json\\2.json'
  114. ref_image_dir = 'H:\\dataset\\A_ref'
  115. query_positive_image_dir = 'H:\\dataset\\A'
  116. query_negative_image_dir = 'H:\\dataset\\C'
  117. json_path = 'H:\\dataset\\B\\image-024_R.json'
  118. pixel_calculation(ref_image_dir, query_positive_image_dir, query_negative_image_dir, json_path)