Как определить край, если края объекта имеют тот же цвет, что и фон - PullRequest
0 голосов
/ 09 ноября 2018

Мне поручено построить систему обнаружения номерных знаков, и мой код не работает, если табличка имеет такой же цвет краски автомобиля (фон).

Посмотрите на эту картинку ниже. enter image description here

Я испробовал различные техники обнаружения краев, и мои выводы сводятся к тому, что они едва ли работают.

Вот мой конвейер обработки изображений:

  • Извлечение серого канала из изображения.
  • Снижение шума с помощью итеративной двухсторонней фильтрации
  • Обнаружение краев с помощью адаптивного порога
  • слегка расширить края
  • Определение контуров на основе некоторых эвристик.

Деталь обнаружения кромки с треском выполнена вокруг области пластины.

enter image description here

Трубопровод работает хорошо, и я могу обнаружить номерные знаки, если у автомобиля другой цвет краски, чем у таблички.

Код

def rectangleness(hull):
    rect = cv2.boundingRect(hull)
    rectPoints = np.array([[rect[0], rect[1]], 
                           [rect[0] + rect[2], rect[1]],
                           [rect[0] + rect[2], rect[1] + rect[3]],
                           [rect[0], rect[1] + rect[3]]])
    intersection_area = cv2.intersectConvexConvex(np.array(rectPoints), hull)[0] 
    rect_area = cv2.contourArea(rectPoints)
    rectangleness = intersection_area/rect_area
    return rectangleness


def preprocess(image):
    image = imutils.resize(image, 1000)

    # Attenuate shadows by using H channel instead of converting to gray directly
    imgHSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    _, _, gray = cv2.split(imgHSV)

    # Reduce noise while preserve edge with Iterative Bilaterial Filtering
    blur = cv2.bilateralFilter(gray, 11, 6, 6)

    # Detect edges by thresholding
    edge = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 5)

    # Dilate edges, kernel size cannot be too big as some fonts are very closed to the edge of the plates
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2))
    dilated = cv2.dilate(edge, kernel)

    # Detect contours
    edge, contours, _ = cv2.findContours(dilated, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    # Loop through contours and select the most probable ones
    contours = sorted(contours, key = cv2.contourArea, reverse=True)[:10]

    for contour in contours:
        perimeter = cv2.arcLength(contour, closed=True)
        approximate = cv2.approxPolyDP(contour, 0.02*perimeter, closed=True)

        if len(approximate) == 4:
            (x, y, w, h) = cv2.boundingRect(approximate)
            whRatio = w / h

            # Heuristics:
            # 1. Width of plate should at least be 2x greater than height
            # 2. Width of contour should be more than 5 (eliminate false positive)
            # 3. Height must not be too small
            # 4. Polygon must resemble a rectangle
            if (2.0 < whRatio < 6.0) and (w > 5.0) and (h > 20):
                hull = cv2.convexHull(approximate, returnPoints=True)
                if rectangleness(hull) > 0.75:
                    print("X Y {} {}".format(x, y))
                    print("Height: {}".format(h))
                    print("Width : {}".format(w))
                    print("Ratio : {}\n".format(w/h))
                    cv2.drawContours(image, [approximate], -1, (0, 255, 0), 2)

    cv2.imshow("Edge", edge)
    cv2.imshow("Frame", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

1 Ответ

0 голосов
/ 14 ноября 2018

Вы можете использовать cv2.morphologyEx, чтобы сделать область пластины более заметной. Следующий шаг - найти контуры и установить разумные условия для извлечения контура, в котором находится пластина. Если хотите, можете взглянуть на этот репозиторий github , где мы с другом показываем подробные шаги по обнаружению и распознаванию номерных знаков.

import cv2
import numpy as np

img = cv2.imread("a.png")

imgBlurred = cv2.GaussianBlur(img, (7, 7), 0)
gray = cv2.cvtColor(imgBlurred, cv2.COLOR_BGR2GRAY) # convert to gray
sobelx = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=3) # sobelX to get the vertical edges

ret,threshold_img = cv2.threshold(sobelx, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

morph_img_threshold = threshold_img.copy()
element = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(22, 3))
cv2.morphologyEx(src=threshold_img, op=cv2.MORPH_CLOSE, kernel=element, 
dst=morph_img_threshold)

cv2.imshow("img", img)
cv2.imshow("sobelx", sobelx)
cv2.imshow("morph_img_threshold", morph_img_threshold)

cv2.waitKey()
cv2.destroyAllWindows()

enter image description here

...