OpenCV怎样提取图片中曲线

  介绍

这篇文章主要介绍OpenCV怎样提取图片中曲线,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

<强>简单介绍

? ?在实际的应用中,我们常常需要对图像中的曲线进行描述,处理,这个曲线可以是轮廓,骨架或者其他。可以用deque描述曲线,接下来简单介绍下如何从图片中搜索这些曲线并保存。

? ?首先,输入的图片是一张二值图片(白色为曲线),其中包含的曲线宽度为1像素的(如果曲线不是1像素的先提取其骨架),遍历寻找图像中第一个白色的点,然后从这个点开始延伸寻找曲线。注意,第一个找到的点不一定是曲线的端点,因此应该分别向两边寻找相邻的点,因此双端队列会好一些。每找到一个点,将其保存双端队列而后置黑(防止重复寻找)。搜索到一个没有相邻点的点,表示一端搜索完成。

? ?值得注意的一点是,我在写搜寻相邻点的时候,会首先搜寻此点与上一个点相邻位置相对的位置,如果没有,则分别搜索向两边搜索。这样的好处是可以减少寻找的次数,而且当有相交的曲线时,能连接到我们一般认为的曲线。

代码

//寻找图像曲线上某个点的下一个点   bool  findNextPoint (vector,, _neighbor_points,, Mat , _image,, Point  _inpoint,, int 国旗,Point&, _outpoint,, int , _outflag)   {   ,int 小姐:=,国旗;   ,int  count =, 1;   ,bool  success =,假;      ,while  (count  & lt;=, 7)   ,{   Point 才能;tmppoint =, _inpoint  +, _neighbor_points[我];   if 才能;(tmppoint.x 祝辞,0,,,,tmppoint.y 祝辞,0,,,,tmppoint.x  & lt;, _image.cols&, tmppoint.y  & lt;, _image.rows)   {才能   ,,if  (_image.at (tmppoint),==, 255)   ,,{   ,,,_outpoint =, tmppoint;   ,,,_outflag =,我;   ,,,success =,真的;   ,,,_image.at (tmppoint),=, 0;   ,,,休息;   ,,}   ,,}   if 才能;(count  %, 2)   {才能   ,,小姐:+=,计数;   ,,if (小姐:祝辞,7)   ,,{   ,,,小姐:-=,8;   ,,}   ,,}   其他的才能   {才能   ,,小姐:+=,计数;   ,,if (小姐:& lt;, 0)   ,,{   ,,,小姐:+=,8;   ,,}   ,,}   数才能+ +;   ,}   ,return 成功;   }//寻找图像上的第一个点   bool  findFirstPoint (Mat 和_inputimg, Point , _outputpoint)   {   ,bool  success =,假;   ,for  (int 小姐:=,0;,小姐:& lt;, _inputimg.rows;,我+ +)   ,{   uchar *,才能data =, _inputimg.ptr(我);   for 才能;(int  j =, 0;, j  & lt;, _inputimg.cols;, j + +)   {才能   ,,if (数据[j],==, 255)   ,,{   ,,,success =,真的;   ,,,_outputpoint.x =, j。   ,,,_outputpoint.y =,我;   ,,,数据[j],=, 0;   ,,,休息;   ,,}   ,,}   if 才能;(成功)   ,才能打破;   ,}   ,return 成功;   }//寻找曲线,   void  findLines (Mat 和_inputimg, vector祝辞,,_outputlines)   {   ,vector neighbor_points =,{,点(1,1)点(0,1),点(1,1)点(1,0),点(1,1)点(0,1),点(1,1)点(1,0),};   ,Point  first_point;   ,while  (findFirstPoint (_inputimg, first_point))   ,{   deque<才能;Point>,行;   line.push_back才能(first_point);//由才能于第一个点不一定是线段的起始位置,双向找   Point 才能;this_point =, first_point;   int 才能;this_flag =, 0;   Point 才能;next_point;   int 才能;next_flag;   while 才能;(findNextPoint (_inputimg, neighbor_points,还以为,this_point, this_flag,, next_point,, next_flag))   {才能   ,,line.push_back (next_point);   ,,this_point =, next_point;   ,,this_flag =, next_flag;   ,,}//才能找另一边   时间=this_point 才能;first_point;   时间=this_flag 才能;0;//才能cout  & lt; & lt;,“旗:“,& lt; & lt;, this_flag  & lt; & lt;, endl;   while 才能;(findNextPoint (_inputimg, neighbor_points,还以为,this_point, this_flag,, next_point,, next_flag))   {才能   ,,line.push_front (next_point);   ,,this_point =, next_point;   ,,this_flag =, next_flag;   ,,}   if 才能;(line.size(),祝辞,10)   {才能   ,,_outputlines.push_back(线);   ,,}   ,}   }//随机取色,用于画线的时候   Scalar  random_color (RNG&, _rng)   {   ,int  icolor =, (unsigned) _rng;   ,return 标量(icolor ,, 0 xff,, (icolor 在祝辞,8),,,0 xff,, (icolor 在祝辞,16),,,0 xff);   }   int  main ()   {   ,Mat  image =, imread (“\ \ 2. bmp"图像);   ,Mat 灰色;   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

OpenCV怎样提取图片中曲线