Ask Your Question
0

How to draw contour around text string

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

psedoykin gravatar image

updated 2017-10-19 06:36:47 -0600

Hi All,

I am trying to implement simple contour around text:

Init picture image description

I want to get the following:

image description

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

edit retag flag offensive close merge delete

Comments

Perhaps a convex hull with an offset may help?

Sanchopanza gravatar imageSanchopanza ( 2017-10-19 07:03:07 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-10-19 08:11:39 -0600

moHe gravatar image

updated 2017-10-21 07:07:47 -0600

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


img = cv2.imread(r'./test_string_for_contour.png')

# 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: image description

I hope these can help to inspire you.

edit flag offensive delete link more

Comments

well done !

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

Thanks for answer.

psedoykin gravatar imagepsedoykin ( 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.
psedoykin gravatar imagepsedoykin ( 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

Sanchopanza gravatar imageSanchopanza ( 2017-10-19 09:32:10 -0600 )edit
sturkmen gravatar imagesturkmen ( 2017-10-19 10:56:54 -0600 )edit

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

psedoykin gravatar imagepsedoykin ( 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.

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

you can try using this function

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

Question Tools

1 follower

Stats

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

Seen: 3,684 times

Last updated: Oct 21 '17