寻找最小矩形边框--OpenCv

好久没有写博客了 

今天写一下比较常用的寻找矩形边框

        ////////////////////////////寻找最矩形边框//////////////////////////////////////////////////////////
        vector<vector<Point>>contours;
        vector<Vec4i>hierarchy;
        findContours(g_grayImage, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
        RotatedRect box;
        double area = 0;
        for (int i = 0; i < contours.size(); i++)
        {

            if (contourArea(contours[i]) > area)
            {
                box = minAreaRect(contours[i]);
                area = contourArea(contours[i]);
            }
        }
        Point2f vertex[4];
        box.points(vertex);
        for (int i = 0; i < 4; i++)
        {
            line(g_grayImage, vertex[i], vertex[(i + 1) % 4], Scalar(100, 200, 211), 2, LINE_AA);
        }
        imshow("框选", g_grayImage);

猜你喜欢

转载自www.cnblogs.com/Loving-Q/p/12061490.html