前言
有时候需要将某一些点生成闭合的二值图像。记录一下。
// 轮廓点个数
int nrCurvePoints = curContour.nr;
// 轮廓点
DIM2DL* curvePoints = curContour.pts;std::vector<cv::Point> points; // 轮廓点集合
for (int cntPoint = 0; cntPoint < nrCurvePoints; ++cntPoint)
{int x = curvePoints[cntPoint].x;int y = curvePoints[cntPoint].y;points.emplace_back(cv::Point(x, y));
}// 轮廓点转Mat
cv::Mat contourImg = cv::Mat::zeros(cv::Size(usW, usH), CV_8UC1);
std::vector<std::vector<cv::Point>> contoursCV;
contoursCV.push_back(points);
cv::fillPoly(contourImg, contoursCV, cv::Scalar(255));// 保存至io文件夹
std::string savePath(str_ws_US + "io/inContour_flag" + std::to_string(curFlag) + "_index" + std::to_string(index) + ".jpg");
cv::imwrite(savePath, contourImg);