[原文档链接]https://www.jianshu.com/p/33728ba5827d
[先码上 轮廓线间距离]
项目要求
寻找下面这幅图片之中两条白线之间的距离
得到每个点到对面线段的最小距离
求解平均距离
求解最中心距离
思路:
1.灰度后去干扰
2.利用labels提取两边轮廓
3.输出结果存储YLM
后期待解决
1.标签labels疑问
2.距离提取问题
代码一:[图片读取]
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
Mat src, gray_src;
src = imread("C:\\Users\\13658\\Desktop\\图片素材\\线条.jpg");
if (!src.data)
{
printf("can not find image file..\n");
return -1;
}
代码二:[连通域处理]
cvtColor(src, gray_src, COLOR_BGR2GRAY);
//阈值处理消去噪点,也可以高斯模糊处理
threshold(gray_src, gray_src, 200, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
//提取连通域
Mat labels;
connectedComponents(gray_src, labels, 8, CV_16U);
Mat result(gray_src.size(),CV_32FC1,Scalar::all(0));
for (int i=0;i<=1;i++)
{
Mat mask1 = labels == 1 + i;//提取标签信息,当满足条件返回255,不满足返回0
Mat mask2 = labels == 1 + (1 - i);
Mat masknot;
bitwise_not(mask1, masknot);
namedWindow("mask1", CV_WINDOW_AUTOSIZE);
imshow("mask1", mask1);//右
namedWindow("mask2", CV_WINDOW_AUTOSIZE);
imshow("mask2",mask2);//左
Mat dist;
distanceTransform(masknot, dist, DIST_L2, CV_8U);
dist.copyTo(result, mask2);//核心,直接提取距离信息
}
FileStorage fs("distCtr.yml", FileStorage::WRITE);
fs << "Image" << result;
fs.release();
namedWindow("src",CV_WINDOW_AUTOSIZE);
imshow("src",src);
waitKey(0);
return 0;
}
[mask1 mask2 ]
[新应用填充与反填充]