# How to draw contour around text string

Hi All,

I am trying to implement simple contour around text:

Init picture

I want to get the following:

I found contours around letters using Imgproc.findContours function but I don't have idea what need do in next step.

edit retag close merge delete

Perhaps a convex hull with an offset may help?

( 2017-10-19 07:03:07 -0600 )edit

Sort by ยป oldest newest most voted

The comment by Sanchopanza is right, convexHull() is effective.

And the interpolation can make the boundary more wavy: link text.

And here is the code, I hope this can help you.

By the way I guess the white spaces around the image was caused by using plt.savefig() to save the image, so I just remove them manually.

import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import splprep, splev

# binarization
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, img_bw = cv2.threshold(img_gray, 120, 255, cv2.THRESH_BINARY)

# find contours of letters
img_t, contours, hierarchy = cv2.findContours(img_bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# combine all the [[y, x]] from all contours into one.
cnt_together = []
for i in contours:
for j in i.tolist():
cnt_together.append(j)
cnt_together = np.array(cnt_together)

hull = cv2.convexHull(cnt_together)
hull = np.array([np.squeeze(hull).tolist()])

t = []
hull = hull[0]

img_bw_cp = img_bw.copy()

# referred from https://stackoverflow.com/questions/31464345/fitting-a-closed-curve-to-a-set-of-points
tck, u = splprep(hull.T, u=None, s=0.0, per=1)
u_new = np.linspace(u.min(), u.max(), 1000)
x_new, y_new = splev(u_new, tck, der=0)

hull_smoothed = np.vstack((x_new, y_new)).T

plt.figure(1)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.plot(hull_smoothed[:, 0], hull_smoothed[:, 1], color="white", linewidth=5)
plt.plot(hull[:, 0], hull[:, 1], 'ro')

plt.show()


And here is the effect image:

I hope these can help to inspire you.

more

well done !

( 2017-10-19 08:29:38 -0600 )edit

( 2017-10-19 08:32:23 -0600 )edit

Maybe do you have idea how I can resolved the next cases:

1. How can I do contour more more wavy ?
2. How to add offset between contour and text. I know that drawContours has offset parameter but it doesn't work correctly.
( 2017-10-19 08:34:51 -0600 )edit

try to map the contour to a new coordinate system (0,0 as the center of contour) and expand it by a constant factor. After that plot it with offset-Point in drawContour-function. to make it more wavy seems to be more complicated

( 2017-10-19 09:32:10 -0600 )edit
( 2017-10-19 10:56:54 -0600 )edit

Thanks guys. Offset is good now.
Need last thing - How can I make contour more wavy?

( 2017-10-20 04:38:18 -0600 )edit

Hi, maybe this can help you link text though it's not good enough for this question, and I've updated my answer.

( 2017-10-21 07:04:37 -0600 )edit

you can try using this function

( 2017-10-21 15:13:34 -0600 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2017-10-19 06:32:37 -0600

Seen: 3,177 times

Last updated: Oct 21 '17