網站首頁 編程語言 正文
本文實例為大家分享了python opencv實現圖像目標的外接圖形,供大家參考,具體內容如下
當使用cv2.findContours函數找到圖像中的目標后,我們通常希望使用一個集合區域將圖像包圍起來,這里介紹opencv幾種幾何包圍圖形。
- 邊界矩形
- 最小外接矩形
- 最小外接圓
簡介
無論使用哪種幾何外接方法,都需要先進行輪廓檢測。
當我們得到輪廓對象后,可以使用boundingRect()得到包裹此輪廓的最小正矩形,minAreaRect()得到包裹輪廓的最小矩形(允許矩陣傾斜),minEnclosingCircle()得到包裹此輪廓的最小圓形。
最小正矩形和最小外接矩形的區別如下圖所示:
實現
這里給出上述5中外接圖形在python opencv上的實現:
①. 邊界矩形
import cv2
import numpy as np
img = cv2.imread('/home/pzs/圖片/test.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh, binary = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY_INV)
binary, contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cv2.imshow('binary', binary)
cv2.waitKey(0)
for cnt in contours:
? ? x,y,w,h = cv2.boundingRect(cnt)
? ? cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 1)
cv2.imshow('image', img)
cv2.waitKey(0)
②. 最小外接矩形
import cv2
import numpy as np
img = cv2.imread('/home/pzs/圖片/test.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh, binary = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY_INV)
binary, contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cv2.imshow('binary', binary)
cv2.waitKey(0)
for cnt in contours:
? ? rect = cv2.minAreaRect(cnt)
? ? box = cv2.boxPoints(rect)
? ? box = np.int0(box)
? ? cv2.drawContours(img, [box], 0, (0, 0, 255), 2)
cv2.imshow('image', img)
cv2.waitKey(0)
③. 最小外接圓
import cv2
import numpy as np
img = cv2.imread('/home/pzs/圖片/test.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh, binary = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY_INV)
binary, contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cv2.imshow('binary', binary)
cv2.waitKey(0)
for cnt in contours:
? ? (x, y), radius = cv2.minEnclosingCircle(cnt)
? ? center = (int(x), int(y))
? ? radius = int(radius)
? ? cv2.circle(img, center, radius, (255, 0, 0), 2)
cv2.imshow('image', img)
cv2.waitKey(0)
原文鏈接:https://blog.csdn.net/HUXINY/article/details/89329307
相關推薦
- 2022-10-20 docker中的volume和bind?mount區別講解_docker
- 2022-03-18 .NET?6開發TodoList應用之實現PUT請求_實用技巧
- 2022-07-14 C++深入探索類和對象之封裝及class與struct的區別_C 語言
- 2022-09-05 docker-compose啟動redis集群的實現步驟_docker
- 2022-03-17 .NET?6開發TodoList應用引入數據存儲_實用技巧
- 2023-02-25 React中常用的Hook有哪些_React
- 2022-06-02 Apache?Hudi集成Spark?SQL操作hide表_數據庫其它
- 2022-12-28 詳解Go語言strconv與其他基本數據類型轉換函數的使用_Golang
- 最近更新
-
- 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同步修改后的遠程分支