How to identify different objects in an image?

后端 未结 3 888
孤城傲影
孤城傲影 2021-02-05 12:13

I\'m intending to write a program to detect and differentiate certain objects from a nearly solid background. The foreground and the background have a high contrast difference w

3条回答
  •  温柔的废话
    2021-02-05 12:34

    With the assumption that the objects are simple shapes, here's an approach using thresholding + contour approximation. Contour approximation is based on the assumption that a curve can be approximated by a series of short line segments which can be used to determine the shape of a contour. For instance, a triangle has three vertices, a square/rectangle has four vertices, a pentagon has five vertices, and so on.

    1. Obtain binary image. We load the image, convert to grayscale, Gaussian blur, then adaptive threshold to obtain a binary image.

    2. Detect shapes. Find contours and identify the shape of each contour using contour approximation filtering. This can be done using arcLength to compute the perimeter of the contour and approxPolyDP to obtain the actual contour approximation.


    Input image

    Detected objects highlighted in green

    Labeled contours

    Code

    import cv2
    
    def detect_shape(c):
        # Compute perimeter of contour and perform contour approximation
        shape = ""
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.04 * peri, True)
    
        # Triangle
        if len(approx) == 3:
            shape = "triangle"
    
        # Square or rectangle
        elif len(approx) == 4:
            (x, y, w, h) = cv2.boundingRect(approx)
            ar = w / float(h)
    
            # A square will have an aspect ratio that is approximately
            # equal to one, otherwise, the shape is a rectangle
            shape = "square" if ar >= 0.95 and ar <= 1.05 else "rectangle"
    
        # Star
        elif len(approx) == 10:
            shape = "star"
    
        # Otherwise assume as circle or oval
        else:
            shape = "circle"
    
        return shape
    
    # Load image, grayscale, Gaussian blur, and adaptive threshold
    image = cv2.imread('1.jpg')
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (7,7), 0)
    thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,31,3)
    
    # Find contours and detect shape
    cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        # Identify shape
        shape = detect_shape(c)
    
        # Find centroid and label shape name
        M = cv2.moments(c)
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])
        cv2.putText(image, shape, (cX - 20, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (36,255,12), 2)
    
    cv2.imshow('thresh', thresh)
    cv2.imshow('image', image)
    cv2.waitKey()
    

提交回复
热议问题