Morphological operations - refinement

  In OpenCV, thinning in image morphology operations is a technique used for image processing. Its main purpose is to refine the boundaries of objects in the image by iteratively deleting pixels in the image so that it Keep as much detail as possible while reducing the number of pixels. This is commonly used in image processing to analyze shape, structure and edge information in images.

principle:

  Thinning operations are usually based on the concept of skeletonization, which retains the main structural and shape features of the object by iteratively removing pixels in the image. Common refinement algorithms include Zhang-Suen algorithm and Guo-Hall algorithm. These algorithms repeatedly apply structural elements (usually small 3x3 matrices) to detect and delete pixels until conditions for refinement are reached.

effect:

  • Reduce noise and redundant information: Refinement can reduce unnecessary details and noise in the image, retaining the main shape and structural information.
  • Edge detection and feature extraction: Refinement can help identify the main contours and shapes of objects in the image for subsequent feature extraction and edge detection.

Applicable scene:

  • Image analysis and recognition: In image processing, when the shape or contour of an object needs to be analyzed, thinning can help remove unnecessary pixels and highlight the object's features. Main features.
  • Pattern recognition and computer vision: Used to extract the main structural information of objects in images for classification, identification or tracking.

  In image processing, thinning operations usually involve binary images and use structuring elements to detect and delete pixels. The specific mathematical formula may involve the neighborhood of the pixel and some conditional judgment logic, but it would be more complicated to give the formula directly. Refinement operations are generally implemented through iterative algorithms rather than simple mathematical formulas.

OpenCV code example:

Here is sample code for image thinning using Python and OpenCV:

import cv2
import numpy as np

def show_images(image):
    cv2.namedWindow('image',cv2.WINDOW_KEEPRATIO)
    cv2.imshow('image',image)
    cv2.waitKey()
    cv2.destroyAllWindows()

def Thinning(image):
    # 二值化图像
    ret, binary_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
    # 定义细化函数
    def thinning_image(img):
        thinned = np.zeros(img.shape, np.uint8)
        thinned = cv2.ximgproc.thinning(img, thinned, cv2.ximgproc.THINNING_ZHANGSUEN)
        return thinned
    # 进行细化操作
    thinned_image = thinning_image(binary_image)
    return thinned_image

if __name__ == '__main__':
    # 读取图像
    img = cv2.imread('cat-dog.png', flags=0)
    re_img=Thinning(img)
    # top_row = np.hstack((img, re_img[0]))
    # bottom_row = np.hstack((re_img[1], re_img[2])) #水平
    # combined_img = np.vstack((img, re_img))# 垂直
    combined_img=np.hstack((img,re_img))
    show_images(combined_img)

Guess you like

Origin blog.csdn.net/qq_50993557/article/details/134863165