Skip to content

Instantly share code, notes, and snippets.

@azarnyx
Last active August 19, 2022 11:12
Show Gist options
  • Save azarnyx/68877d9b815d7ecd8df7185863c9b568 to your computer and use it in GitHub Desktop.
Save azarnyx/68877d9b815d7ecd8df7185863c9b568 to your computer and use it in GitHub Desktop.
...
img_rgb = skimage.io.imread(fnamei)
# 1. RGB to HED
img_hed = rgb2hed(img_rgb)
img = img_hed[:, :, 1]
# 2. Create mask using threshold
mask = (img > 0.05)
# 3. Delete small objects and holes
mask = morphology.remove_small_objects(mask, 40000)
mask = morphology.remove_small_holes(mask, 40000)
# 4. Detect labels and properties of the region
labels = measure.label(mask)
props = measure.regionprops(labels, img_hed)[0]
# 6. Find an angle of segmented region
angle = props.orientation
angle = periodic(angle, anglep)
anglep = angle
# 7. Draw angle and region
axis_min_length = props.axis_minor_length
axis_maj_length = props.axis_major_length
y0, x0 = props.centroid
x1 = x0 + math.cos(angle) * 0.5 * axis_min_length
y1 = y0 - math.sin(angle) * 0.5 * axis_min_length
x2 = x0 - math.sin(angle) * 0.5 * axis_maj_length
y2 = y0 - math.cos(angle) * 0.5 * axis_maj_length
li.append(angle)
# 7.1 Draw axis of a region
fig, ax = plt.subplots()
ax.plot((x0, x1), (y0, y1), '--b', linewidth=2.5)
ax.plot((x0, x2), (y0, y2), '--b', linewidth=2.5)
ax.plot(x0, y0, '.g', markersize=15)
# 7.2 Draw segmented region
alpha = 0.4
clr = [46/225., 1., 48/225.]
for c in range(3):
img_rgb[:, :, c] = np.where(mask == 1,
img_rgb[:, :, c] *
(1 - alpha) + alpha * clr[c] * 255,
img_rgb[:, :, c])
ax.imshow(img_rgb)
# 7.3 Draw a contour of a region
contours = measure.find_contours(mask, 0.5)
for contour in contours:
ax.plot(contour[:, 1], contour[:, 0], linewidth=2, color=[46/225., 1., 48/225.])
plt.axis('off')
plt.savefig('../pics/skimage_red_hed_countour/'+fnamei.split('/')[-1])
plt.clf()
plt.close('all')
...
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment