当前位置:网站首页>Comprehensive application of OpenCV in contour detection and threshold processing
Comprehensive application of OpenCV in contour detection and threshold processing
2022-06-27 05:17:00 【Keep_ Trying_ Go】
List of articles
1. Contour find and draw contour
https://mydreamambitious.blog.csdn.net/article/details/125339604
2.Opencv Approximation and convex hull of multiple deformations in
https://mydreamambitious.blog.csdn.net/article/details/125340595
3.Opencv Global binarization and local binarization
https://mydreamambitious.blog.csdn.net/article/details/125249121
4. What is an image Mask
https://www.cnblogs.com/skyfsm/p/6894685.html
5. Basic rotation of image ( Flip, etc )
https://mydreamambitious.blog.csdn.net/article/details/125428402
6.Opencv Calculate the perimeter and area of the contour in
https://mydreamambitious.blog.csdn.net/article/details/125340081
7. Code combat
(1) Flip and zoom the image
# Flip the picture ,Opencv The image read is the opposite of our normal display
img=cv2.flip(src=frame,flipCode=2)
img=cv2.resize(src=img,dsize=(600,600))
(2) Remove the background
# Remove the background
bgsegment=cv2.createBackgroundSubtractorMOG2()
# Convert to grayscale
img=cv2.cvtColor(src=img,code=cv2.COLOR_BGR2GRAY)
img = bgsegment.apply(img)
(3) Gaussian processing and binarization processing
# Gaussian noise processing on the image
gauss=cv2.GaussianBlur(src=img,ksize=(3,3),sigmaX=0)
# Image binarization
ok, dst = cv2.threshold(src=gauss, thresh=70, maxval=255, type=cv2.THRESH_BINARY)
(4) Image corrosion and expansion operation
# First obtain convolution kernel
kernel = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(3, 3))
# Corrode the image
erode=cv2.erode(src=dst,kernel=kernel,iterations=1)
# canny=cv2.Canny(image=erode,threshold1=50,threshold2=100)
# Expand the image
dilate=cv2.dilate(src=erode,kernel=kernel,iterations=1)
(5) Contour search
# Find the outline
contours,hierarchy=cv2.findContours(image=dilate,mode=cv2.RETR_TREE,
method=cv2.CHAIN_APPROX_SIMPLE)
(6) Find out the outline with the largest area and draw the outline
# Find the largest outline
index=0
maxArea=0
length=len(contours)
if length>0:
for i in range(length):
# Calculate the area of the contour
area=cv2.contourArea(contours[i])
if area>maxArea:
maxArea=area
index=i
# print('len(contours)= {} ; index={}'.format(len(contours),index))
# First draw the shape of the image
image=np.zeros(shape=frame.shape,dtype=np.uint8)
res=contours[index]
cv2.drawContours(image=image,contours=[res],
contourIdx=0,color=(0,255,0),thickness=2)
# Drawing convex hull
hull=cv2.convexHull(points=res)
cv2.drawContours(image=image,contours=[hull],contourIdx=0,
color=(0,0,255),thickness=3)
(7) Code synthesis
import os
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
# Remove the background
bgsegment=cv2.createBackgroundSubtractorMOG2()
while cap.isOpened():
OK,frame=cap.read()
if OK==False:
break
# Flip the picture ,Opencv The image read is the opposite of our normal display
img=cv2.flip(src=frame,flipCode=2)
img=cv2.resize(src=img,dsize=(600,600))
# Convert to grayscale
img=cv2.cvtColor(src=img,code=cv2.COLOR_BGR2GRAY)
img = bgsegment.apply(img)
# Gaussian noise processing on the image
gauss=cv2.GaussianBlur(src=img,ksize=(3,3),sigmaX=0)
# Image binarization
ok, dst = cv2.threshold(src=gauss, thresh=70, maxval=255, type=cv2.THRESH_BINARY)
# First obtain convolution kernel
kernel = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(3, 3))
# Corrode the image
erode=cv2.erode(src=dst,kernel=kernel,iterations=1)
# canny=cv2.Canny(image=erode,threshold1=50,threshold2=100)
# Expand the image
dilate=cv2.dilate(src=erode,kernel=kernel,iterations=1)
# Find the outline
contours,hierarchy=cv2.findContours(image=dilate,mode=cv2.RETR_TREE,
method=cv2.CHAIN_APPROX_SIMPLE)
# Find the largest outline
index=0
maxArea=0
length=len(contours)
if length>0:
for i in range(length):
# Calculate the area of the contour
area=cv2.contourArea(contours[i])
if area>maxArea:
maxArea=area
index=i
# print('len(contours)= {} ; index={}'.format(len(contours),index))
# First draw the shape of the image
image=np.zeros(shape=frame.shape,dtype=np.uint8)
res=contours[index]
cv2.drawContours(image=image,contours=[res],
contourIdx=0,color=(0,255,0),thickness=2)
# Drawing convex hull
hull=cv2.convexHull(points=res)
cv2.drawContours(image=image,contours=[hull],contourIdx=0,
color=(0,0,255),thickness=3)
cv2.imshow('image',image)
if cv2.waitKey(1)&0xFF==27:
break
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')
边栏推荐
- 微服务系统设计——微服务监控与系统资源监控设计
- 双位置继电器DLS-34A DC0.5A 220VDC
- Some articles about component packaging and my experience
- Common programming abbreviations for orbit attitude
- 017 basics of C language: bit field and typedef
- Redis high availability cluster (sentry, cluster)
- 差点因为 JSON.stringify 丢了奖金...
- DAST 黑盒漏洞扫描器 第六篇:运营篇(终)
- 【NIPS 2017】PointNet++:度量空间中点集的深层次特征学习
- Discussion on streaming media protocol (MPEG2-TS, RTSP, RTP, RTCP, SDP, RTMP, HLS, HDS, HSS, mpeg-dash)
猜你喜欢

【C语言】关键字的补充

Microservice system design -- microservice monitoring and system resource monitoring design

Microservice system design - service fusing and degradation design

neo4j图数据库基本概念

Discussion on streaming media protocol (MPEG2-TS, RTSP, RTP, RTCP, SDP, RTMP, HLS, HDS, HSS, mpeg-dash)

双位置继电器RXMVB2 R251 204 110DC

Edge在IE模式下加载网页 - Edge设置IE兼容性

双位置继电器JDP-1440/DC110V
![[unity] button of UI interactive component & summary of optional base classes](/img/9f/be9005f03ad9a2bc8da0f910f064c5.png)
[unity] button of UI interactive component & summary of optional base classes

Penetration test - file upload / download / include
随机推荐
Microservice system design -- distributed transaction service design
高翔slam14讲-笔记1
Niuke practice 101-c reasoning clown - bit operation + thinking
双位置继电器HJWS-9440
006 C语言基础:C存储类
How JQ gets the ID name of an element
Avoid asteroids
014 C语言基础:C字符串
py2neo基本语法
Almost because of json Stringify lost his bonus
EPICS记录参考5 -- 数组模拟输入记录Array Analog Input (aai)
Some articles about component packaging and my experience
Flink production problems (1.10)
Redis高可用集群(哨兵、集群)
【B站UP DR_CAN学习笔记】Kalman滤波1
Execution rules of pytest framework
021 basics of C language: recursion, variable parameters
微服务系统设计——API 网关服务设计
《数据库原理与应用》第一版 马春梅……编著 期末复习笔记
Basic concepts of neo4j graph database