def filterRegions(img_orig, regions):
    width = 90
    height = 30

    current_path = os.path.dirname(os.path.realpath(__file__))
    clf = joblib.load(os.path.join(current_path, "models", "svm.pkl"))

    if len(regions) > 0:
        scores = []

        for i, region in enumerate(regions):
            minr, minc, maxr, maxc = region.bbox
            img_window = img_orig[minr: maxr, minc: maxc]
            img_window = transform.resize(img_window, (height, width))
            img_window = np.absolute(filters.prewitt_v(img_window))
            features = img_window.ravel()
            scores.append((clf.decision_function(features)[0], i))

        # all scores
        scores.sort(reverse=True)
        scores = np.array(scores)
        i = int(scores[0, 1])
        return [regions[i]]

    return []
Exemple #2
0
def test_vprewitt_vertical():
    """Vertical prewitt on an edge should be a vertical line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (j >= 0).astype(float)
    result = filters.prewitt_v(image)
    # Fudge the eroded points
    j[np.abs(i) == 5] = 10000
    assert (np.all(result[j == 0] == 1))
    assert_allclose(result[np.abs(j) > 1], 0, atol=1e-10)
def prewittk(image_file, path_to_train):
    im = imread(image_file, as_gray=True)
    edges_prewitt_horizontal = prewitt_h(im)
    edges_prewitt_vertical = prewitt_v(im)
    imsave(image_file[:-4] + '_p.jpg', edges_prewitt_vertical, cmap='gray')
    base = os.path.basename(image_file)
    base = base[:-4] + '.txt'
    shutil.copy2(path_to_train + "/labels/" + base,
                 path_to_train + "/labels/" + base[:-4] + '_p.txt')
def refineBoundaries(img_orig, plate):
    np.random.seed(7)

    minr, minc, maxr, maxc = plate.bbox
    img_window = img_orig[minr: maxr, minc: maxc]

    plate_points = [(minc, minr), (maxc, minr), (maxc, maxr), (minc, maxr)]
    plate_x = minc
    plate_y = minr

    img_window = np.absolute(filters.prewitt_v(img_window))
    thresh = filters.threshold_otsu(img_window)
    img_window = img_window <= thresh
    labels = measure.label(img_window)

    points = []

    for region in measure.regionprops(labels):
        minr, minc, maxr, maxc = region.bbox
        ratio = float(maxr - minr) / float(maxc - minc)
        heigh = maxr - minr
        area = region.area

        if (ratio > 1 and area > 10 and heigh > 10):
            points.append((minc, minr, maxc, maxr))

    if len(points) > 1:
        points = np.array(points)
        x1 = np.min(points[:, 0])
        x2 = np.max(points[:, 2])

        ransac_model, inliers = measure.ransac(points[:, 0:2], measure.LineModel, 5, 3, max_trials=30)
        points = points[inliers]

        if ransac_model.params[1] != 0:
            average_heigh = int(np.mean(points[:, 3]) - np.mean(points[:, 1]))
            pad_t = average_heigh / 2
            pad_b = average_heigh + (average_heigh / 3)

            y1 = ransac_model.predict_y(x1)
            y2 = ransac_model.predict_y(x2)

            refined_points = [(x1, y1 - pad_t), (x2, y2 - pad_t), (x2, y2 + pad_b), (x1, y1 + pad_b)]
            refined_points = [(x + plate_x, y + plate_y) for (x, y) in refined_points]
            return refined_points

    return plate_points
Exemple #5
0
def detect_motion_blur(image, ratio=2, band=None):
    """
    Detect if an image is blurry due to movement, and therefore unreliable
    to analyze. Uses the variance of the prewitt edge detectors in the horizontal and vertical directions.

    Parameters
    ----------
    image : ndarray
        rgb or grayscale image.
    ratio : float
        Maximum ratio of edge strength variance in x- to y-directions. `threshold=2` means if
        `x_edge.var()` / `y_edge.var() is > 2 OR < 0.5, the image is flagged as blurry do to
        movement.
    band : int
        Band of image to use for analysis. Unspecified defaults to 0 (R in RGB).

    Returns
    -------
    bool - does the image meet requirements?

    """
    if band is None:
        try:
            analyze = image[:, :, 0].copy()
        except:
            analyze = image.copy()
            pass
    else:
        analyze = image[:, :, band]

    horiz = img_as_ubyte(filters.prewitt_v(analyze)).var()
    vert = img_as_ubyte(filters.prewitt_h(analyze)).var()

    test = horiz / vert

    if test < ratio and test > 1/ratio:
        out = True
    else:
        out = False

    return(out)
def detect_motion_blur(image, ratio=2, band=None):
    """
    Detect if an image is blurry due to movement, and therefore unreliable
    to analyze. Uses the variance of the prewitt edge detectors in the horizontal and vertical directions.

    Parameters
    ----------
    image : ndarray
        rgb or grayscale image.
    ratio : float
        Maximum ratio of edge strength variance in x- to y-directions. `threshold=2` means if
        `x_edge.var()` / `y_edge.var() is > 2 OR < 0.5, the image is flagged as blurry do to
        movement.
    band : int
        Band of image to use for analysis. Unspecified defaults to 0 (R in RGB).

    Returns
    -------
    bool - does the image meet requirements?

    """
    if band is None:
        try:
            analyze = image[:, :, 0].copy()
        except:
            analyze = image.copy()
            pass
    else:
        analyze = image[:, :, band]

    horiz = img_as_ubyte(filters.prewitt_v(analyze)).var()
    vert = img_as_ubyte(filters.prewitt_h(analyze)).var()

    test = horiz / vert

    if test < ratio and test > 1 / ratio:
        out = True
    else:
        out = False

    return (out)
def getRegions(img_orig):
    # sizes = [4, 6, 8, 10]
    sizes = [4, 8, 12]

    img_processed = (255 * np.absolute(filters.prewitt_v(img_orig))).astype(np.uint8)
    img_aux = (255 * np.absolute(filters.prewitt_h(img_orig))).astype(np.uint8)
    img_processed[img_aux > 10] = 0

    regions_of_interest = []

    for s in sizes:
        img_processed = morphology.dilation(img_processed, morphology.square(s))
        thresh = filters.threshold_otsu(img_processed)
        img_processed = img_processed >= thresh
        labels = measure.label(img_processed)

        for region in measure.regionprops(labels):
            minr, minc, maxr, maxc = region.bbox
            ratio = float(maxr - minr) / float(maxc - minc)
            area = region.area
            if area > 1500 and area < 50000 and ratio < 1 and ratio > 0.2:
                regions_of_interest.append(region)

    return regions_of_interest
Exemple #8
0
def test_vprewitt_horizontal():
    """Vertical prewitt on a horizontal edge should be zero."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.prewitt_v(image)
    assert_allclose(result, 0)
Exemple #9
0
def test_vprewitt_mask():
    """Vertical prewitt on a masked array should be zero."""
    np.random.seed(0)
    result = filters.prewitt_v(np.random.uniform(size=(10, 10)),
                               np.zeros((10, 10), bool))
    assert_allclose(result, 0)
Exemple #10
0
def test_vprewitt_zeros():
    """Vertical prewitt on an array of all zeros."""
    result = filters.prewitt_v(np.zeros((10, 10)), np.ones((10, 10), bool))
    assert_allclose(result, 0)
Exemple #11
0
from skimage.filters import gaussian, gaussian_filter, laplace,prewitt
from skimage.filters import prewitt_v,prewitt_h,scharr, wiener

gauss=gaussian(gray0, sigma=5, multichannel=True)
plt.imshow(gauss)

gauss2=gaussian_filter(gray0, sigma=5, multichannel=True)
plt.imshow(gauss2)

lap=laplace(gray0,ksize=100)
plt.imshow(lap)

pre=prewitt(gray0, mask=None)
plt.imshow(pre)

pre_v=prewitt_v(gray0, mask=None)
plt.imshow(pre_v)

from skimage import filters
edges2 = filters.roberts(gray0)
plt.imshow(edges2)

plt.imshow(scharr(gray0))
plt.imshow(threshold_mean(gray0))
plt.imshow(wiener(gray0))

#######################################
plt.imshow(img)
plt.imshow(gray0)
plt.imshow(image)
### TREES
from PIL import Image, ImageChops
import matplotlib.pyplot as plt
import numpy as np
import cv2
from skimage import data, io, filters, color, exposure

img1 = io.imread('../image/pic2.jpg', as_gray=True)
img2 = filters.sobel_v(img1)
img3 = filters.prewitt_v(img1)
img4 = filters.laplace(img1)

plt.figure('show image')
plt.subplot(2, 2, 1)
plt.imshow(img1, cmap='gray')
plt.title('original image')
plt.axis('off')

plt.subplot(2, 2, 2)
plt.imshow(img2, cmap='gray')
plt.title('sobel image')
plt.axis('off')

plt.subplot(2, 2, 3)
plt.imshow(img3, cmap='gray')
plt.title('prewitt image')
plt.axis('off')

plt.subplot(2, 2, 4)
plt.imshow(img4, cmap='gray')
plt.title('laplace image')
plt.axis('off')
Exemple #13
0
    def prewitt(self):
        filteredImage = filters.prewitt_v(self.rawImage)

        self.showImage(filteredImage)
        self.rawImage = filteredImage
        print("Prewitt")
def getFeatures(img, px, py, w, h):
    img_window = img[py : py + h, px : px + w]
    img_window = transform.resize(img_window, (window_height, window_width))
    img_window = np.absolute(filters.prewitt_v(img_window))
    return img_window.ravel()