OpenCV:曲线的检测与提取-0

2025-06-02 17:00:38

1、基本思想:给出一个带有曲线像素宽度为1的二值图像;①遍历图像像素,找到第一个白色的点;②在此点的八邻域内找到下一个白色的点;③将找到的点的灰度值置为0;保存找到点的坐标;④八邻域内没有找到白色点,则表示一端搜索完成;原图如下:

OpenCV:曲线的检测与提取-0

3、程序如下://寻找二值图像上的第一个点//maskImg二值图像,标记//outPoint输出查找的点bool findFirstPoint(Mat &maskImg, Point &outPoint){ bool success = false; for (int i = 0; i < maskImg.rows; i++) { unsigned char *pData = (unsigned char*)(maskImg.data+i*maskImg.step); for (int j = 0; j < maskImg.cols; j++) { if (pData[j] == 255)//找到第一个白色的点 { success = true; outPoint.x = j; outPoint.y = i; pData[j] = 0;//将此点像素设为0 break; } } if (success) break; } return success;}

OpenCV:曲线的检测与提取-0

5、//寻找曲线 :void findLines(Mat &binaryImg, vector<deque<Point媪青怍牙>> &outLines){ //八邻域 vector<Point> neighborPtVec; neighborPtVec.push_back(Point(-1,-1)); neighborPtVec.push_back(Point(0,-1)); neighborPtVec.push_back(Point(1,-1)); neighborPtVec.push_back(Point(1,0)); neighborPtVec.push_back(Point(1,1)); neighborPtVec.push_back(Point(0,1)); neighborPtVec.push_back(Point(-1,1)); neighborPtVec.push_back(Point(-1,0)); Point firstPt; while (findFirstPoint(binaryImg, firstPt)) { deque<Point> line;//点的队列 line.push_back(firstPt);//存储第一个点 //由于第一个点不一定是线段的起始位置, //因此两个方向都要查找 Point currPt = firstPt;//当前点 int currFlag = 0;//标志 Point nextPt;//下一个点 int nextFlag;//下一点的标志 while (findNextPoint(neighborPtVec, binaryImg, currPt, currFlag, nextPt, nextFlag))//一端 { line.push_back(nextPt);//压入队列 currPt = nextPt; currFlag = nextFlag; } //找另一端 currPt = firstPt; currFlag = 0; while (findNextPoint(neighborPtVec, binaryImg, currPt, currFlag, nextPt, nextFlag)) { line.push_front(nextPt); currPt = nextPt; currFlag = nextFlag;//邻域与中心像素的位置 } if (line.size() > 10) { outLines.push_back(line); } }}

OpenCV:曲线的检测与提取-0OpenCV:曲线的检测与提取-0
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢