Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain white background when using opencv warpaffine

I'm trying to rotate image using

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(src.cols, src.rows));
}

by giving angle, source and destination image. Rotation works correctly as follows.

enter image description here

I want to make black areas white. I have tried with

cv::Mat dst = cv::Mat::ones(src.cols, src.rows, src.type());

before calling rotate, but no change in result. How can I achieve this?

Note: I am looking for solution which achieve this while doing the rotation. obviously by making black areas white after the rotation this can be achieved.

like image 231
Ruwanka Madhushan Avatar asked Dec 06 '15 06:12

Ruwanka Madhushan


1 Answers

You will want to use the borderMode and borderValue arguments of the warpAffine function to accomlish this. By setting the mode to BORDER_CONSTANT it will use a constant value for border pixels (i.e. outside the image), and you can set the value to the constant value you want to use (i.e. white). It would look something like:

cv::warpAffine(src, dst, r,
               cv::Size(src.cols, src.rows),
               cv::INTER_LINEAR,
               cv::BORDER_CONSTANT,
               cv::Scalar(255, 255, 255));

For more details see the OpenCV API Documentation.

like image 81
ajshort Avatar answered Sep 23 '22 20:09

ajshort