c++ - 在 cv::Mat 中从 cv::Point 的 vector 转换轮廓

标签 c++ opencv image-processing data-structures contour

假设我的目标是从初始图像中提取轮廓。我使用 OpenCV 执行了这个操作:

cv::Mat gray, edges;
cv::cvtColor(image, gray, CV_BGRA2GRAY);
cv::Canny(gray, edges, 100, 300, 3);

std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(edges, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

现在轮廓包含在点数组中。我想重建一个与初始图像具有相同尺寸的 cv::Mat 结构,以便通过将其叠加到图像上来强调轮廓。

特别是,我对立即绘制轮廓不感兴趣。我将执行以下操作:

  • 提取轮廓
  • 扩张轮廓
  • 将轮廓叠加到图像上(就像在边缘锐化中所做的那样)

因此,轮廓必须是与输入图像大小相同的矩阵。

我该怎么做?

提前致谢。

最佳答案

你可以重建一个矩阵

Mat image_contours_grayscale = Mat::zeros(gray.size(),gray.type());
Scalar color(255);
drawContours( image_contours_grayscale, contours,-1,color,1);

我不确定 drawContours 是否适用于灰度图像,但如果不行,您可以试试这个

Mat image_contours_color = Mat::zeros(image.size(),image.type());
Scalar color(255,255,255);
drawContours( image_contours_color, contours,-1,color,1);
Mat image_contours_grayscale;
cv::cvtColor(image_contours_color, image_contours_grayscale, CV_BGRA2GRAY);

image_contours_grayscale 应该是灰度图像,全黑,轮廓为白色。

让我知道这是否有效,我没有机会测试这个解决方案!

关于c++ - 在 cv::Mat 中从 cv::Point 的 vector 转换轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18122857/

相关文章:

c++ - 内存中的 DLL 大小和硬盘上的大小

c++ - 默认继承访问说明符

c++ - 该程序已退出,代码为 0 (0x0)。 C++

java - 从图像识别并裁剪衣服

image-processing - 从编码图像和视频中提取 DCT 系数

c++ - 我的 C++ 代码无法处理(一点点)快速数据流量

c++ - 覆盖派生类中的 protected 变量

opencv - 我下载了用于处理的 OpenCV 库,但无法运行示例

python - Python + OpenCV-将图像复制到某个目录

c++ - 从哪里获得用于面部表情识别的 SVM 训练数据?