Ejemplo n.º 1
0
def convolve(img, filters, show=False):
    accum = np.zeros_like(img)
    for filter in filters:
        img_filt = cv2.filter2D(img, cv2.CV_8UC3, filter)
        # img_filt += img_filt
    np.maximum(accum, img_filt, accum)

    if show:
        titles = ['Original', 'Filtered']
        images = [img, img_filt]
        title = 'Apply Gabor filter'
        tl.plotImages(titles, images, title, 1, 2)

    return accum
def detectDefects(img_filtered, show=False):
    kernel_close = np.ones((5, 5), np.uint8)
    kernel_dilate = np.ones((5, 5), np.uint8)
    defects_detected_nms = None
    copy = img_filtered.copy()
    # Threshold image
    thresh = cv2.threshold(copy, 65, 220, cv2.THRESH_BINARY_INV)[1]
    # Clean image
    thresh = cv2.morphologyEx(thresh,
                              cv2.MORPH_CLOSE,
                              kernel_close,
                              iterations=3)
    thresh = cv2.dilate(thresh, kernel_dilate, iterations=2)
    # Find contours
    contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)[1]
    contours = [
        cnt for cnt in contours
        if cv2.boundingRect(cnt)[2] > 10 and cv2.boundingRect(cnt)[3] > 10
    ]
    # filtered_contours = []
    # for cnt in contours:
    #     if cnt[2] > 10 and cnt[3] > 10:
    #         filtered_contours.append(cnt)

    if len(contours) != 0:
        # Get all bboxes
        defects_detected = [cv2.boundingRect(contour) for contour in contours]
        # Apply NMS to all bboxes
        defects_detected_nms = non_max_suppression_fast(
            np.array(defects_detected), 0.1)

        if show:
            copy_th = cv2.cvtColor(thresh.copy(), cv2.COLOR_GRAY2BGR)
            # copy_th = cv2.drawContours(copy_th, contours, -1, (0, 255, 0), 2)
            for (x, y, w, h) in defects_detected_nms:
                cv2.rectangle(copy_th, (x, y), (x + w, y + h), (255, 0, 0), 2)

            titles = ['Filtered', 'Thresh (bbox)']
            images = [img_filtered, copy_th]
            title = 'Detect defects'
            tl.plotImages(titles, images, title, 1, 2)

    return defects_detected_nms
def removeBG(img, margin, show=False):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img = cv2.GaussianBlur(img, (5, 5), 0)
    thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
    contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)[1]
    if len(contours) != 0:
        # find the biggest area
        cnt_wood = max(contours, key=cv2.contourArea)
        x, y, w, h = cv2.boundingRect(cnt_wood)
        fg = img[y + margin:y + h - margin, x:x + w]
    if show:
        copy = cv2.cvtColor(img.copy(), cv2.COLOR_GRAY2BGR)
        cv2.rectangle(copy, (x, y), (x + w, y + h), (0, 255, 255), 2)
        titles = ['Contour', 'ROI']
        images = [copy, fg]
        title = 'Remove background'
        tl.plotImages(titles, images, title, 1, 2)

    return fg, (x, y, w, h)
Ejemplo n.º 4
0
def build_filters(size, theta, sigma, show=False):
    filters = []
    ksize = size
    for t in theta:
        for s in sigma:
            #cv2.getGaborKernel(ksize, sigma, theta, lambda, gamma, psi, ktype)
            kern = np.real(
                cv2.getGaborKernel((ksize, ksize),
                                   s,
                                   t,
                                   10.0,
                                   0.5,
                                   0,
                                   ktype=cv2.CV_32F))
            kern /= 1.5 * kern.sum()
            filters.append(kern)

    if show:
        titles = []
        images = np.real(filters)
        title = 'Gabor kernels'
        tl.plotImages(titles, images, title, 6, 8)

    return filters
Ejemplo n.º 5
0
                                                     margin)

            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            # Draw GT
            if gt is not None:
                for x, y, w, h in gt:
                    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
            # Draw detections
            if defects_detected is not None:
                for x, y, w, h in defects_detected:
                    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            if show:
                titles = []
                images = [img]
                title = "Detection result for '{}'".format(file)
                tl.plotImages(titles, images, title, 1, 1)
            results.append(imp.evaluate(gt, defects_detected))  # debug

        prev_file = substr
        # break
    results = np.array(results)

    iou_avg = np.mean([np.mean(iou) for iou in results[:, 0]])
    TR = np.sum(results[:, 1]).astype(int)
    FR = np.sum(results[:, 2]).astype(int)
    FA = np.sum(results[:, 3]).astype(int)
    TA = np.sum(results[:, 4]).astype(int)
    print(text.format(iou_avg, TR, FR, FA, TA))

    classes = ['rejected', 'accepted']
    cnf_matrix = np.zeros((2, 2), dtype=int)