How to store all the pixels within a RotatedRect to another matrix?

后端 未结 2 1381

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

2条回答
  •  -上瘾入骨i
    2021-02-11 07:09

    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;
    }
    

提交回复
热议问题