Friday 26 August 2011

Text manipulations in OpenCV

Hello All,


This is regarding text operations we do in image processing using OpenCV.

All of the OpenCV users know the power of OpenCV regarding image processing operations but sometimes we need to some text manipulations operations in OpenCV. Some of the operations are as follows:
1) Text to be put on the image
2) Underline the text
3) Strike through the text
4) Wrap the text
5) Puting text in vertical direction like
O
P
E
N
C
V
6) Making text bold and italic.

These are the few operations for which i searched extensively on google to get some help but was not successful as OpenCV is more involved in image processing arena than playing with text.
So here I will tell the approach so that other people who try to get help can apply the approach and if they have better approach, then kindly inform me :)

PUTTING TEXT

OpenCV has a function name cvputtext() which helps in doing this operation. This is one of the simple operations as you just need to specify the corrdinates, font type and size of text in the input parameter list.
Format of using the function is :
cvPutText(CvArr* img, const char* text, CvPoint org, const CvFont* font, CvScalar color)
  • img – The image
  • text – The text string to be drawn
  • org – The bottom-left corner of the text string in the image
  • fontFace – The font type, one of FONT_HERSHEY_SIMPLEX , FONT_HERSHEY_PLAIN , FONT_HERSHEY_DUPLEX , FONT_HERSHEY_COMPLEX , FONT_HERSHEY_TRIPLEX , FONT_HERSHEY_COMPLEX_SMALL , FONT_HERSHEY_SCRIPT_SIMPLEX or FONT_HERSHEY_SCRIPT_COMPLEX ,
    where each of the font id’s can be combined with FONT_HERSHEY_ITALIC to get the slanted letters.
  • fontScale – The font scale factor that is multiplied by the font-specific base size
  • color – The text color
  • thickness – Thickness of the lines used to draw the text
  • lineType – The line type; see line for details
 Example:Check out this tutorial:
http://nashruddin.com/OpenCV_Examples_Part_2

2) UNDERLINE TEXT
Basic Algorithm to underline the text is as follows:
  • Get the text height and text width
  • Generate two points keeping in mind the text height and width. ( one point will be zeroth coordinate of height and width and othe rwill be zeroth point of height and max width point.
  • use cvLine to connect the points so as to get the text underlined.
NOTE: Make sure to take into account the font thickness into consideration.

3) STRIKE OUT TEXT
The algorithm is as follows 

  • Get the text height and text width.
  • Generate two points ( one point at the middle of height and at the starting of the text and othe rpoint at the end of the text and at half the text size.
  • Use cvLine to connect these two points. It will generate the effect of Underline.
 NOTE: Make sure to take the font size factor into account.

4) WRAP TEXT
I basically used some intrinsic functions of C++ to implement this.
Basic Algorithm is as follows:
  • Firstly find the text size which will have thew information about text size width. Depending on ratio of text width and image width, we can find out if we need to wrap the text or not.
  • If text width is larger than the image width, we need to wrap it and we can find out how many times we need to wrap it.
  • Depending on ratio of text width and image width, we can extract sub-string using C++ intrinsic substr() and put the text extracted using cvPutText.
  • Do it till all the text is wrapped on the image.
5) VERTICAL TEXT

This is one of the functionality which is interesting and involved some discussion with my colleagues. It involves uses of OpenCV with several intrinsocs of C++.
Basic Algorithm is as follows:
  • use strtok() to split the string into tokens. I have seen in some blogs and forum regarding not using strtok() stating memory issues but you can search for other alternatives which have been mentioned in forums.
  • For each token, use substr() to extract each character from the token carefully using the parameters of substr().
  • Use cvPuttext  according for putting each character obtained.
NOTE: Take care of the space which need to be placed between each token of string.

6) MAKING TEXT BOLD AND ITALIC 

This is easy as compared to other above mentioned functionality. Basically we can get the effect by just manipulating the cvInitfont() parameters.
cvInitFont() is defined as :
cvInitFont(CvFont* font, int fontFace, double hscale, double vscale, double shear=0, int thickness=1, int lineType=8)


For understanding on parameters,consult opencv willow-garage documentation 2.0.

In this article, I tried to tell the basic algorithmic approach. In case someone needs the code, just contact me and I can help in that regard.

4 comments:

  1. i have tiff files. It contain texts. i need to count no of words striked ou t and no of words underlined. can u please provide code and algorithm implemnt this. Is there any way to extract striked out and underlined words. I used opencv and tesseract , but i dont get striked words.

    ReplyDelete
  2. Can you share the code for Italic?

    ReplyDelete
  3. Can you share the code for Italic?

    ReplyDelete