def filter_result3(img, bw_result, ths, thm):
    bw_result_orig = np.copy(bw_result)
    points = np.where((bw_result > 0))
    points = np.reshape(points, np.shape(points))
    points = np.transpose(points)
    npoints = np.shape(points)[0]
    k = 20
    step = 5
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    sat = (hsv[:, :, 1] / 255)
    bw_result_filter = np.zeros(np.shape(bw_result))
    xc = points[:, 1]
    yc = points[:, 0]
    for ct in range(0, npoints, step):
        ystart = max(0, (yc[ct] - k))
        xstart = max(0, (xc[ct] - k))
        yend = min(np.shape(img)[0], (yc[ct] + k))
        xend = min(np.shape(img)[1], (xc[ct] + k))
        p = points[ct, :]
        p = np.reshape(p, (1, 2))
        Dpoints = distance.cdist(p, points)
        Dpoints = np.squeeze(Dpoints)
        ipoints = np.squeeze(np.where((Dpoints < 40)))
        xneigh = points[(ipoints, 1)]
        yneigh = points[(ipoints, 0)]
        (V, D) = eigen_cov(xneigh, yneigh)
        vmin = V[:, 0]
        if (D[1] < D[0]):
            vmin = V[:, 1]
        x1 = (xc[ct] - (k * vmin[0]))
        y1 = (yc[ct] - (k * vmin[1]))
        x2 = (xc[ct] + (k * vmin[0]))
        y2 = (yc[ct] + (k * vmin[1]))
        (p, px, py) = improfile(sat, np.array([x1, x2]), np.array([y1, y2]), 30)
        s = np.abs((np.mean(p[0:5]) - np.mean(p[(len(p) - 5):len(p)])))
        s = round((s * 100))
        m = np.max([p[0:5], p[(len(p) - 5):len(p)]])
        if ((s < ths) and (m < thm)):
            bw_result_filter[ystart:yend, xstart:xend] = bw_result_orig[ystart:yend, xstart:xend]
    return bw_result_filter
def filter_result3(img, bw_result, ths, thm):
    bw_result_orig = np.copy(bw_result)
    points = np.where((bw_result > 0))
    points = np.reshape(points, np.shape(points))
    points = np.transpose(points)
    npoints = np.shape(points)[0]
    k = 20
    step = 5
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    sat = (hsv[:, :, 1] / 255)
    bw_result_filter = np.zeros(np.shape(bw_result))
    xc = points[:, 1]
    yc = points[:, 0]
    for ct in range(0, npoints, step):
        ystart = max(0, (yc[ct] - k))
        xstart = max(0, (xc[ct] - k))
        yend = min(np.shape(img)[0], (yc[ct] + k))
        xend = min(np.shape(img)[1], (xc[ct] + k))
        p = points[ct, :]
        p = np.reshape(p, (1, 2))
        Dpoints = distance.cdist(p, points)
        Dpoints = np.squeeze(Dpoints)
        ipoints = np.squeeze(np.where((Dpoints < 40)))
        xneigh = points[(ipoints, 1)]
        yneigh = points[(ipoints, 0)]
        (V, D) = eigen_cov(xneigh, yneigh)
        vmin = V[:, 0]
        if (D[1] < D[0]):
            vmin = V[:, 1]
        x1 = (xc[ct] - (k * vmin[0]))
        y1 = (yc[ct] - (k * vmin[1]))
        x2 = (xc[ct] + (k * vmin[0]))
        y2 = (yc[ct] + (k * vmin[1]))
        (p, px, py) = improfile(sat, np.array([x1, x2]), np.array([y1, y2]), 30)
        s = np.abs((np.mean(p[0:5]) - np.mean(p[(len(p) - 5):len(p)])))
        s = round((s * 100))
        m = np.max([p[0:5], p[(len(p) - 5):len(p)]])
        if ((s < ths) and (m < thm)):
            bw_result_filter[ystart:yend, xstart:xend] = bw_result_orig[ystart:yend, xstart:xend]
    return bw_result_filter