opencv object measurement

Detect shapes in images

Image to grayscale, binarization, contour extraction, contour and center of gravity drawing

During binarization, it is determined whether to reverse according to the image

 

 

import cv2 as cv
import numpy as np

img = cv.imread('tooth.png')
img = cv.imread('num.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
gray = cv.GaussianBlur(gray, (3, 3), 0)
cv.imshow('gray', gray)
ret, binary = cv.threshold(gray, 0, 255,
                           cv.THRESH_BINARY | cv.THRESH_OTSU
                           | cv.THRESH_BINARY_INV
                           )
cv.imshow('binary', binary)

clone_img, contours, heriachy = cv.findContours(
    binary,
    # cv.RETR_TREE,  # Find all
    cv.RETR_EXTERNAL,  # External contour
    cv.CHAIN_APPROX_SIMPLE,
)

for i, contour in enumerate(contours):
    # Calculate contour area
    area = cv.contourArea(contour)
    # External rectangle
    x, y, w, h = cv.boundingRect(contour)
    # Geometric moments
    mm = cv.moments(contour)
    # Focus
    cx = int(mm['m10'] / mm['m00'])
    cy = int(mm['m01'] / mm['m00'])
    # Visualize center of gravity and contour rectangles
    cv.circle(img, (cx, cy), 5, (0, 0, 0), -1)
    cv.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)


cv.imshow('ret', img)
cv.waitKey(0)

 

 

Detect the image shape according to the extracted contour

approxPolyDP uses line segments to approach images. Generally, regular images can be detected. Note that noise needs to be removed in advance

In binarization, some information may be lost, so you need to choose the appropriate threshold

 

 

 

import cv2 as cv
import numpy as np

img = cv.imread('tooth.png')
img = cv.imread('num.jpg')
img = cv.imread('shape2.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
gray = cv.GaussianBlur(gray, (3, 3), 0)
cv.imshow('gray', gray)
ret, binary = cv.threshold(gray, 0, 255,
                           cv.THRESH_BINARY | cv.THRESH_OTSU
                           # | cv.THRESH_BINARY_INV
                           )
cv.imshow('binary', binary)

clone_img, contours, heriachy = cv.findContours(
    binary,
    # cv.RETR_TREE,  # Find all
    cv.RETR_EXTERNAL,  # External contour
    cv.CHAIN_APPROX_SIMPLE,
)

for i, contour in enumerate(contours):
    # Calculate contour area
    area = cv.contourArea(contour)
    # External rectangle
    x, y, w, h = cv.boundingRect(contour)
    # Geometric moments
    mm = cv.moments(contour)
    # Focus
    cx = int(mm['m10'] / mm['m00'])
    cy = int(mm['m01'] / mm['m00'])
    # Visualize center of gravity and contour rectangles
    cv.circle(img, (cx, cy), 5, (0, 0, 0), -1)
    cv.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)

    approxCurve = cv.approxPolyDP(contour, 5, True)
    print(approxCurve.shape)
    # Pentagon
    if approxCurve.shape[0] == 3:
        cv.drawContours(img, contours, i, [255, 0, 255], 4)
    # Triangle
    if approxCurve.shape[0] == 4:
        cv.drawContours(img, contours, i, [255, 255, 0], 4)
    # circular
    if approxCurve.shape[0] > 9:
        cv.drawContours(img, contours, i, [255, 0, 255], 4)

cv.imshow('ret', img)
cv.waitKey(0)

Posted by clarket on Fri, 10 Jan 2020 10:56:47 -0800