open cv学习 (十)图形检测

图形检测

demo1
python 复制代码
# 绘制几何图像的轮廓
import cv2

img = cv2.imread("./shape1.png")

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 将图像二值化
t, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# 检测图像中的所有轮廓
contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

cv2.drawContours(img, contours, 3, (0, 0, 255), 5)
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
demo2
python 复制代码
# 绘制花朵的轮廓
import cv2

img = cv2.imread("flower.png")

cv2.imshow("img", img)
img = cv2.medianBlur(img, 5)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
t, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

cv2.imshow("binary", binary)

contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(img, contours, -1, (0, 0, 255), 2)
cv2.imshow("contours", img)
cv2.waitKey()
cv2.destroyAllWindows()
demo3
python 复制代码
import cv2
# 矩形包围框

img = cv2.imread("./shape2.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
t, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# 获取第一个轮廓的最小矩形边框
x, y, w, h = cv2.boundingRect(contours[0])
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
demo4
python 复制代码
import cv2
# 圆形包围框
img = cv2.imread("./shape2.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
t, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# 获取第一个轮廓的最小矩形边框
center, radius = cv2.minEnclosingCircle(contours[0])
x = int(round(center[0]))
y = int(round(center[1]))
cv2.circle(img, (x, y), int(radius), (0, 0, 255), 2)
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
demo5
python 复制代码
import cv2
# 凸包
img = cv2.imread("./shape2.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
t, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# 获取第一个轮廓的最小矩形边框
hull = cv2.convexHull(contours[0])
cv2.polylines(img, [hull], True, (0, 0, 255), 2)
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
demo6
python 复制代码
# Canny边缘检测
import cv2

img = cv2.imread("flower.png")

r1 = cv2.Canny(img, 10, 50)
r2 = cv2.Canny(img, 100, 200)
r3 = cv2.Canny(img, 400, 600)

cv2.imshow("img", img)
cv2.imshow("r1", r1)
cv2.imshow("r2", r2)
cv2.imshow("r3", r3)
cv2.waitKey()
cv2.destroyAllWindows()
demo7
python 复制代码
# 检测笔图像中出现的直线
import cv2
import numpy as np

img = cv2.imread("./pen.jpg")

o = img.copy()

o = cv2.medianBlur(o, 5)

gray = cv2.cvtColor(o, cv2.COLOR_BGR2GRAY)

binary = cv2.Canny(o, 50, 150)

lines = cv2.HoughLinesP(binary, 1, np.pi/180, 15, minLineLength=100, maxLineGap=18)

for line in lines:
    x1, y1, x2, y2 = line[0]
    cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)

cv2.imshow("canny", binary)
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
demo8
python 复制代码
# 圆环检测
import cv2
import numpy as np

img = cv2.imread("coin.jpg")

o = img.copy()
o = cv2.medianBlur(o, 5)
gray = cv2.cvtColor(o, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 70, param1=100, param2=25, minRadius=10, maxRadius=50)
circles = np.uint(np.around(circles))
for c in circles[0]:
    x, y, r = c
    cv2.circle(img, (x, y), r, (0, 0, 255), 3)
    cv2.circle(img, (x, y), 2, (0, 0, 255), 3)

cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
相关推荐
小夏子_riotous7 分钟前
Docker学习路径——3、常用命令
linux·运维·服务器·学习·docker·容器·centos
STLearner36 分钟前
WSDM 2026 | 时间序列(Time Series)论文总结【预测,表示学习,因果】
大数据·论文阅读·人工智能·深度学习·学习·机器学习·数据挖掘
redaijufeng1 小时前
网络爬虫学习:应用selenium获取Edge浏览器版本号,自动下载对应版本msedgedriver,确保Edge浏览器顺利打开。
爬虫·学习·selenium
腾科IT教育1 小时前
零基础快速上岸HCIP,高效学习思路分享
学习·华为认证·hcip·hcip考试·hcip认证
23471021271 小时前
4.14 学习笔记
笔记·python·学习
醇氧1 小时前
【学习】软件过程模型全解析:从瀑布到敏捷的演进之路
学习·log4j
邪修king1 小时前
UE5 零基础入门第三弹: 碰撞与触发交互,解锁场景机关与蓝图封装(高娱乐性学习)
学习·ue5·交互
今天你TLE了吗2 小时前
LLM到Agent&RAG——AI概念概述 第二章:提示词
人工智能·笔记·后端·学习
烤麻辣烫3 小时前
JS基础
开发语言·前端·javascript·学习
red_redemption3 小时前
自由学习记录(168)
学习·已经运行中世界-模与约束·闭环