当前位置:网站首页>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()函数
边栏推荐
- [Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
- 2-3查找樹
- 快速集成认证服务-HarmonyOS平台
- A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
- Merge sort and non comparison sort
- Opencv learning notes II - basic image operations
- The field value in Splunk subquery fuzzy matching CSV is*
- IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
- 测试踩坑 - 当已有接口(或数据库表中)新增字段时,都需要注意哪些测试点?
- 单元测试报告成功率低
猜你喜欢

opencv学习笔记一——读取图像的几种方法

Golang compilation constraint / conditional compilation (/ / +build < tags>)

Laravel8 uses passport login and JWT (generate token)

DeiT学习笔记

Opencv learning note 3 - image smoothing / denoising

登山小分队(dfs)

Using nocalhost to develop microservice application on rainbow

Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications

Data type - floating point (C language)

Pvtv2--pyramid vision transformer V2 learning notes
随机推荐
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
POJ - 3784 running medium
Rainbow combines neuvector to practice container safety management
Practice of combining rook CEPH and rainbow, a cloud native storage solution
Tips for using jeditabletable
关于基于kangle和EP面板使用CDN
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
iptables 之 state模块(ftp服务练习)
Snyk dependency security vulnerability scanning tool
You should use Google related products with caution
Golang compilation constraint / conditional compilation (/ / +build < tags>)
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
Go语言中,函数是一种类型
Pvtv2--pyramid vision transformer V2 learning notes
Interface as a parameter (interface callback)
Several ways of lambda used in functions in kotlin (higher-order functions)
Openvscode cloud ide joins rainbow integrated development system
Installation and configuration of PLSQL
How to understand distributed architecture and micro service architecture
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience