OpenCV獲取圖像中直線上的數(shù)據(jù)具體流程
需求說明
在對圖像進行處理時,經(jīng)常會有這類需求:客戶想要提取出圖像中某條直線或者ROI區(qū)域內(nèi)的感興趣數(shù)據(jù),進行重點關注。該需求在圖像檢測領域尤其常見。ROI區(qū)域一般搭配Rect即可完成提取,直線數(shù)據(jù)的提取沒有現(xiàn)成的函數(shù),需要自行實現(xiàn)。
當直線為縱向或者橫向時,比較簡單,只需要從起點到終點提取該行或者列的數(shù)據(jù)即可;但是直線若為斜向的,則需要從起點出發(fā),向終點方向逐個像素提取。大家都知道,圖像是由許多像素組成,而斜向直線的數(shù)據(jù)提取路線并不一定就是標準的斜線,也可能是呈階梯狀的路線,而如何進行路線設計,就是本文所要展示的內(nèi)容。
具體流程
1)建立vector<pair<float,int>> result容器用于存放數(shù)據(jù),設置初始化參數(shù)。其中,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,將起點到終點的橫坐標差和縱坐標差進行勾股定理可得。所得距離可能為帶小數(shù)的數(shù)據(jù),然而像素的個數(shù)都為整數(shù),所以進行四舍五入。除此之外,還要判斷下距離,若距離為0,則只返回起點數(shù)據(jù)。
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地圖,用于標記已存儲過的位置,避免同一數(shù)據(jù)二次放入。
cv::Mat Flag = cv::Mat::zeros(mask.size(), mask.type());
5)開始存儲數(shù)據(jù)。計數(shù)從0開始,若該點處于掩膜內(nèi),且Flag地圖中沒有標記,則進行存儲。
int k = 0;
for (float i = 0; i <= dist; ++i) {
// 若該點處于掩膜內(nè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);
}
}
功能函數(shù)
/**
* @brief GetOneDimLineData 獲取一維直線數(shù)據(jù)
* @param inImage 輸入位相圖
* @param mask 輸入掩膜圖
* @param start 起始點坐標
* @param end 終點坐標
* @return 直線數(shù)據(jù)(數(shù)值&序號)
*/
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地圖,用于存儲已放入的數(shù)據(jù),避免同一數(shù)據(jù)二次放入
cv::Mat Flag = cv::Mat::zeros(mask.size(), mask.type());
// 數(shù)據(jù)量計數(shù),從0開始
int k = 0;
for (float i = 0; i <= dist; ++i) {
// 若該點處于掩膜內(nè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 獲取一維直線數(shù)據(jù)
* @param inImage 輸入位相圖
* @param mask 輸入掩膜圖
* @param start 起始點坐標
* @param end 終點坐標
* @return 直線數(shù)據(jù)(數(shù)值&序號)
*/
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地圖,用于存儲已放入的數(shù)據(jù),避免同一數(shù)據(jù)二次放入
cv::Mat Flag = cv::Mat::zeros(mask.size(), mask.type());
// 數(shù)據(jù)量計數(shù),從0開始
int k = 0;
for (float i = 0; i <= dist; ++i) {
// 若該點處于掩膜內(nè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;
}
測試效果



不難看出,獲取的數(shù)據(jù)為直線上數(shù)據(jù)。對于有一定斜度的直線,F(xiàn)lag地圖可能呈現(xiàn)階梯狀步進路線,這也是正常的~
如果函數(shù)有什么可以改進完善的地方,非常歡迎大家指出,一同進步何樂而不為呢~
到此這篇關于OpenCV獲取圖像中直線上的數(shù)據(jù)具體流程的文章就介紹到這了,更多相關OpenCV獲取圖像數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

