从cv::Mat中的cv::Point的矢量转换轮廓
Converting contours from vector of cv::Point in cv::Mat
假设我的目标是从初始图像中提取轮廓。我使用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_grascale应该是灰度图像,全部为黑色,轮廓为白色。
让我知道这是否有效,我没有机会测试这个解决方案!
相关文章:
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 将CHW格式的浮点向量转换为cv::Mat
- 将 cv::mat 转换为 QImage
- 将 cv::Mat 转换为 std::vector 的通用函数
- 将 opencv 从 2.4.13 转换为 3.3.1 时由 cv::resize() 引起的运行错误
- 如何将 cv::Mat 转换为 vtkImageData?
- 如何将 vtkImageData 转换为 cv::Mat?
- Qt-工作线程崩溃时将cv::Mat转换为QImage
- 将 cv::Mat 转换为无符号的 int 像素
- 如何在不复制数据的情况下将 cv::Mat 转换为 2d 标准::矢量
- 转换 cv::mat 转换为 std::矢量<double>并发症
- 如何将屏幕截图位图转换为 cv::Mat
- 如何从 cv::Mat (C++) 转换为 tf.placeholder(C++ Boost.Python)
- CV :: MAT如何从/转换为GLM :: MAT4
- 无法将参数 8 从 'int' 转换为 'cv::HOGDescriptor::HistogramNormType'
- 如何转换 cv:垫子的类型
- 将CV :: MAT转换为STD :: vector而无需复制
- 如何将atl :: cimage转换为cv :: mat
- 将非类非数组 prvalue 转换为符合 cv 条件的类型是否合法?
- 修复 cv::转换点从均匀错误