Ask Your Question
0

How to put rotated text in OpenCV ?

asked 2019-07-15 21:52:23 -0600

open_ranger gravatar image

OpenCV accepts

 void putText( InputOutputArray img, const String& text, Point org,   int fontFace, double fontScale, Scalar color,int thickness = 1, int lineType = LINE_8,  bool bottomLeftOrigin = false );

which put any text at point origin in a horizontal left to right line but how do we construct a

void putRotatedText()

which take all previous inputs plus int angle

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2019-07-16 00:15:35 -0600

berak gravatar image

updated 2019-07-16 00:18:05 -0600

it's not meant to do that ;(

but you can draw your text into a small (empty) image, rotate() it, and draw that into your original image.

in general, putText() is a simple debug facility, if you need more luxury, use a real text rendering engine (quite off-topic for a computer-vision library)

edit flag offensive delete link more

Comments

Dont know any text rendering engine yet so have to deal with openCV for now.

open_ranger gravatar imageopen_ranger ( 2019-07-17 19:53:52 -0600 )edit
0

answered 2019-07-20 08:22:03 -0600

supra56 gravatar image

Try this:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

void PutRotateText(cv::Mat& src, double angle, cv::Mat& dst)
{
    int _len = std::max(src.cols, src.rows);
    cv::Point2f pt(_len/2., _len/2.);
    cv::Mat r = cv::getRotationMatrix2D(pt, angle, 1.0);
    cv::warpAffine(src, dst, r, cv::Size(_len, _len));
}

int main() {
    Mat _img = imread("messi.jpg", CV_LOAD_IMAGE_COLOR);

    // Create and rotate the red text
    Mat messi_Imgage = Mat::zeros(_img.rows, _img.cols, _img.type());
    putText(messi_Imgage, "lionel messi", Point(0, _img.cols/2), FONT_HERSHEY_SIMPLEX, 2.0,Scalar(255,0,0),2);
    PutRotateText(messi_Imgage, -30, messi_Imgage);

    img+= messi_Imgage;

    namedWindow("Rotate Text", CV_WINDOW_AUTOSIZE);
    imshow("Rotate Text", _img);

    waitKey(0);
    return 0;
}
edit flag offensive delete link more

Comments

this way the text will be written as watermark,if the background happen to be white you wont be able to see anything.Also the point of rotation needs to be linked to the text insertion

open_ranger gravatar imageopen_ranger ( 2019-07-24 04:10:16 -0600 )edit
0

answered 2019-07-17 19:51:56 -0600

open_ranger gravatar image

updated 2019-07-17 19:55:04 -0600

Here is my version of putRotateText()

void putRotateText(InputOutputArray img, const String& text, Point org,double angle,int fontFace, double fontScale, Scalar color, int thicknes ){

    Mat text_image_color = Mat::zeros(static_cast<int>(img.rows * 1.2), (img.cols*1.2), img.type());
    Mat text_image_white = Mat::zeros(static_cast<int>(img.rows * 1.2), (img.cols*1.2), imgl.type());
    putText(text_image_color , text, org, frontFace, frontScale, color                       ,thickness);
    putText(text_image_white, text, org, frontFace, frontScale, Scalar(255,255,255),thickness);
    Mat rotationMatrix = cv::getRotationMatrix2D(orgr, angle, 1.0);//couter clockwise rotation
    warpAffine(text_image_color, text_image_color, rotationMatrix,Size(img.width,img.height));
    warpAffine(text_image_white, text_image_white, rotationMatrix,Size(img.width,imgheight));
    img-=text_image_white;
    img+=text_image_color;
}

anyone any suggestion?

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-07-15 21:52:23 -0600

Seen: 12,124 times

Last updated: Jul 20 '19