網站首頁 編程語言 正文
需求說明
在對圖像進行處理時,經常會有這類需求:客戶想要提取出圖像中某條直線、圓線或者ROI區域內的感興趣數據,進行重點關注。該需求在圖像檢測領域尤其常見。ROI區域一般搭配Rect即可完成提取,直線和圓線數據的提取沒有現成的函數,需要自行實現。
直線的提取見:
而圓線的提取則是本文要將的內容,對圓線而言,將線上某點作為起點,沿順時針或逆時針方向依次提取感興趣數據,可放置在容器中。那么如何快速提取呢?本文提供了一種比較簡單的思路,應用窗口模板,在窗口中快速找到下一可前進點的位置,步進然后再找下個點,形成路徑追蹤,進而實現整圈圓線數據的提取。
具體流程
1)初始化。設置路徑追蹤窗口尺寸size為3,創建path作為行進路徑,p點作為起點,c用來存放目標數據點。
cv::Mat c; int size = 3; cv::Mat path = mask.clone(); Point p = Point(center.x + radius, center.y);
2)將起點放置在c中,將path中的起點值置0,表示該點已經走過。
c.push_back(src.at<uchar>(p.y, p.x)); path.at<uchar>(p.y, p.x) = 0;
3)用WinDataNum函數判斷當前點的窗口內有幾個可前進點,若無則說明路徑封死或者完成路徑,wn值表示可前進點的個數。
int wn = WinDataNum(path, p, size);
4)窗口內遍歷,查看是否有可前進路徑,若找到,則將當前點信息刷新為此點,并將標記符find設為true,find的意義是快速中斷遍歷,用來提速。
int t = size / 2; bool find = false; for (int i = p.y - t; i <= p.y + t; ++i) { uchar *g = path.ptr<uchar>(i); for (int j = p.x - t; j <= p.x + t; ++j) { if (g[j] == 255) { p.x = j; p.y = i; find = true; break; } } if (find) break; }
5)若找到了點,即find為true,則將該點的數據存放在c中,path中置0,并以該點為中心搜索窗口內可前進路徑。
if (find) { c.push_back(src.at<uchar>(p.y, p.x)); path.at<uchar>(p.y, p.x) = 0; wn = WinDataNum(path, p, size); } else break;
6)若wn為0了,則說明路徑封死或者完成路徑了,跳出循環,函數執行完畢。?
while (wn) { int t = size / 2; bool find = false; for (int i = p.y - t; i <= p.y + t; ++i) { uchar *g = path.ptr<uchar>(i); for (int j = p.x - t; j <= p.x + t; ++j) { if (g[j] == 255) { p.x = j; p.y = i; find = true; break; } } if (find) break; } if (find) { c.push_back(src.at<uchar>(p.y, p.x)); path.at<uchar>(p.y, p.x) = 0; wn = WinDataNum(path, p, size); } else break; }
功能函數
// 獲取圓圈上的數據,逆時針存儲,起點在中心同行最右側數據 cv::Mat getCircleData(cv::Mat src, cv::Mat mask, cv::Point center, int radius) { cv::Mat c; int size = 3; cv::Mat path = mask.clone(); Point p = Point(center.x + radius, center.y); c.push_back(src.at<uchar>(p.y, p.x)); path.at<uchar>(p.y, p.x) = 0; int wn = WinDataNum(path, p, size); while (wn) { int t = size / 2; bool find = false; for (int i = p.y - t; i <= p.y + t; ++i) { uchar *g = path.ptr<uchar>(i); for (int j = p.x - t; j <= p.x + t; ++j) { if (g[j] == 255) { p.x = j; p.y = i; find = true; break; } } if (find) break; } if (find) { c.push_back(src.at<uchar>(p.y, p.x)); path.at<uchar>(p.y, p.x) = 0; wn = WinDataNum(path, p, size); } else break; } return c; }
// 獲取窗口內的有效數據個數 int WinDataNum(cv::Mat path, cv::Point p, int size) { int number = 0; int t = size / 2; for (int i = p.y - t; i <= p.y + t; ++i) { uchar *g = path.ptr<uchar>(i); for (int j = p.x - t; j <= p.x + t; ++j) { if (g[j] == 255) number++; } } return number; }
C++測試代碼
#include <iostream> #include <opencv2/opencv.hpp> #include <string> using namespace std; using namespace cv; cv::Mat getCircleData(cv::Mat src, cv::Mat mask, cv::Point center, int radius); int WinDataNum(cv::Mat path, cv::Point p, int size); int main() { cv::Mat src = imread("test.jpg", 0); cv::Mat mask = cv::Mat::zeros(src.size(), CV_8UC1); cv::Point center = cv::Point(src.cols / 2, src.rows / 2); int radius = min(src.cols, src.rows) / 2 - 10; circle(mask, center, radius, Scalar(255), 1, 8); cv::Mat c = getCircleData(src, mask, center, radius); src.setTo(0, mask == 0); imshow("src", src); imshow("mask", mask); waitKey(0); return 0; } // 獲取圓圈上的數據,逆時針存儲,起點在中心同行最右側數據 cv::Mat getCircleData(cv::Mat src, cv::Mat mask, cv::Point center, int radius) { cv::Mat c; int size = 3; cv::Mat path = mask.clone(); Point p = Point(center.x + radius, center.y); c.push_back(src.at<uchar>(p.y, p.x)); path.at<uchar>(p.y, p.x) = 0; int wn = WinDataNum(path, p, size); while (wn) { int t = size / 2; bool find = false; for (int i = p.y - t; i <= p.y + t; ++i) { uchar *g = path.ptr<uchar>(i); for (int j = p.x - t; j <= p.x + t; ++j) { if (g[j] == 255) { p.x = j; p.y = i; find = true; break; } } if (find) break; } if (find) { c.push_back(src.at<uchar>(p.y, p.x)); path.at<uchar>(p.y, p.x) = 0; wn = WinDataNum(path, p, size); } else break; } return c; } // 獲取窗口內的有效數據個數 int WinDataNum(cv::Mat path, cv::Point p, int size) { int number = 0; int t = size / 2; for (int i = p.y - t; i <= p.y + t; ++i) { uchar *g = path.ptr<uchar>(i); for (int j = p.x - t; j <= p.x + t; ++j) { if (g[j] == 255) number++; } } return number; }
測試效果
圖1 原圖
圖2 掩膜內圖像
如圖1圖2所示,掩膜內的圖像數據就是我們要提取的目標。
圖3 放大后數據搜索路徑
圖3放大后可以看出,起點是230,之后的數據是230、231、236、232、234、146等等,再看c容器中的數據。
圖4 容器內數據
對比完開頭,再看結尾,如圖3所示,是234、234、231、234、234,然后就是起點230,查看容器。
圖5 容器內數據
這樣有的小伙伴可能覺得中間會不會有數據錯誤呢,很簡單,打開VS復制代碼后,搭配ImageWatch插件,debug調試打斷點觀察path矩陣,看看它的255數據是不是按預想的路徑消失,如果是則說明扔的數據也沒有問題。
總結
本文提供的只是一個簡單思路,有一定局限性。比如該方法在圓線寬為1時效果最佳,若線寬大了就不能用窗口簡單判斷了;另外,起點在右側時是逆時針獲取數據,起點在左側時是順時針獲取數據,如果想統一標準的話,最好加上起點的位置判斷,然后決定是否將c的數據翻轉。至于運行速度方面,3000*3000的圖像矩陣中運行基本為0ms,畢竟只是提取了一條圓線而已。
原文鏈接:https://zhaitianbao.blog.csdn.net/article/details/121613124
相關推薦
- 2022-07-25 C#爬蟲基礎之HttpClient獲取HTTP請求與響應_C#教程
- 2022-06-28 C#中Lambda表達式的三種寫法_C#教程
- 2022-06-15 Win10下通過Ubuntu安裝Redis的過程_Redis
- 2022-01-17 url傳參 中文出現亂碼問題 解決方案
- 2022-11-21 Python?tabulate結合loguru打印出美觀方便的日志記錄_python
- 2023-04-01 PyTorch之torch.randn()如何創建正態分布隨機數_python
- 2022-03-14 windows環境搭建golang的gin框架簡易教程
- 2022-04-28 C#使用BackgroundWorker控件_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支