8

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.

Ruwanka De Silva
  • 3,555
  • 6
  • 35
  • 51

1 Answers1

15

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.

ajshort
  • 3,684
  • 5
  • 29
  • 43
  • what to do if you would like to discard all those pixels out of bounds, like including only those pixels inside of valid roi. Is there any method for that? – Pablo Gonzalez Nov 14 '18 at 07:02
  • @PabloGonzalez There is no method in the API for that, you'll have to write your own method to take those pixels out of whatever you're doing. – Tarun Uday Apr 16 '19 at 21:09