Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentationcomparison benchmark #9

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added latest version of segmentation comparison script
EStorvik committed Feb 14, 2023
commit 5a7d30beddc143aa27efc2163bef529baad937e6
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
import cv2
import darsia as da
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import seaborn as sns
import matplotlib
matplotlib.use('Agg')


def whole_img(segmentation_path,depth_map,plot_name,plot=False):
# Create segmentation object (It is necessary to provide the number of segmentations that you want to compare)
# If no colors are provided, default colors will be chosen. NOTE: Number of colors must match the number of segmentated images.
segmentationComparison = da.SegmentationComparison()


# Create the comparison array (Here as many segmentations as desirable can be provided)
comparison = segmentationComparison.compare_segmentations_binary_array(np.load(segmentation_path[0]),
np.load(segmentation_path[1]),
np.load(segmentation_path[2]),
np.load(segmentation_path[3]),
np.load(segmentation_path[4]))

# Create color palette
sns_palette = np.array(sns.color_palette())
gray = np.array([[0.3,0.3,0.3], [1, 1, 1]])
palette = np.append(sns_palette[:6],gray,axis=0)


# Image to be filled with colors depending on segmentation overlap
colored_comparison = np.zeros(comparison.shape[:2] + (3,),
dtype=np.uint8,)

# List of unique combinations
unique_combinations = [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0],
[0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]



# List of combinations including c2, c3, and c4 (the 1, 2, 3 is for the relative
# position in the presence lists, i.e., we will get all combinations of the form [x,1,1,1,x],
# it is of course only 4 different ones in this specific case, but this is a general function)
comb_c2_c3_c4 = segmentationComparison.get_combinations(1, 2, 3,
num_segmentations=5)


# Get bolean array of true-values for all combinations of c2, c3, and c4.
c_2_c3_c4_bool = np.zeros(comparison.shape[:2], dtype = bool)
for combination in comb_c2_c3_c4:
c_2_c3_c4_bool += np.all(comparison == combination, axis=2)

#-------------------------------------#
# Get bolean array of true-values for all combinations between two runs for c2-c3-c4.
spes_bool = np.zeros(comparison.shape[:2], dtype = bool)

comb_c2_c3 = segmentationComparison.get_combinations(1, 2,
num_segmentations=5)
for combination in comb_c2_c3:
spes_bool += np.all(comparison == combination, axis=2)


comb_c3_c4 = segmentationComparison.get_combinations(2, 3,
num_segmentations=5)

for combination in comb_c3_c4:
spes_bool += np.all(comparison == combination, axis=2)


comb_c2_c4 = segmentationComparison.get_combinations(1, 3,
num_segmentations=5)

for combination in comb_c2_c4:
spes_bool += np.all(comparison == combination, axis=2)
#-------------------------------------#


# Fill the colored image. Start by coloring all pixels that have any segmentation present at all with the light gray color
colored_comparison[np.any(comparison != [0, 0, 0, 0, 0], axis=2)] = (palette[7]*255).astype(np.uint8)

# Fill the colored image with the spesial combinations
colored_comparison[spes_bool] = (palette[5] * 255).astype(np.uint8)

# Color the unique combinations
for c, i in enumerate(unique_combinations):
colored_comparison[np.all(comparison == i, axis=2)] = (palette[c] * 255).astype(
np.uint8
)

# Color the combination where c2, c3 and c4 are included
colored_comparison[c_2_c3_c4_bool] = (palette[6] * 255).astype(np.uint8)

# NOTE: If several of these computations are to be done on images of the same size, save the depth_map and feed it to the color_fractions() method instead of depth_measurements. That will result in a HUGE time save.
( weighted_colors,
color_fractions,
colors,
total_color,
depth_map,
) = segmentationComparison.color_fractions(
colored_comparison,
colors=(palette*255).astype(np.uint8),
depth_map=depth_map,
width=2.8,
height=1.5,
)

if plot:
figure, axes = plt.subplots(figsize=(20,10))
# create legend paches
labels = ["C1", "C2", "C3", "C4", "C5", "spesial_comb","C2+C3+C4", "Other"]

for i in range(len(labels)):
labels[i] = labels[i] + " " + str(round(weighted_colors[i], 2))
patch = []
for i in range(8):
patch.append(mpatches.Patch(color=palette[i], label=labels[i]))


# Read baseline image for overlay plot
base = cv2.imread("E:/Git/fluidflower_benchmark_analysis/analysis/segmentationcomparison_new_method/211124_time082740_DSC00067.TIF")
# Resize image to same size as colored comparison
base = cv2.resize(base, (colored_comparison.shape[1], colored_comparison.shape[0]))
base = cv2.cvtColor(base, cv2.COLOR_BGR2RGB)

# Process the comparison image
processed_comparison_image = segmentationComparison._post_process_image(colored_comparison,unique_colors=colors,opacity=0.6,contour_thickness=10)

plt.imshow(base)
plt.imshow(processed_comparison_image)
plt.legend(handles=patch, bbox_to_anchor=(0.85, 1), loc=2, borderaxespad=0.0)

boxes = [np.array([[1.1, 0.6], [2.8, 0.0]]), # boxA
np.array([[0.0,1.2], [1.1, 0.0]]), # boxB*
np.array([[1.1,1.2], [2.8, 0.6]])] # boxD

colors = [[0.2,0.8,0.2],[1,1,1],[1,1,0]]
# for c,i in enumerate(boxes):
# _, roi_box = da.extractROI(da.Image(base,width=2.8,height=1.5), i, return_roi=True)
# x0,y0 = roi_box[1].start,roi_box[0].start
# width,height = roi_box[1].stop-roi_box[1].start,roi_box[0].stop-roi_box[0].start
# rec = plt.Rectangle((x0,y0), width, height,fill = False,linewidth=1.5,ls = "--",color = colors[c])
# axes.add_artist(rec)
plt.axis('off')
gs = plt.tight_layout()
plt.savefig(plot_name,dpi=100)

try:
return True,weighted_colors,total_color
except:
False,None

def zones(segmentation_path,depth_map,subregion):
# Create segmentation object (It is necessary to provide the number of segmentations that you want to compare)
# If no colors are provided, default colors will be chosen. NOTE: Number of colors must match the number of segmentated images.
segmentationComparison = da.SegmentationComparison()


# Create the comparison array (Here as many segmentations as desirable can be provided)
comparison = segmentationComparison.compare_segmentations_binary_array(da.extractROI(da.Image(np.load(segmentation_path[0]), width=2.8,height=1.5,color_space="GRAY"),subregion),
da.extractROI(da.Image(np.load(segmentation_path[1]), width=2.8,height=1.5,color_space="GRAY"),subregion),
da.extractROI(da.Image(np.load(segmentation_path[2]), width=2.8,height=1.5,color_space="GRAY"),subregion),
da.extractROI(da.Image(np.load(segmentation_path[3]), width=2.8,height=1.5,color_space="GRAY"),subregion),
da.extractROI(da.Image(np.load(segmentation_path[4]), width=2.8,height=1.5,color_space="GRAY"),subregion))
# Create color palette
sns_palette = np.array(sns.color_palette())
gray = np.array([[0.3,0.3,0.3], [1, 1, 1]])
palette = np.append(sns_palette[:6],gray,axis=0)


# Image to be filled with colors depending on segmentation overlap
colored_comparison = np.zeros(comparison.shape[:2] + (3,),
dtype=np.uint8,)

# List of unique combinations
unique_combinations = [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0],
[0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]



# List of combinations including c2, c3, and c4 (the 1, 2, 3 is for the relative
# position in the presence lists, i.e., we will get all combinations of the form [x,1,1,1,x],
# it is of course only 4 different ones in this specific case, but this is a general function)
comb_c2_c3_c4 = segmentationComparison.get_combinations(1, 2, 3,
num_segmentations=5)


# Get bolean array of true-values for all combinations of c2, c3, and c4.
c_2_c3_c4_bool = np.zeros(comparison.shape[:2], dtype = bool)
for combination in comb_c2_c3_c4:
c_2_c3_c4_bool += np.all(comparison == combination, axis=2)

#-------------------------------------#
# Get bolean array of true-values for all combinations between two runs for c2-c3-c4.
spes_bool = np.zeros(comparison.shape[:2], dtype = bool)

comb_c2_c3 = segmentationComparison.get_combinations(1, 2,
num_segmentations=5)
for combination in comb_c2_c3:
spes_bool += np.all(comparison == combination, axis=2)


comb_c3_c4 = segmentationComparison.get_combinations(2, 3,
num_segmentations=5)

for combination in comb_c3_c4:
spes_bool += np.all(comparison == combination, axis=2)


comb_c2_c4 = segmentationComparison.get_combinations(1, 3,
num_segmentations=5)

for combination in comb_c2_c4:
spes_bool += np.all(comparison == combination, axis=2)
#-------------------------------------#

# Fill the colored image. Start by coloring all pixels that have any segmentation present at all with the light gray color
colored_comparison[np.any(comparison != [0, 0, 0, 0, 0], axis=2)] = (palette[7]*255).astype(np.uint8)

# Fill the colored image with the spesial combinations
colored_comparison[spes_bool] = (palette[5] * 255).astype(np.uint8)

# Color the unique combinations
for c, i in enumerate(unique_combinations):
colored_comparison[np.all(comparison == i, axis=2)] = (palette[c] * 255).astype(
np.uint8
)

# Color the combination where c2, c3 and c4 are included
colored_comparison[c_2_c3_c4_bool] = (palette[6] * 255).astype(np.uint8)

# NOTE: If several of these computations are to be done on images of the same size, save the depth_map and feed it to the color_fractions() method instead of depth_measurements. That will result in a HUGE time save.
( weighted_colors,
color_fractions,
colors,
total_color,
depth_map,
) = segmentationComparison.color_fractions(
colored_comparison,
colors=(palette*255).astype(np.uint8),
depth_map=depth_map.img,
width=depth_map.width,
height=depth_map.height,
)


try:
return True,weighted_colors,total_color
except:
False,None