当前位置:网站首页>Image segmentation in opencv

Image segmentation in opencv

2022-07-07 08:42:00 Top of the program

Original picture :
 Insert picture description here
The graph obtained by final segmentation :
 Insert picture description here

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

OpenCV-Python The image processing : Corrosion and expansion principles and erode、dilate Function introduction

Python OpenCV morphologyEx() function

opencv The problem of image binarization (cv2.adaptiveThreshold function )

OpenCV adaptiveThreshold

opencv-python Adaptive threshold binarization function cv2.adaptiveThreshold Use and effect

原网站

版权声明
本文为[Top of the program]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070549282172.html