Skip to content Skip to sidebar Skip to footer

Opencv(4.0.0) Assertion Failed In Function 'contourarea'

My aim is to identify car logos using HOG descriptors. I am following tutorial linked https://gurus.pyimagesearch.com/lesson-sample-histogram-of-oriented-gradients-and-car-logo-rec

Solution 1:

Before finding countours of image edged, convert it into uint8 type:

edged = np.uint8(edged)
cnts, _ = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

I have edited your code. I checked it on my system, it is working fine. Try this:

# import the necessary packagesfrom sklearn.neighbors import KNeighborsClassifier
from skimage import exposure
from skimage import feature
from imutils import paths
import argparse
import imutils
import cv2
# construct the argument parse and parse command line arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--training", required=True, help="Path to the logos training dataset")
ap.add_argument("-t", "--test", required=True, help="Path to the test dataset")
args = vars(ap.parse_args())

# initialize the data matrix and labelsprint('[INFO] extracting features...')
data = []
labels = []


# loop over the image paths in the training setfor imagePath in paths.list_images(args["training"]):
    # extract the make of the car
    make = imagePath.split("/")[-2]

    # load the image, convert it to grayscale, and detect edges
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    edged = imutils.auto_canny(gray)

    # find contours in the edge map, keeping only the largest one which# is presmumed to be the car logo
    edged = np.uint8(edged)
    cnts, _ =  = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    if cnts isnotNone:
        cnts = cnts[0] if imutils.is_cv2() else cnts[1]
        c = max(cnts, key=cv2.contourArea)

        # extract the logo of the car and resize it to a canonical width# and height
        (x, y, w, h) = cv2.boundingRect(c)
        logo = gray[y:y + h, x:x + w]
        logo = cv2.resize(logo, (200, 100))

        # extract Histogram of Oriented Gradients from the logo
        H = feature.hog(logo, orientations=9, pixels_per_cell=(10, 10),
            cells_per_block=(2, 2), transform_sqrt=True, block_norm="L1")

        # update the data and labels
        data.append(H)
        labels.append(make)

Post a Comment for "Opencv(4.0.0) Assertion Failed In Function 'contourarea'"