7

Is it possible using the putText() method to draw text on a picture diagonally?

If not, beside using addWeighted() to blend two pictures together (where one of them is a text placed diagonally), is there any other option?

I'm trying to place a text watermark on a picture, my problem is that right now i'm using addWeighted() to blend a text drawn diagonally on a white background. Even with alpha 0.9, the white background changes the original picture.

I'm using OpenCV 2.4.9 with VC10. putText() method is part of CORE library on OpenCV.

Any ideas?

Thanks,

Alex

user1003302
  • 81
  • 1
  • 1
  • 3
  • You haven't given us any context at all. What library is this `putText` a member of? – Dai Sep 21 '14 at 07:40
  • 1
    @Dai, part of opencv's api. (core module in 2.4.9) – berak Sep 21 '14 at 07:49
  • 1
    You could use putText in an empty image, then rotate it diagonally, and finally add it to your original image. – sergioFC Sep 21 '14 at 08:15
  • 3
    putText doesn't have an option for drawing text at an angle, as far as I can tell. – Mats Petersson Sep 21 '14 at 08:16
  • However, it sounds (from what I can tell) that `addWeighted` has both `alpha` and `beta`, which should make it possible to get 100% of one image and, say, 10% or 70% of another - of course, where the text is, it SHOULD modify the image, right? – Mats Petersson Sep 21 '14 at 08:19
  • @sergioFC, but wouldn't that require creating a new image for it? which is the same as using addWeighted. – user1003302 Sep 21 '14 at 08:20
  • Yes, it would require creating a new image for it. If the text isn't black I think its a good idea to do that (put a text in a black -zero- Mat rotate and sum to your image). I don't understand why you need to use addWeighted. – sergioFC Sep 21 '14 at 08:29
  • @user1003302 You're welcome. I've written an example of that (you'll probably have to adapt the example to your case). – sergioFC Sep 21 '14 at 09:15

1 Answers1

13

Have a look at this example using the idea of my comment:

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

using namespace std;
using namespace cv;

/**
 * Rotate an image (source: http://opencv-code.com/quick-tips/how-to-rotate-image-in-opencv/)
 */
void rotate(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("lenna.png", CV_LOAD_IMAGE_COLOR);

    // Create and rotate the text
    Mat textImg = Mat::zeros(img.rows, img.cols, img.type());
    putText(textImg, "stackoverflow", Point(0, img.cols/2), FONT_HERSHEY_SIMPLEX, 2.0,Scalar(20,20,20),2);
    rotate(textImg, -45, textImg);

    // Sum the images (add the text to the original img)
    img= img+textImg;

    namedWindow("WaterMark", CV_WINDOW_AUTOSIZE);
    imshow("WaterMark", img);

    waitKey(0);
    return 0;
}

Result:

lenna Wattermark

sergioFC
  • 5,926
  • 3
  • 40
  • 54