How to keep white background when using opencv warpaffine

I'm trying to rotate an 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)); } 

indicating the angle, source and image of the destination. The rotation works correctly as follows.

enter image description here

I want to make black areas white. I tried with

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

before calling rotate, but without changing the result. How can I achieve this?

Note. I am looking for a solution that achieves this while doing a rotation. obviously by making black areas white after rotation, this can be achieved.

+5
source share
1 answer

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

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

See the OpenCV API Documentation for more information.

+12
source

Source: https://habr.com/ru/post/1237488/


All Articles