当前位置:网站首页>opencv之图像分割
opencv之图像分割
2022-07-07 05:49:00 【程序之巅】
原图:
最终分割得到的图:
def OpenCVSegmentation():
cv2.namedWindow('origin', cv2.WINDOW_NORMAL)
cv2.namedWindow('gray', cv2.WINDOW_NORMAL)
cv2.namedWindow('closed', cv2.WINDOW_NORMAL)
cv2.namedWindow('result', cv2.WINDOW_NORMAL)
cv2.namedWindow('binary', cv2.WINDOW_NORMAL)
cv2.namedWindow('DILATE', cv2.WINDOW_NORMAL)
cv2.namedWindow('ERODE', cv2.WINDOW_NORMAL)
img = cv2.imread("../data/imgs/20211126-Z2111001-4-1-1_0_1.png") # 载入图像
h, w = img.shape[:2] #获取图像的高和宽
#设置显示图片窗口大小
cv2.resizeWindow('origin', int(w / 3), int(h / 3))
cv2.resizeWindow('gray', int(w/3), int(h/3))
cv2.resizeWindow('closed', int(w/3), int(h/3))
cv2.resizeWindow('result', int(w/3), int(h/3))
cv2.resizeWindow('binary', int(w / 3), int(h / 3))
cv2.resizeWindow('DILATE', int(w / 3), int(h / 3))
cv2.resizeWindow('ERODE', int(w / 3), int(h / 3))
cv2.imshow("origin", img) #显示原始图像
# 滤波去噪
blured = cv2.blur(img,(3,3)) #进行滤波去掉噪声
# cv2.imshow("Blur", blured) #显示低通滤波后的图像
#得到灰度图
gray = cv2.cvtColor(blured,cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", gray)
#定义结构元素
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(9, 9))
#开闭运算,先开运算去除背景噪声,再继续闭运算填充目标内的孔洞
opened = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)
closed = cv2.morphologyEx(opened, cv2.MORPH_CLOSE, kernel)
cv2.imshow("closed", closed)
#求二值图
# ret, binary = cv2.threshold(closed,150,255,cv2.THRESH_BINARY)
# cv2.imshow("binary", binary)
# binary = cv2.adaptiveThreshold(closed, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 5)
binary = cv2.adaptiveThreshold(closed, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,31, 5)
cv2.imshow("binary", binary)
#膨胀
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
binary = cv2.morphologyEx(binary, cv2.MORPH_DILATE, kernel)
cv2.imshow("DILATE", binary)
#腐蚀
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 25))
binary = cv2.morphologyEx(binary, cv2.MORPH_ERODE, kernel)
cv2.imshow("ERODE", binary)
#找到轮廓
contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#绘制轮廓
cv2.drawContours(img,contours,-1,(0,0,255),3)
#绘制结果
cv2.imshow("result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
OpenCVSegmentation()
参考文章:
OpenCV-Python图像形态变换概述及morphologyEx函数介绍
OpenCV-Python图像处理:腐蚀和膨胀原理及erode、dilate函数介绍
Python OpenCV morphologyEx()函数
边栏推荐
- Le système mes est un choix nécessaire pour la production de l'entreprise
- DeiT学习笔记
- Automatic upgrading of database structure in rainbow
- How to understand distributed architecture and micro service architecture
- Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
- Practice of combining rook CEPH and rainbow, a cloud native storage solution
- Train your dataset with swinunet
- 2-3查找树
- Basic data types and string types are converted to each other
- 调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
猜你喜欢
Lua programming learning notes
Pvtv2--pyramid vision transformer V2 learning notes
opencv学习笔记二——图像基本操作
GFS distributed file system
Exercise arrangement 2.10, 11
Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
[hard core science popularization] working principle of dynamic loop monitoring system
2 - 3 arbre de recherche
详解华为应用市场2022年逐步减少32位包体上架应用和策略
随机推荐
Openvscode cloud ide joins rainbow integrated development system
Golan idea IntelliJ cannot input Chinese characters
Basic data types and string types are converted to each other
如何理解分布式架构和微服务架构呢
National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
Give full play to the wide practicality of maker education space
One click installation of highly available Nacos clusters in rainbow
Pvtv2--pyramid vision transformer V2 learning notes
Obsidan之数学公式的输入
IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
Data type - integer (C language)
String operation
字符串操作
National SMS center number inquiry
grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录
redis故障处理 “Can‘t save in background: fork: Cannot allocate memory“
Download and install orcale database11.2.0.4
数据中台落地实施之法
POJ - 3616 Milking Time(DP+LIS)
PVTV2--Pyramid Vision TransformerV2学习笔记