当前位置:网站首页>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

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')

原网站

版权声明
本文为[Keep_ Trying_ Go]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270514304358.html