Repository: opencv/opencv
File: modules/imgproc/src/shapedescr.cpp (C++)

Code before:
    align_T_ori_f32(1,2) = si*shift_x - co*shift_y;

    Matx23f ori_T_align_f32;
    ori_T_align_f32(0,0) = co;
    ori_T_align_f32(0,1) = -si;
    ori_T_align_f32(0,2) = shift_x;
    ori_T_align_f32(1,0) = si;
    ori_T_align_f32(1,1) = co;
    ori_T_align_f32(1,2) = shift_y;

    std::vector<Point2f> closest_pts_list;
    for (int i = 0; i < n; i++)
    {
        Point2f p = is_float ? ptsf[i] : Point2f((float)ptsi[i].x, (float)ptsi[i].y);
        Matx31f pmat;
        pmat(0,0) = p.x;
        pmat(1,0) = p.y;
        pmat(2,0) = 1;

        Mat X_align = Mat(align_T_ori_f32) * pmat;
        Point2f closest_pt;
        solveFast(semi_major, semi_minor, Point2f(X_align.at<float>(0,0), X_align.at<float>(1,0)), closest_pt);

        pmat(0,0) = closest_pt.x;
        pmat(1,0) = closest_pt.y;
        Mat closest_pt_ori = Mat(ori_T_align_f32) * pmat;
        closest_pts_list.push_back(Point2f(closest_pt_ori.at<float>(0,0), closest_pt_ori.at<float>(1,0)));
    }

    cv::Mat(closest_pts_list).convertTo(closest_pts, CV_32F);
}

////////////////////////////////////////////// C API ///////////////////////////////////////////

CV_IMPL int
cvMinEnclosingCircle( const void* array, CvPoint2D32f * _center, float *_radius )
{
    cv::AutoBuffer<double> abuf;
    cv::Mat points = cv::cvarrToMat(array, false, false, 0, &abuf);
    cv::Point2f center;
    float radius;

    cv::minEnclosingCircle(points, center, radius);
    if(_center)
        *_center = cvPoint2D32f(center);
    if(_radius)
        *_radius = radius;
    return 1;
}

static void

Review comment (suggestion):
Keep using Matx instead of Mat, since Matx is instantly allocated on stack and for Matx there is also overloaded '*' opereator. Here you basically initialize quite big structure Mat and allocate the Mat data in heap for each single point, which is very bad.

Code after:
    align_T_ori_f32(1,2) = si*shift_x - co*shift_y;

    Matx23f ori_T_align_f32;
    ori_T_align_f32(0,0) = co;
    ori_T_align_f32(0,1) = -si;
    ori_T_align_f32(0,2) = shift_x;
    ori_T_align_f32(1,0) = si;
    ori_T_align_f32(1,1) = co;
    ori_T_align_f32(1,2) = shift_y;

    std::vector<Point2f> closest_pts_list;
    closest_pts_list.reserve(n);
    for (int i = 0; i < n; i++)
    {
        Point2f p = is_float ? ptsf[i] : Point2f((float)ptsi[i].x, (float)ptsi[i].y);
        Matx31f pmat(p.x, p.y, 1);

        Matx21f X_align = align_T_ori_f32 * pmat;
        Point2f closest_pt;
        solveFast(semi_major, semi_minor, Point2f(X_align(0,0), X_align(1,0)), closest_pt);

        pmat(0,0) = closest_pt.x;
        pmat(1,0) = closest_pt.y;
        Matx21f closest_pt_ori = ori_T_align_f32 * pmat;
        closest_pts_list.push_back(Point2f(closest_pt_ori(0,0), closest_pt_ori(1,0)));
    }

    cv::Mat(closest_pts_list).convertTo(closest_pts, CV_32F);
}

////////////////////////////////////////////// C API ///////////////////////////////////////////

CV_IMPL int
cvMinEnclosingCircle( const void* array, CvPoint2D32f * _center, float *_radius )
{
    cv::AutoBuffer<double> abuf;
    cv::Mat points = cv::cvarrToMat(array, false, false, 0, &abuf);
    cv::Point2f center;
    float radius;

    cv::minEnclosingCircle(points, center, radius);
    if(_center)
        *_center = cvPoint2D32f(center);
    if(_radius)
        *_radius = radius;
    return 1;
}

static void
icvMemCopy( double **buf1, double **buf2, double **buf3, int *b_max )
{