I did sketalization
of images using morphology module of skimage
.
The output seems quite cool but I want the output to somehow be over my threshold/binary output so that I can confirm whether the output is accurate or not. I tried achieving this using PIL but in vain.

- 81
- 5
1 Answers
To keep things simple make a copy of the original image
result = img.copy()
Outline the skeleton as follows:
result[skeleton == 255] = (255,255,255)
Displaying result
should give the expected output
UPDATE:
I have provided complete solution using OpenCV:
img = cv2.imread('object.jpg')
# convert to grayscale
g = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# inverse binary image
th = cv2.threshold(g,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1]
# obtain skeleton
sk = cv2.ximgproc.thinning(th, None, 1)
# create copy of original image and superimpose the skeleton
result = img.copy()
result[sk == 255] = (255,255,255)
Result for an additional image shared recently:
Problems:
There are some problem as to why it doesn't work with your code:
Your
img
is read as grayscale. The snippetresult[sk == 255] = (255,255,255)
expectesresult
to be 3-channel RGB/BGR image hence you face the value error.Another thing I noted is, using
imread
fromskimage
reads the image infloat
data type with pixel range 0 to 1.0. Later when you useskeleton = medial_axis(canny).astype(np.uint8)
convertsskeleton
toint
data type but also restricts pixel range from 0 to 1. As a result, even the portion expected to be in white is seen in black

- 20,118
- 13
- 80
- 87
-
`result = img.copy() result[skeleton == 255] = (255, 255, 255) plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB)) plt.show()` – quest1001 Jun 04 '22 at 10:43
-
im getting this error->. `--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_33/3508954884.py in
29 30 result = img.copy() ---> 31 result[skeleton == 255] = (255,255,255) 32 plt.imshow((result)) 33 plt.show() ValueError: NumPy boolean array indexing assignment cannot assign 3 input values to the 0 output values where the mask is true ` – quest1001 Jun 04 '22 at 10:44 -
an update i change the code to -> `result = img.copy() result[skeleton==255] = (225,)` the output image does not show the skeleton over it T_T – quest1001 Jun 05 '22 at 19:10
-
and on doing `result = img.copy() result[skeleton==0] = (255,)` only skeleton apppears T_T – quest1001 Jun 05 '22 at 19:13
-
Thank you so very much Jeru. You've helped a lot _ / \ _ – quest1001 Jun 06 '22 at 07:18