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

Zener diode zener diode sod123 package positive and negative distinction

Microservice system design -- unified authentication service design

使用域名转发mqtt协议,避坑指南

什么是BFC?有什么用?

双位置继电器DLS-34A DC0.5A 220VDC

机械转码日记【17】模板,STL简介

pycharm 如何安装 package

Microservice system design -- API gateway service design

微服务系统设计——微服务调用设计

Experience oceanbase database under win10
随机推荐
019 C语言基础:C预处理
微服务系统设计——微服务调用设计
Pytest框架的执行规则
jq怎么获取元素的id名
快速排序(非递归)和归并排序
双位置继电器XJLS-8G/220
Microservice system design - service fusing and degradation design
[station B up dr_can learning notes] Kalman filter 1
微服务系统设计——服务熔断和降级设计
leetcode299周赛记录
RTP 发送PS流工具(已经开源)
Flink production problems (1.10)
Microservice system design -- unified authentication service design
021 basics of C language: recursion, variable parameters
Chapter 1 Introduction
010 C language foundation: C function
Ad22 Gerber files Click to open the Gerber step interface. Official solutions to problems
微服务系统设计——服务链路跟踪设计
高翔slam14讲-笔记1
网易云音乐params和encSecKey参数生成代码