Ask Your Question
0

Mean of the distance transform

asked 2018-12-19 03:25:40 -0600

Esh gravatar image

I need to use openCV python to find the average width of the curve as attached in the image.

image description

I used distance transform to find the maximum width :

dist = cv2.distanceTransform(img, cv2.DIST_L2, 3)
mnv, mv, _, mp = cv2.minMaxLoc(dist)
print(mv * 2, mp)  # (half)width*2, pos

For the average width, i tried:

avgwid=2*np.mean(dist)

as well as:

avgwid= 0.5*(mnv+mv)

However, both options are giving me the incorrect results. What is the correct way to find average width of the curve ? A solution in python would be highly appreciated. Thanks.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-12-19 04:03:52 -0600

VxW gravatar image

updated 2018-12-19 05:16:50 -0600

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()
edit flag offensive delete link more

Comments

Hi

I have already written the program to find the coordinates of the skeleton. I just do not know how to proceed from there. I am an absolute beginner in image processing as well as programming. I am having a tough time trying to put it all together.

Esh gravatar imageEsh ( 2018-12-19 04:10:47 -0600 )edit

any example python implementation of using the skeleton and distance transform to find width at every points would solve all my problems.

Esh gravatar imageEsh ( 2018-12-19 04:12:10 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-12-19 03:25:40 -0600

Seen: 1,362 times

Last updated: Dec 19 '18