Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hi,

to find the width of a contour for each position is not so an easy task but there exists approaches.

You can do it as you suggested with distance transform but you also need the skeleton of it to get the witdh. Have e look at this discussion for more details

Another solution was proposed by Boris Epshtein et al. (see here) namely a Stroke Width Transform (SWT) . A Python - OpenCV implementation can be found here

Best

Hi,

to find the width of a contour for each position is not so an easy task but there exists exist approaches.

You can do it as you suggested with distance transform but you also need the skeleton of it to get the witdh. Have e look at this discussion for more details

Another solution was proposed by Boris Epshtein et al. (see here) namely a Stroke Width Transform (SWT) . A Python - OpenCV implementation can be found here

Best

Hi,

to find the width of a contour for each position is not so an easy task but there exist approaches.

You can do it as you suggested with distance transform but you also need the skeleton of it to get the witdh. Have e look at this discussion for more details

Another solution was proposed by Boris Epshtein et al. (see here) namely a Stroke Width Transform (SWT) . A Python - OpenCV implementation can be found here

Best

Edit:

ok because it's near to chrismas you can try this:

import cv2
import matplotlib.pyplot as plt
from skimage.morphology import  medial_axis
import numpy as np

# start calulcation
gray_image = cv2.imread(r'path,..', 0)
ret,thresh = cv2.threshold(gray_image,200,255,cv2.THRESH_BINARY)

skeleton, distance = medial_axis(thresh, return_distance=True)

med_dist = distance * skeleton
width_dist = (med_dist)*2
average_width = np.mean(width_dist[skeleton])
print("average width: ", average_width)

fig, axes = plt.subplots(3,1, figsize=(8, 8), sharex=True, sharey=True)
ax = axes.ravel()

ax[0].imshow(thresh, cmap=plt.cm.gray, interpolation='nearest')
ax[0].set_title('original')
ax[0].axis('off')

ax[1].imshow(med_dist, cmap='magma', interpolation='nearest')
ax[1].contour(thresh, [0.5], colors='w')
ax[1].set_title('medial_axis')
ax[1].axis('off')

ax[2].imshow(skeleton, cmap=plt.cm.gray, interpolation='nearest')
ax[2].set_title('skeletonize')
ax[2].axis('off')

fig.tight_layout()
plt.show()