OpenCV-获取图像中直线上的数据

简介: OpenCV-获取图像中直线上的数据

需求说明

      在对图像进行处理时,经常会有这类需求:客户想要提取出图像中某条直线或者ROI区域内的感兴趣数据,进行重点关注。该需求在图像检测领域尤其常见。ROI区域一般搭配Rect即可完成提取,直线数据的提取没有现成的函数,需要自行实现。


      当直线为纵向或者横向时,比较简单,只需要从起点到终点提取该行或者列的数据即可;但是直线若为斜向的,则需要从起点出发,向终点方向逐个像素提取。大家都知道,图像是由许多像素组成,而斜向直线的数据提取路线并不一定就是标准的斜线,也可能是呈阶梯状的路线,而如何进行路线设计,就是本文所要展示的内容。

具体流程

      1)建立vector<pair<float,int>> result容器用于存放数据,设置初始化参数。其中,inImage是输入图像,start为起点,end为终点,点的类型为cv::Point。

vector<pair<float, int>> result;
int row = inImage.rows;
int col = inImage.cols;
int r1 = start.y;
int c1 = start.x;
int r2 = end.y;
int c2 = end.x;

      2)确定两点间距离dist,将起点到终点的横坐标差和纵坐标差进行勾股定理可得。所得距离可能为带小数的数据,然而像素的个数都为整数,所以进行四舍五入。除此之外,还要判断下距离,若距离为0,则只返回起点数据。

float dist = round(sqrt(pow(float(r2) - float(r1), 2.0) + pow(float(c2) - float(c1), 2.0)));
if (dist <= 0.00001f) {
  pair<float, int> temp;
  temp.first = inImage.at<float>(r1, c1);
  temp.second = 0;
  result.push_back(temp);
  return result;
}

  3)确定横向纵向的步进间隔。

float slope_r = (float(r2) - float(r1)) / dist;
float slope_c = (float(c2) - float(c1)) / dist;

      4)建立Flag地图,用于标记已存储过的位置,避免同一数据二次放入。

cv::Mat Flag = cv::Mat::zeros(mask.size(), mask.type());

     5)开始存储数据。计数从0开始,若该点处于掩膜内,且Flag地图中没有标记,则进行存储。

int k = 0;
for (float i = 0; i <= dist; ++i) {
  // 若该点处于掩膜内,且未被Flag存储,则进行存储工作
  if ((mask.at<uchar>(int(r1) + int(round(i * slope_r)), int(c1) + int(round(i * slope_c))) == 255)
    && (Flag.at<uchar>(int(r1) + int(round(i * slope_r)), int(c1) + int(round(i * slope_c))) == 0))
  {
    pair<float, int> temp;
    temp.first = inImage.at<float>(int(r1) + int(round(i * slope_r)), int(c1) + int(round(i * slope_c)));
    temp.second = k;
    Flag.at<uchar>(int(r1) + int(round(i * slope_r)), int(c1) + int(round(i * slope_c))) = 255;
    k++;
    result.push_back(temp);
  }
}

功能函数

/**
 * @brief GetOneDimLineData                 获取一维直线数据
 * @param inImage                           输入位相图
 * @param mask                              输入掩膜图
 * @param start                             起始点坐标
 * @param end                               终点坐标
 * @return                                  直线数据(数值&序号)
 */
vector<pair<float, int>> GetOneDimLineData(const cv::Mat inImage, cv::Mat mask, cv::Point start, cv::Point end)
{
  vector<pair<float, int>> result;
  int row = inImage.rows;
  int col = inImage.cols;
  int r1 = start.y;
  int c1 = start.x;
  int r2 = end.y;
  int c2 = end.x;
  // 确定两点间距离
  float dist = round(sqrt(pow(float(r2) - float(r1), 2.0) + pow(float(c2) - float(c1), 2.0)));
  if (dist <= 0.00001f) {
    pair<float, int> temp;
    temp.first = inImage.at<float>(r1, c1);
    temp.second = 0;
    result.push_back(temp);
    return result;
  }
  // 横向纵向的步进间隔
  float slope_r = (float(r2) - float(r1)) / dist;
  float slope_c = (float(c2) - float(c1)) / dist;
  // Flag地图,用于存储已放入的数据,避免同一数据二次放入
  cv::Mat Flag = cv::Mat::zeros(mask.size(), mask.type());
  // 数据量计数,从0开始
  int k = 0;
  for (float i = 0; i <= dist; ++i) {
    // 若该点处于掩膜内,且未被Flag存储,则进行存储工作
    if ((mask.at<uchar>(int(r1) + int(round(i * slope_r)), int(c1) + int(round(i * slope_c))) == 255)
      && (Flag.at<uchar>(int(r1) + int(round(i * slope_r)), int(c1) + int(round(i * slope_c))) == 0))
    {
      pair<float, int> temp;
      temp.first = inImage.at<float>(int(r1) + int(round(i * slope_r)), int(c1) + int(round(i * slope_c)));
      temp.second = k;
      Flag.at<uchar>(int(r1) + int(round(i * slope_r)), int(c1) + int(round(i * slope_c))) = 255;
      k++;
      result.push_back(temp);
    }
  }
  return result;
}

C++测试代码

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
using namespace std;
using namespace cv;
vector<pair<float, int>> GetOneDimLineData(const cv::Mat inImage, cv::Mat mask, cv::Point start, cv::Point end);
int main()
{
  Mat src(10,10,CV_32FC1,nan(""));
  for (int i = 3; i < 7; ++i)
  {
    for (int j = 3; j < 9; ++j)
    {
      src.at<float>(i, j) = rand() % 255;
    }
  }
  cv::Mat mask = cv::Mat::zeros(src.size(), CV_8UC1);
  mask.setTo(255, src == src);
  Point start = Point(2, 1);
  Point end = Point(8, 7);
  vector<pair<float, int>> test= GetOneDimLineData(src,mask, start, end);
  cout << "size:" << test.size() << endl;
  for (int i=0;i<test.size();++i)
  {
    cout << i << ":" << endl;
    cout << test[i].first << " " << test[i].second << endl;
  }
  return 0;
}
/**
 * @brief GetOneDimLineData                 获取一维直线数据
 * @param inImage                           输入位相图
 * @param mask                              输入掩膜图
 * @param start                             起始点坐标
 * @param end                               终点坐标
 * @return                                  直线数据(数值&序号)
 */
vector<pair<float, int>> GetOneDimLineData(const cv::Mat inImage, cv::Mat mask, cv::Point start, cv::Point end)
{
  vector<pair<float, int>> result;
  int row = inImage.rows;
  int col = inImage.cols;
  int r1 = start.y;
  int c1 = start.x;
  int r2 = end.y;
  int c2 = end.x;
  // 确定两点间距离
  float dist = round(sqrt(pow(float(r2) - float(r1), 2.0) + pow(float(c2) - float(c1), 2.0)));
  if (dist <= 0.00001f) {
    pair<float, int> temp;
    temp.first = inImage.at<float>(r1, c1);
    temp.second = 0;
    result.push_back(temp);
    return result;
  }
  // 横向纵向的步进间隔
  float slope_r = (float(r2) - float(r1)) / dist;
  float slope_c = (float(c2) - float(c1)) / dist;
  // Flag地图,用于存储已放入的数据,避免同一数据二次放入
  cv::Mat Flag = cv::Mat::zeros(mask.size(), mask.type());
  // 数据量计数,从0开始
  int k = 0;
  for (float i = 0; i <= dist; ++i) {
    // 若该点处于掩膜内,且未被Flag存储,则进行存储工作
    if ((mask.at<uchar>(int(r1) + int(round(i * slope_r)), int(c1) + int(round(i * slope_c))) == 255)
      && (Flag.at<uchar>(int(r1) + int(round(i * slope_r)), int(c1) + int(round(i * slope_c))) == 0))
    {
      pair<float, int> temp;
      temp.first = inImage.at<float>(int(r1) + int(round(i * slope_r)), int(c1) + int(round(i * slope_c)));
      temp.second = k;
      Flag.at<uchar>(int(r1) + int(round(i * slope_r)), int(c1) + int(round(i * slope_c))) = 255;
      k++;
      result.push_back(temp);
    }
  }
  return result;
}

测试效果

图1 初始化测试图像

图2 Flag地图

图3 结果打印

      不难看出,获取的数据为直线上数据。对于有一定斜度的直线,Flag地图可能呈现阶梯状步进路线,这也是正常的~


      如果函数有什么可以改进完善的地方,非常欢迎大家指出,一同进步何乐而不为呢~


      如果文章帮助到你了,可以点个赞让我知道,我会很快乐~加油!

相关文章
|
11天前
|
算法 计算机视觉
【Qt&OpenCV 图像的感兴趣区域ROI】
【Qt&OpenCV 图像的感兴趣区域ROI】
14 1
|
11天前
|
运维 算法 计算机视觉
【Qt&OpenCV 图像的模板匹配 matchTemplate/minMaxLoc】
【Qt&OpenCV 图像的模板匹配 matchTemplate/minMaxLoc】
13 1
|
11天前
|
存储 编解码 算法
【Qt&OpenCV 检测图像中的线/圆/轮廓 HoughLinesP/HoughCircles/findContours&drawContours】
【Qt&OpenCV 检测图像中的线/圆/轮廓 HoughLinesP/HoughCircles/findContours&drawContours】
24 0
|
11天前
|
算法 计算机视觉
【Qt&OpenCV 图像边缘检测 Sobel/Laplace/Canny】
【Qt&OpenCV 图像边缘检测 Sobel/Laplace/Canny】
10 0
|
11天前
|
算法 计算机视觉
【Qt&OpenCV 图像的形态学变换 morpholgyEx】
【Qt&OpenCV 图像的形态学变换 morpholgyEx】
6 0
|
11天前
|
算法 计算机视觉
【Qt&OpenCV 图像阈值操作 threshold】
【Qt&OpenCV 图像阈值操作 threshold】
11 0
|
11天前
|
资源调度 算法 计算机视觉
【Qt&OpenCV 图像平滑/滤波处理 -- Blur/Gaussian/Median/Bilateral】
【Qt&OpenCV 图像平滑/滤波处理 -- Blur/Gaussian/Median/Bilateral】
14 0
|
23天前
|
计算机视觉
OpenCV中图像算术操作与逻辑操作
OpenCV中图像算术操作与逻辑操作
18 1
|
24天前
|
计算机视觉
OpenCV图像二值化
OpenCV图像二值化
|
24天前
|
存储 Cloud Native Linux
OpenCV图像翻转和旋转
OpenCV图像翻转和旋转