c++ - OpenCV Mat rotation gets wrong result -
i want rotate image 90 degrees. code following:
int main(int argc, const char * argv[]) { mat img = imread("/users/chuanliu/desktop/src4/p00.jpg"); resize(img, img, size(1024, 683)); imwrite("/users/chuanliu/desktop/resize.jpg", img); mat dst; mat rot_mat = getrotationmatrix2d(point(img.cols / 2.0, img.rows / 2.0), 90, 1); warpaffine(img, dst, rot_mat, size(img.rows, img.cols)); imwrite("/users/chuanliu/desktop/roatation.jpg", dst); return 0; }
but result following:
before rotation:
after rotation:
it seems center of rotation has sth wrong. don't think set wrong center. there can tell me wrong?
the centre specified in terms of source image's dimensions point(img.cols / 2.0, img.rows / 2.0)
not rotating image swapping width , height in output size when calling warpaffine
:
size(img.rows, img.cols)
so looks might need specify centre in terms of output image coordinates; eg. point(rows/2, cols/2)
.
update:
nope, that's not solution. there simple , efficient method rotating image 90 degrees: using cv::transpose()
function:
int main() { cv::mat img = cv::imread("5syfi.jpg"); cv::mat img_rotated; cv::transpose(img, img_rotated); cv::imwrite("out.jpg", img_rotated); return 0; }
using combination of cv::transpose()
(to rotate) , cv::flip()
(to mirror vertically , horizontally) can perform rotations 90, 180 , 270 degrees.
using warpaffine()
more flexible, more expensive (ie. slower) calculate. if need rotate multiple of 90deg, use cv::transpose
. if need rotate arbitrary angle, use warpaffine/warpperspective
functions. @micka's answer gives great example of how that.
Comments
Post a Comment