OpenCV如何提取图片中曲线

所属分类: 软件编程 / C 语言 阅读数: 67
收藏 0 赞 0 分享

简单介绍

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

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

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

代码

//寻找图像曲线上某个点的下一个点
bool findNextPoint(vector<Point> &_neighbor_points, Mat &_image, Point _inpoint, int flag, Point& _outpoint, int &_outflag)
{
 int i = flag;
 int count = 1;
 bool success = false;

 while (count <= 7)
 {
  Point tmppoint = _inpoint + _neighbor_points[i];
  if (tmppoint.x > 0 && tmppoint.y > 0 && tmppoint.x < _image.cols&&tmppoint.y < _image.rows)
  {
   if (_image.at<uchar>(tmppoint) == 255)
   {
    _outpoint = tmppoint;
    _outflag = i;
    success = true;
    _image.at<uchar>(tmppoint) = 0;
    break;
   }
  }
  if (count % 2)
  {
   i += count;
   if (i > 7)
   {
    i -= 8;
   }
  }
  else
  {
   i += -count;
   if (i < 0)
   {
    i += 8;
   }
  }
  count++;
 }
 return success;
}
//寻找图像上的第一个点
bool findFirstPoint(Mat &_inputimg, Point &_outputpoint)
{
 bool success = false;
 for (int i = 0; i < _inputimg.rows; i++)
 {
  uchar* data = _inputimg.ptr<uchar>(i);
  for (int j = 0; j < _inputimg.cols; j++)
  {
   if (data[j] == 255)
   {
    success = true;
    _outputpoint.x = j;
    _outputpoint.y = i;
    data[j] = 0;
    break;
   }
  }
  if (success)
   break;
 }
 return success;
}
//寻找曲线 
void findLines(Mat &_inputimg, vector<deque<Point>> &_outputlines)
{
 vector<Point> neighbor_points = { Point(-1,-1),Point(0,-1),Point(1,-1),Point(1,0),Point(1,1),Point(0,1),Point(-1,1),Point(-1,0) };
 Point first_point;
 while (findFirstPoint(_inputimg, first_point))
 {
  deque<Point> line;
  line.push_back(first_point);
  //由于第一个点不一定是线段的起始位置,双向找
  Point this_point = first_point;
  int this_flag = 0;
  Point next_point;
  int next_flag;
  while (findNextPoint(neighbor_points, _inputimg, 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 << "flag:" << this_flag << endl;
  while (findNextPoint(neighbor_points, _inputimg, 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(line);
  }
 }
}
//随机取色 用于画线的时候
Scalar random_color(RNG& _rng)
{
 int icolor = (unsigned)_rng;
 return Scalar(icolor & 0xFF, (icolor >> 8) & 0xFF, (icolor >> 16) & 0xFF);
}
int main()
{
 Mat image = imread("images\\2.bmp");
 Mat gray;
 cvtColor(image, gray, CV_BGR2GRAY);
 vector<deque<Point>> lines;
 findLines(gray, lines);
 cout << lines.size() << endl;
 //draw lines
 Mat draw_img = image.clone();
 RNG rng(123);
 Scalar color;
 for (int i = 0; i < lines.size(); i++)
 {
  color = random_color(rng);
  for (int j = 0; j < lines[i].size(); j++)
  {
   draw_img.at<Vec3b>(lines[i][j]) = Vec3b(color[0], color[1], color[2]);
  }
 }
 imshow("draw_img", draw_img);
 imwrite("images\\draw_img.bmp", draw_img);
 waitKey(0);
 system("pause");
 return 0;
}

结果

输入图像

结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

利用C语言来求最大连续子序列乘积的方法

这篇文章主要介绍了利用C语言来求最大连续子序列乘积的方法,基本的思路以外文中还附有相关ACM题目,需要的朋友可以参考下
收藏 0 赞 0 分享

用C语言判断一个二叉树是否为另一个的子结构

这篇文章主要介绍了用C语言判断一个二叉树是否为另一个的子结构,是数据结构学习当中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言实现的阶乘,排列和组合实例

这篇文章主要介绍了C语言实现的阶乘,排列和组合的方法,涉及C语言数学运算的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言查找数组里数字重复次数的方法

这篇文章主要介绍了C语言查找数组里数字重复次数的方法,涉及C语言针对数组的遍历与判断技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言简单实现计算字符个数的方法

这篇文章主要介绍了C语言简单实现计算字符个数的方法,涉及C语言针对字符串的简单遍历与判定技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

c实现linux下的数据库备份

本文给大家简单介绍下c实现linux下的数据库备份的方法和具体的源码,十分的实用,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

C++获得文件状态信息的方法

这篇文章主要介绍了C++获得文件状态信息的方法,包括文件状态信息、文件所在磁盘盘符、文件创建时间、访问时间及修改日期等,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言按关键字搜索文件夹中文件的方法

这篇文章主要介绍了C语言按关键字搜索文件夹中文件的方法,涉及C语言文件操作及字符串查找的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言之字符串模糊查询方法的实现

本篇文章主要为大家介绍字符串模糊查询的C语言程序编写方法,有需要的朋友可以参考下
收藏 0 赞 0 分享

C语言实现BMP转换JPG的方法

这篇文章主要介绍了C语言实现BMP转换JPG的方法,涉及C#图片格式转换的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多