#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2024/6/20 0020 下午 1:44 # @Author : liudan # @File : 11111.py # @Software: pycharm #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2024/6/20 0020 下午 1:44 # @Author : liudan # @File : 11111.py # @Software: pycharm import os import cv2 import cv2 as cv import numpy as np import json import os THRESHOLD=0.15 def otsu_binarize(image): gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray_image = cv2.equalizeHist(gray_image) # ret1, mask1 = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY +cv2.THRESH_OTSU) ret2, mask =cv2.threshold(gray_image,60, 255, cv2.THRESH_BINARY) # mask =mask1 & mask2 kernel = np.ones((3, 3), np.uint8) mask = cv2.dilate(mask, kernel, iterations=3) mask = cv2.erode(mask, kernel, iterations=3) return mask def read_json_file(json_path): with open(json_path, 'r', encoding='utf-8') as f: data = json.load(f) coordinates_dict = {} for shape in data['shapes']: shape_name = shape['label'] points = shape['points'] rounded_points = [[int(round(x)), int(round(y))] for x, y in points] coordinates_dict[shape_name] = rounded_points return coordinates_dict def pixel_calculation(ref_image_dir, query_positive_image_dir, query_negative_image_dir, json_path): true_positives = 0 false_positives = 0 false_negatives = 0 true_negatives = 0 false_negatives_list = [] false_positives_list = [] for ref_file in os.listdir(ref_image_dir): ref_image_path = os.path.join(ref_image_dir, ref_file) ref_image = cv2.imread(ref_image_path) ostu_ref_image = otsu_binarize(ref_image) for query_file in os.listdir(query_positive_image_dir): query_image_path = os.path.join(query_positive_image_dir, query_file) query_image = cv2.imread(query_image_path) ostu_query_image = otsu_binarize(query_image) coordinates = read_json_file(json_path) num_pixel_list = [] for _, points in coordinates.items(): x1, y1 = points[0] x2, y2 = points[1] region_ref = ostu_ref_image[y1:y2, x1:x2] region_query = ostu_query_image[y1:y2, x1:x2] num_pixel_region_ref = round((np.sum(region_ref == 0) / (region_ref.shape[0] * region_ref.shape[1])),2) num_pixel_region_query = round((np.sum(region_query == 0) / (region_query.shape[0] * region_query.shape[1])),2) num_pixel_list.append(num_pixel_region_query) flag = any(element <= THRESHOLD for element in num_pixel_list) if not flag: true_positives += 1 else: false_negatives += 1 false_negatives_list.append((query_file, ref_file)) # cv2.imwrite(os.path.join(query_positive_image_dir, f'{query_file}_img.jpg'),ostu_query_image) print(f"positive ={num_pixel_list}") for query_file in os.listdir(query_negative_image_dir): query_image_path = os.path.join(query_negative_image_dir, query_file) query_image = cv2.imread(query_image_path) ostu_query_image = otsu_binarize(query_image) # cv2.imwrite(os.path.join(query_negative_image_dir, '1.jpg'), ostu_query_image) coordinates = read_json_file(json_path) num_pixel_list = [] for _, points in coordinates.items(): x1, y1 = points[0] x2, y2 = points[1] region_ref = ostu_ref_image[y1:y2, x1:x2] region_query = ostu_query_image[y1:y2, x1:x2] num_pixel_region_ref = round((np.sum(region_ref == 0) / (region_ref.shape[0] * region_ref.shape[1])), 2) num_pixel_region_query = round((np.sum(region_query == 0) / (region_query.shape[0] * region_query.shape[1])), 2) num_pixel_list.append(num_pixel_region_query) flag = any(element <= THRESHOLD for element in num_pixel_list) if flag: true_negatives += 1 else: false_positives += 1 false_positives_list.append((query_file, ref_file)) cv2.imwrite(os.path.join(query_negative_image_dir, f'{query_file}_img.jpg'), ostu_query_image) print(f"negtive ={num_pixel_list}") Accurary = (true_positives + true_negatives) / (true_positives + true_negatives + false_positives + false_negatives) # Precision = true_positives / (true_positives + false_positives) Recall = true_negatives / (true_negatives + false_positives) Precision = true_negatives / (true_negatives + false_negatives) # Recall = true_positives / (true_positives + false_negatives) F1_score = 2 * (Precision * Recall) / (Precision + Recall) print(false_negatives_list) print(false_positives_list) # print(file_name) print(f"Accurary:{Accurary: .4f}") print(f"Precision: {Precision:.4f}") print(f"Recall: {Recall:.4f}") print(f"F1 Score: {F1_score:.4f}") if __name__ == "__main__": # ref_image_dir = 'H:\\dataset\\region_samples\\positive_test_img\\R_ref' # query_positive_image_dir = 'H:\\dataset\\region_samples\\positive_test_img\\R_pos' # query_negative_image_dir = 'H:\\dataset\\region_samples\\positive_test_img\\R_neg' # json_path = 'H:\\dataset\\region_samples\\positive_test_img\\R_json\\2.json' ref_image_dir = 'H:\\dataset\\A_ref' query_positive_image_dir = 'H:\\dataset\\A' query_negative_image_dir = 'H:\\dataset\\C' json_path = 'H:\\dataset\\B\\image-024_R.json' pixel_calculation(ref_image_dir, query_positive_image_dir, query_negative_image_dir, json_path)