Ask Your Question

Revision history [back]

You can use the following method to resize the contour by keeping the contour center like the morphology operation.

void contourOffset(const Contour& src, Contour& dst,const cv::Point& offset) {
    dst.clear();
    dst.resize(src.size());
    for (int j = 0; j < src.size(); j++)
        dst[j] = src[j]+ offset;

}
void scaleContour(const Contour& src, Contour& dst, float scale)
{
    cv::Rect rct = cv::boundingRect(src);

    Contour dc_contour;
    cv::Point rct_offset(-rct.tl().x, -rct.tl().y);
    contourOffset(src, dc_contour,rct_offset);

    Contour dc_contour_scale(dc_contour.size());

    for (int i = 0; i < dc_contour.size(); i++)
        dc_contour_scale[i] = dc_contour[i] * scale;

    cv::Rect rct_scale = cv::boundingRect(dc_contour_scale);

    cv::Point offset((rct.width - rct_scale.width)/2, (rct.height - rct_scale.height)/2);
    offset -= rct_offset;
    dst.clear();
    dst.resize(dc_contour_scale.size());
    for (int i = 0; i < dc_contour_scale.size(); i++)
        dst[i] = dc_contour_scale[i] + offset;
}

void scaleContours(const Contours& src, Contours& dst, float scale)
{
    dst.clear();
    dst.resize(src.size());
    for (int i = 0; i < src.size(); i++)
        scaleContour(src[i], dst[i], scale);
}

In the sample below, the red contour is main contour and the red contour is scaled contour with a coefficient of 0.95. image description

You can use the following method to resize the contour by keeping the contour center like the morphology operation.

void contourOffset(const Contour& src, Contour& dst,const cv::Point& offset) {
    dst.clear();
    dst.resize(src.size());
    for (int j = 0; j < src.size(); j++)
        dst[j] = src[j]+ offset;

}
void scaleContour(const Contour& src, Contour& dst, float scale)
{
    cv::Rect rct = cv::boundingRect(src);

    Contour dc_contour;
    cv::Point rct_offset(-rct.tl().x, -rct.tl().y);
    contourOffset(src, dc_contour,rct_offset);

    Contour dc_contour_scale(dc_contour.size());

    for (int i = 0; i < dc_contour.size(); i++)
        dc_contour_scale[i] = dc_contour[i] * scale;

    cv::Rect rct_scale = cv::boundingRect(dc_contour_scale);

    cv::Point offset((rct.width - rct_scale.width)/2, (rct.height - rct_scale.height)/2);
    offset -= rct_offset;
    dst.clear();
    dst.resize(dc_contour_scale.size());
    for (int i = 0; i < dc_contour_scale.size(); i++)
        dst[i] = dc_contour_scale[i] + offset;
}

void scaleContours(const Contours& src, Contours& dst, float scale)
{
    dst.clear();
    dst.resize(src.size());
    for (int i = 0; i < src.size(); i++)
        scaleContour(src[i], dst[i], scale);
}
    void main(){
        std::vector<std::vector<cv::Point>> src,dst;
        scaleContours(src,dst,0.95);
     }

In the sample below, the red contour is main contour and the red contour is scaled contour with a coefficient of 0.95. image description

You can use the following method to resize the contour by keeping the contour center like the morphology operation.

void contourOffset(const Contour& src, Contour& dst,const cv::Point& offset) {
    dst.clear();
    dst.resize(src.size());
    for (int j = 0; j < src.size(); j++)
        dst[j] = src[j]+ offset;

}
void scaleContour(const Contour& src, Contour& dst, float scale)
{
    cv::Rect rct = cv::boundingRect(src);

    Contour dc_contour;
    cv::Point rct_offset(-rct.tl().x, -rct.tl().y);
    contourOffset(src, dc_contour,rct_offset);

    Contour dc_contour_scale(dc_contour.size());

    for (int i = 0; i < dc_contour.size(); i++)
        dc_contour_scale[i] = dc_contour[i] * scale;

    cv::Rect rct_scale = cv::boundingRect(dc_contour_scale);

    cv::Point offset((rct.width - rct_scale.width)/2, (rct.height - rct_scale.height)/2);
    offset -= rct_offset;
    dst.clear();
    dst.resize(dc_contour_scale.size());
    for (int i = 0; i < dc_contour_scale.size(); i++)
        dst[i] = dc_contour_scale[i] + offset;
}

void scaleContours(const Contours& src, Contours& dst, float scale)
{
    dst.clear();
    dst.resize(src.size());
    for (int i = 0; i < src.size(); i++)
        scaleContour(src[i], dst[i], scale);
}
    void main(){
        std::vector<std::vector<cv::Point>> src,dst;
        scaleContours(src,dst,0.95);
     }

In the sample below, the red green contour is main contour and the red contour is scaled contour with a coefficient of 0.95. image description

You can use the following method to resize the contour by keeping the contour center like the morphology operation.

void contourOffset(const Contour& src, Contour& dst,const cv::Point& offset) {
    dst.clear();
    dst.resize(src.size());
    for (int j = 0; j < src.size(); j++)
        dst[j] = src[j]+ offset;

}
void scaleContour(const Contour& src, Contour& dst, float scale)
{
    cv::Rect rct = cv::boundingRect(src);

    Contour dc_contour;
    cv::Point rct_offset(-rct.tl().x, -rct.tl().y);
    contourOffset(src, dc_contour,rct_offset);

    Contour dc_contour_scale(dc_contour.size());

    for (int i = 0; i < dc_contour.size(); i++)
        dc_contour_scale[i] = dc_contour[i] * scale;

    cv::Rect rct_scale = cv::boundingRect(dc_contour_scale);

    cv::Point offset((rct.width - rct_scale.width)/2, (rct.height - rct_scale.height)/2);
    offset -= rct_offset;
    dst.clear();
    dst.resize(dc_contour_scale.size());
    for (int i = 0; i < dc_contour_scale.size(); i++)
        dst[i] = dc_contour_scale[i] + offset;
}

void scaleContours(const Contours& src, Contours& dst, float scale)
{
    dst.clear();
    dst.resize(src.size());
    for (int i = 0; i < src.size(); i++)
        scaleContour(src[i], dst[i], scale);
}
    void main(){
        std::vector<std::vector<cv::Point>> src,dst;
        scaleContours(src,dst,0.95);
     }

In the sample below, the green contour is main contour and the red contour is scaled contour with a coefficient of 0.95. image description

You can use the following method to resize the contour by keeping the contour center like the morphology operation.

void contourOffset(const Contour& src, Contour& dst,const cv::Point& offset) {
    dst.clear();
    dst.resize(src.size());
    for (int j = 0; j < src.size(); j++)
        dst[j] = src[j]+ offset;

}
void scaleContour(const Contour& src, Contour& dst, float scale)
{
    cv::Rect rct = cv::boundingRect(src);

    Contour dc_contour;
    cv::Point rct_offset(-rct.tl().x, -rct.tl().y);
    contourOffset(src, dc_contour,rct_offset);

    Contour dc_contour_scale(dc_contour.size());

    for (int i = 0; i < dc_contour.size(); i++)
        dc_contour_scale[i] = dc_contour[i] * scale;

    cv::Rect rct_scale = cv::boundingRect(dc_contour_scale);

    cv::Point offset((rct.width - rct_scale.width)/2, (rct.height - rct_scale.height)/2);
    offset -= rct_offset;
    dst.clear();
    dst.resize(dc_contour_scale.size());
    for (int i = 0; i < dc_contour_scale.size(); i++)
        dst[i] = dc_contour_scale[i] + offset;
}

void scaleContours(const Contours& src, Contours& dst, float scale)
{
    dst.clear();
    dst.resize(src.size());
    for (int i = 0; i < src.size(); i++)
        scaleContour(src[i], dst[i], scale);
}
    void main(){
        std::vector<std::vector<cv::Point>> src,dst;
        scaleContours(src,dst,0.95);
     }

In the sample below, the green contour is main contour and the red contour is scaled contour with a coefficient of 0.95. image description

You can use the following method to resize the contour by keeping the contour center like the morphology operation.

 void contourOffset(const Contour& std::vector<cv::Point>& src, Contour& dst,const std::vector<cv::Point>& dst, const cv::Point& offset) {
    dst.clear();
    dst.resize(src.size());
    for (int j = 0; j < src.size(); j++)
        dst[j] = src[j]+ src[j] + offset;

}
void scaleContour(const Contour& std::vector<cv::Point>& src, Contour& std::vector<cv::Point>& dst, float scale)
{
    cv::Rect rct = cv::boundingRect(src);

    Contour std::vector<cv::Point> dc_contour;
    cv::Point rct_offset(-rct.tl().x, -rct.tl().y);
    contourOffset(src, dc_contour,rct_offset);

    Contour dc_contour, rct_offset);

    std::vector<cv::Point> dc_contour_scale(dc_contour.size());

    for (int i = 0; i < dc_contour.size(); i++)
        dc_contour_scale[i] = dc_contour[i] * scale;

    cv::Rect rct_scale = cv::boundingRect(dc_contour_scale);

    cv::Point offset((rct.width - rct_scale.width)/2, rct_scale.width) / 2, (rct.height - rct_scale.height)/2);
rct_scale.height) / 2);
    offset -= rct_offset;
    dst.clear();
    dst.resize(dc_contour_scale.size());
    for (int i = 0; i < dc_contour_scale.size(); i++)
        dst[i] = dc_contour_scale[i] + offset;
 }

void scaleContours(const Contours& std::vector<std::vector<cv::Point>>& src, Contours& std::vector<std::vector<cv::Point>>& dst, float scale)
{
    dst.clear();
    dst.resize(src.size());
    for (int i = 0; i < src.size(); i++)
        scaleContour(src[i], dst[i], scale);
}
     void main(){
         std::vector<std::vector<cv::Point>> src,dst;
         scaleContours(src,dst,0.95);
      }

In the sample below, the green contour is main contour and the red contour is scaled contour with a coefficient of 0.95. image description