File size: 2,115 Bytes
036e7c4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import cv2
import numpy as np
# from shared_queue import image_queue
def read_image(file_bytes):
np_img = np.frombuffer(file_bytes, np.uint8)
img = cv2.imdecode(np_img, cv2.IMREAD_COLOR)
return img
def combine_mask(image, mask, label2color={1: (255, 255, 0), 2: (0, 255, 255)}, alpha=0.1):
image = to_color(image)
image = cv2.resize(image, mask.shape)
mask_image = np.zeros_like(image)
for label, color in label2color.items():
mask_image[mask == label] = color
mask_image = cv2.addWeighted(image, 1 - alpha, mask_image, alpha, 0)
return mask_image
def to_color(image):
if len(image.shape) == 3 and image.shape[-1] == 3:
return image
return cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
def to_gray(image):
if len(image.shape) == 3 and image.shape[-1] == 3:
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return image
def prob_process(prob_list):
prob = prob_list[0]
max_idx = np.argmax(prob)
min_idx = np.argmin(prob)
prob[max_idx] += prob[min_idx]
prob[min_idx] = 0
if max_idx == 0:
return 1 - prob[max_idx]
elif max_idx ==1:
return prob[max_idx]
else:
return 1 + prob[max_idx]
def combine_prob_jsw(prob, jsw_m, jsw_mm):
processed_prob = prob_process(prob)
temp = np.array([processed_prob, jsw_m, jsw_mm])
if temp.any():
temp = np.hstack(temp)
else:
temp = temp.flatten()
return temp
def scale_coordinates(links, original_size, mask_size):
scale_factor = original_size / mask_size
scaled_links = [(int(x * scale_factor), int(y * scale_factor)) for x, y in links]
return scaled_links
def get_annotations(probabilites):
OSTEOPYTE_LEVELS = {
0: "Definite osteophytes",
1: "No osteophytes",
2: "Possible osteophytes",
}
JNS_LEVELS = {
0: "Definite JSN",
1: "Mild JSN",
2: "No JSN",
3: "Severe JSN",
}
return {
"osteophyte": OSTEOPYTE_LEVELS[np.argmax(probabilites[5:8])],
"jsn": JNS_LEVELS[np.argmax(probabilites[8:12])],
}
|