当前位置:网站首页>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
边栏推荐
猜你喜欢

指针进阶,字符串函数

Opencv learning note 3 - image smoothing / denoising

Rapid integration of authentication services - harmonyos platform

登山小分队(dfs)
![FPGA knowledge accumulation [6]](/img/db/c3721c3e842ddf4c1088a3f54e9f2a.jpg)
FPGA knowledge accumulation [6]

2-3 lookup tree
![[Yugong series] February 2022 U3D full stack class 006 unity toolbar](/img/2e/3a7d71a0b5a6aff294a0bd2f8515f8.jpg)
[Yugong series] February 2022 U3D full stack class 006 unity toolbar

idea里使用module项目的一个bug

21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design

PLSQL的安装和配置
随机推荐
Golang compilation constraint / conditional compilation (/ / +build < tags>)
Greenplum6.x监控软件搭建
更改当前文件夹及文件夹下文件日期shell脚本
[Chongqing Guangdong education] accounting reference materials of Nanjing University of Information Engineering
Rsync remote synchronization
Composer change domestic image
下载和安装orcale database11.2.0.4
How to realize the high temperature alarm of the machine room in the moving ring monitoring system
Compilation and linking of programs
What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
mysql分区讲解及操作语句
Gson转换实体类为json时报declares multiple JSON fields named
Laravel8 uses passport login and JWT (generate token)
为什么要选择云原生数据库
Merge sort and non comparison sort
指针进阶,字符串函数
如何理解分布式架构和微服务架构呢
Greenplum6.x搭建_环境配置
Data type - integer (C language)
Go write a program that runs within a certain period of time