網站首頁 編程語言 正文
1. 圖像輪廓
1.1 findContours介紹
cv2.findContours(img, mode, method)
mode:輪廓檢索模式
- RETR_EXTERNAL :只檢索最外面的輪廓;
- RETR_LIST:檢索所有的輪廓,并將其保存到一條鏈表當中;
- RETR_CCOMP:檢索所有的輪廓,并將他們組織為兩層:頂層是各部分 外部邊界,第二層是空洞的邊界;
- RETR_TREE:檢索所有的輪廓,并重構嵌套輪廓的整個層次;
method:輪廓逼近方法
- CHAIN_APPROX_NONE:以Freeman鏈碼的方式輸出輪廓,所有其他方法輸出多邊形(頂點的序列)。
- CHAIN_APPROX_SIMPLE:壓縮水平的、垂直的和斜的部分,也就是,函數只保留他們的終點部分。
1.2 繪制輪廓
import cv2 def cv_show(img, name): cv2.imshow(name, img) cv2.waitKey() cv2.destroyAllWindows() img = cv2.imread('DataPreprocessing/img/contours.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) cv_show(thresh, 'thresh')
contours.png原圖展示:
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) draw_img = img.copy() res = cv2.drawContours(draw_img, contours, -1, (0, 0, 255), 2) cv_show(res, 'res')
“-1”表示顯示所有輪廓,(B, G , R) = (0, 0, 255) 采用紅色的顯示全部輪廓,如下:
或者顯示索引為1的輪廓,代碼如下:?
draw_img = img.copy() res = cv2.drawContours(draw_img, contours, 1, (0, 0, 255), 2) cv_show(res, 'res')
索引為1的是三角形的內輪廓,0是外輪廓:
1.3 輪廓特征
cnt = contours[0] # 面積 print("面積: ", cv2.contourArea(cnt)) # 周長,True表示閉合的 print("周長: ", cv2.arcLength(cnt, True))
輸出:
面積: 8500.5
周長: 437.9482651948929
2. 輪廓近似
2.1 輪廓
contours2.png原圖 :
img = cv2.imread('DataPreprocessing/img/contours2.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) cnt = contours[0] draw_img = img.copy() res = cv2.drawContours(draw_img, [cnt], -1, (0, 0, 255), 2) cv_show(res, 'res')
邊緣檢測:
原理:以這個弧線為例, A , B A,B A,B端連線,取弧線上一點 C C C離線段 A B AB AB的距離最大,判斷 d 1 d_{1} d1?是否小于設置的閾值 T T T, 不小于 T T T的,則以 A , C A,C A,C連接線段 A C AC AC,重復上面的操作,取得圖中的 d 2 d_{2} d2?,再同 T T T做比較,直至 d n d_{n} dn?小于閾值得出線段為輪廓邊緣。
2.2 邊界矩形
img = cv2.imread('DataPreprocessing/img/contours.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) cnt = contours[0] x, y, w, h = cv2.boundingRect(cnt) img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) cv_show(img, 'img')
2.3 外界多邊形及面積
area = cv2.contourArea(cnt) x, y, w, h = cv2.boundingRect(cnt) rect_area = w * h extent = float(area) / rect_area print('輪廓面積與邊界矩形比', extent)
輸出:
輪廓面積與邊界矩形比 0.5154317244724715
外接圓形:
(x, y), radius = cv2.minEnclosingCircle(cnt) center = (int(x), int(y)) radius = int(radius) img = cv2.circle(img, center, radius, (0, 255, 0), 2) cv_show(img, 'img')
結果展示:
原文鏈接:https://blog.csdn.net/qq_37700257/article/details/127068745
相關推薦
- 2022-08-02 C#?HttpClient?Post參數同時上傳文件的實現_C#教程
- 2023-02-12 完美解決Redis在雙擊redis-server.exe出現閃退問題_Redis
- 2023-06-16 GO語言中通道和sync包的使用教程分享_Golang
- 2022-12-01 Golang?編寫Tcp服務器的解決方案_Golang
- 2022-10-06 Python時間戳與日期格式之間相互轉化的詳細教程_python
- 2022-05-09 Python?Matplotlib條形圖之垂直條形圖和水平條形圖詳解_python
- 2022-12-03 Golang檢查變量類型的四種方式_Golang
- 2022-07-10 網絡I/o編程模型23 netty的出站與入站中handler加載與執行順序
- 最近更新
-
- 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同步修改后的遠程分支