当前位置:网站首页>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')
边栏推荐
- 012 C language foundation: C array
- 认知篇----2022高考志愿该如何填报
- Microservice system design -- microservice monitoring and system resource monitoring design
- 015 C语言基础:C结构体、共用体
- Flink production problems (1.10)
- leetcode298周赛记录
- neo4j数据库导出
- Web3 has not been implemented yet, web5 suddenly appears!
- EPICS记录参考5 -- 数组模拟输入记录Array Analog Input (aai)
- RTP sending PS stream tool (open source)
猜你喜欢
[unity] button of UI interactive component & summary of optional base classes
系统架构设计——互联网金融的架构设计
Penetration test - file upload / download / include
stm32读取IO高低电平状态
EasyExcel合并相同内容单元格及动态标题功能的实现
【B站UP DR_CAN学习笔记】Kalman滤波1
Basic concepts of neo4j graph database
高翔slam14讲-笔记1
RTP sending PS stream tool (open source)
论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
随机推荐
Epics record reference 5 -- array analog input recordarray analog input (AAI)
015 basics of C language: C structure and common body
Avoid asteroids
Ad22 Gerber files Click to open the Gerber step interface. Official solutions to problems
网易云音乐params和encSecKey参数生成代码
Chapter 2 Introduction to key technologies
020 basics of C language: C language forced type conversion and error handling
pycharm 如何安装 package
006 C language foundation: C storage class
Cognition - how to fill in 2022 college entrance examination volunteers
《数据库原理与应用》第一版 马春梅……编著 期末复习笔记
019 basics of C language: C preprocessing
Chapter 1 Introduction
微服务系统设计——微服务监控与系统资源监控设计
golang hello 安装环境异常【已解决】
差点因为 JSON.stringify 丢了奖金...
Laptop does not have WiFi option solution
Neo4j community conflicts with neo4j desktop
Quick sort (non recursive) and merge sort
Execution rules of pytest framework