|
|
import cv2 |
|
|
import numpy as np |
|
|
|
|
|
|
|
|
|
|
|
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])], |
|
|
} |
|
|
|