I am going to process a region of pixels defined by RotatedRect in OpenCV. Although I know the rectangle center, size, and angle, I am not sure how to store all the x and y of t
Implementation with OpenCV
warpAffine
.
Mat getAffineTransformForRotatedRect(RotatedRect rr) {
float angle = rr.angle * M_PI / 180.0;
// angle += M_PI; // you may want rotate it upsidedown
float sinA = sin(angle), cosA = cos(angle);
float data[6] = {
cosA, sinA, rr.size.width/2.0f - cosA * rr.center.x - sinA * rr.center.y,
-sinA, cosA, rr.size.height/2.0f - cosA * rr.center.y + sinA * rr.center.x};
Mat rot_mat(2, 3, CV_32FC1, data);
return rot_mat.clone();
}
Mat getRotatedRectImg(const cv::Mat &mat, RotatedRect rr) {
Mat M, result;
M = getAffineTransformForRotatedRect(rr);
warpAffine(mat, result, M, rr.size, INTER_CUBIC);
return result;
}