当前位置:网站首页>Image segmentation in opencv
Image segmentation in opencv
2022-07-07 08:42:00 【Top of the program】
Original picture :
The graph obtained by final segmentation :
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") # Load the image
h, w = img.shape[:2] # Get the height and width of the image
# Set the size of the display picture window
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) # Show the original image
# Filtering and denoising
blured = cv2.blur(img,(3,3)) # Filter to remove noise
# cv2.imshow("Blur", blured) # Display the image after low-pass filtering
# Get the grayscale image
gray = cv2.cvtColor(blured,cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", gray)
# Define structural elements
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(9, 9))
# Open close operation , First open the operation to remove the background noise , Then continue the closed operation to fill the holes in the target
opened = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)
closed = cv2.morphologyEx(opened, cv2.MORPH_CLOSE, kernel)
cv2.imshow("closed", closed)
# Find binary graph
# 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)
# inflation
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
binary = cv2.morphologyEx(binary, cv2.MORPH_DILATE, kernel)
cv2.imshow("DILATE", binary)
# corrosion
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 25))
binary = cv2.morphologyEx(binary, cv2.MORPH_ERODE, kernel)
cv2.imshow("ERODE", binary)
# Find the outline
contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# Draw the outline
cv2.drawContours(img,contours,-1,(0,0,255),3)
# Draw the result
cv2.imshow("result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
OpenCVSegmentation()
Reference article :
OpenCV-Python Overview of image morphological transformation and morphologyEx Function introduction
Python OpenCV morphologyEx() function
opencv The problem of image binarization (cv2.adaptiveThreshold function )
opencv-python Adaptive threshold binarization function cv2.adaptiveThreshold Use and effect
边栏推荐
- Basic data types and string types are converted to each other
- AVL平衡二叉搜索树
- Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
- idea里使用module项目的一个bug
- [kuangbin] topic 15 digit DP
- Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
- 调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
- 指针进阶,字符串函数
- IP地址的类别
- Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
猜你喜欢
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
Open3d ISS key points
opencv之图像分割
联想混合云Lenovo xCloud:4大产品线+IT服务门户
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"
【踩坑】nacos注册一直连接localhost:8848,no available server
Using nocalhost to develop microservice application on rainbow
Three series of BOM elements
Greenplum6.x监控软件搭建
详解华为应用市场2022年逐步减少32位包体上架应用和策略
随机推荐
Greenplum6.x-版本变化记录-常用手册
Go语言中,函数是一种类型
对API接口或H5接口做签名认证
opencv之图像分割
Qt Charts使用(重写QChartView,实现一些自定义功能)
Basic data types and string types are converted to each other
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
说一个软件创业项目,有谁愿意投资的吗?
Splunk query CSV lookup table data dynamic query
如何在图片的目标中添加目标的mask
[untitled]
Installation and configuration of PLSQL
GFS distributed file system
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
Virtual address space
All about PDF crack, a complete solution to meet all your PDF needs
Implementation of navigation bar at the bottom of applet
uniapp 微信小程序监测网络
grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录
The single value view in Splunk uses to replace numeric values with text