当前位置:网站首页>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
边栏推荐
- POJ - 3616 Milking Time(DP+LIS)
- Greenplum6.x-版本变化记录-常用手册
- Deit learning notes
- 注解@ConfigurationProperties的三种使用场景
- Le système mes est un choix nécessaire pour la production de l'entreprise
- All about PDF crack, a complete solution to meet all your PDF needs
- 详解华为应用市场2022年逐步减少32位包体上架应用和策略
- Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
- 路由信息协议——RIP
- 如何在快应用中实现滑动操作组件
猜你喜欢
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
Teach you how to select PCB board by hand (II)
The single value view in Splunk uses to replace numeric values with text
Greenplum6.x常用语句
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
Pvtv2--pyramid vision transformer V2 learning notes
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
Componentspace2022, assertions, protocols, bindings, and configuration files
下载和安装orcale database11.2.0.4
Implement your own dataset using bisenet
随机推荐
Gson转换实体类为json时报declares multiple JSON fields named
南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
Open3d ISS key points
【踩坑】nacos注册一直连接localhost:8848,no available server
Golan idea IntelliJ cannot input Chinese characters
[kuangbin]专题十五 数位DP
[machine learning] watermelon book data set_ data sharing
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
数据分片介绍
路由信息协议——RIP
Appeler l'interface du moteur de création du service multimédia de jeu Huawei renvoie le Code d'erreur 1002, le message d'erreur: les paramètres sont l'erreur
单场带货涨粉10万,农村主播竟将男装卖爆单?
Deit learning notes
Go write a program that runs within a certain period of time
Obsidan之数学公式的输入
Qt Charts使用(重写QChartView,实现一些自定义功能)
Opencv learning notes 1 -- several methods of reading images
Implementation of navigation bar at the bottom of applet
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error