OpenCV – 使用findContours()查找图片轮廓线,并将轮廓线坐标点输出
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:OpenCV – 使用findContours()查找图片轮廓线,并将轮廓线坐标点输出
原文链接:https://www.stubbornhuang.com/441/
发布于:2019年11月18日 22:32:55
修改于:2019年11月18日 22:32:55

1 示例代码
使用findContours()查找图片轮廓线,并将轮廓线坐标点输出,访问二维vector,使用迭代器和下标两种方式。
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <math.h>
using namespace cv;
using namespace std;
int main()
{
//载入原始图,且必须以二值图模式载入
Mat M=imread("E:/A.bmp",0);
imshow("原始图",M);
waitKey(500); //等待5000ms后窗口自动关闭
//初始化结果图
Mat dstImage=Mat::zeros(M.rows,M.cols,CV_8UC3);
//M提取阈值小于250的部分
M=M<250;
imshow("阈值",M);
waitKey(50);
//定义轮廓和层次结构
vector<vector<Point>>contours;
vector<Vec4i>hierarchy;
findContours(M,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_NONE);
//迭代器输出
/*for (vector<vector<Point>>::iterator it=contours.begin();it!=contours.end();++it)
{
for (vector<Point>::iterator inner_it=it->begin();inner_it!=it->end();++inner_it)
{
cout<<*inner_it<<endl;
}
}
*/
//下标输出
for (int i=0;i<contours.size();i++)
{
for (int j=0;j<contours[i].size();j++)
{
cout<<contours[i][j].x<<" "<<contours[i][j].y<<endl;
ofstream f;
f.open("E:/坐标轮廓线.txt",ios::out|ios::app);
f<<contours[i][j].x<<" "<<contours[i][j].y<<endl;
}
}
//遍历顶层轮廓,以随机颜色绘制出每个连接组件颜色
int index=0;
for (;index>=0;index=hierarchy[index][0])
{
Scalar color(rand()%255,rand()%255,rand()%255);
drawContours(dstImage,contours,index,color,1,8,hierarchy);
}
imshow("轮廓图",dstImage);
waitKey(5000); //等待5000ms后窗口自动关闭
getchar();
}
当前分类随机文章推荐
- OpenCV - 打开摄像头并对摄像头获取的每一帧图像进行Canny算子边缘化提取操作 阅读3413次,点赞0次
- OpenCV - Mat与lplImage和CvMat的相互转换 阅读3056次,点赞0次
- OpenCV - linux上编译出现undefined reference to cv::dnn::experimental_dnn错误 阅读548次,点赞0次
- OpenCV - 静态图片人脸检测和摄像头人脸检测 阅读2553次,点赞0次
- OpenCV - linux上编译出现undefined reference to `cv::VideoCapture::VideoCapture()'错误 阅读634次,点赞0次
- OpenCV - 将图片转换为深度学习模型输入格式,BGR通道转RGB,图片归一化,HWC转CHW 阅读2076次,点赞0次
- OpenCV - cv::Mat转换为unsigned char*数组或者float*数组,unsigned char*数组或者float*数组转换为cv::Mat 阅读1950次,点赞0次
- Python – 解决opencv-python使用cv2.imwrite()保存中文路径图片失败的问题 阅读180次,点赞0次
- OpenCV - 读取一张图片显示,并将其重写为灰度图 阅读3710次,点赞0次
- OpenCV - 将OpenCV视频帧绘制到Win32窗口或者MFC控件上的CvvImage类,包括老版IplImage与新版Mat的绘制 阅读835次,点赞0次
全站随机文章推荐
- 资源分享 - Jim Blinn's Corner - Notation, Notation, Notation 英文高清PDF下载 阅读1355次,点赞0次
- Pytorch - nn.Transformer、nn.TransformerEncoderLayer、nn.TransformerEncoder、nn.TransformerDecoder、nn.TransformerDecoder参数详解 阅读116次,点赞0次
- C++ - 字节数组byte[]或者unsigned char[]与long的相互转换 阅读419次,点赞0次
- 资源分享 - Practical Linear Algebra - A Geometry Toolbox , Fourth Edition 英文高清PDF下载 阅读571次,点赞0次
- WordPress - wp_login_url函数详解 阅读710次,点赞0次
- Modern OpenGL - 与着色器Shader相关的API函数总结以及如何创建、编译与使用Shader 阅读177次,点赞0次
- 资源分享 - ShaderX3 - Advanced Rendering with DirectX and OpenGL 英文高清PDF下载 阅读1504次,点赞0次
- Visual Studio - 借助远程Linux服务器环境在Visual Studio中编写和远程调试Linux C++程序 阅读502次,点赞0次
- Unity - Color32[]转IntPtr 阅读2088次,点赞0次
- Github - 使用新的Personal Access Token进行仓库认证 阅读1194次,点赞0次
评论
150