Ejemplo n.º 1
0
def main():
    img = cv2.imread('data/radial_gradient.jpg', cv2.IMREAD_GRAYSCALE)

    ret, thresh1_cv = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    ret, thresh2_cv = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
    ret, thresh3_cv = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
    ret, thresh4_cv = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
    ret, thresh5_cv = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
    thresh1, thresh2, thresh3, thresh4, thresh5 = threshold(img, 127, 255)

    titles = [
        'Original Image', None, 'BINARY', 'BINARY_CV', 'BINARY_INV',
        'BINARY_INV_CV', 'TRUNC', 'TRUNC_CV', 'TOZERO', 'TOZERO_CV',
        'TOZERO_INV', 'TOZERO_INV_CV'
    ]
    images = [
        img, None, thresh1, thresh1_cv, thresh2, thresh2_cv, thresh3,
        thresh3_cv, thresh4, thresh4_cv, thresh5, thresh5_cv
    ]
    for i in range(12):
        if titles[i] != None:
            plt.subplot(3, 4, i + 1)
            plt.imshow(images[i], 'gray', vmin=0, vmax=255)
            plt.title(titles[i])
            plt.xticks([]), plt.yticks([])
    plt.tight_layout()
    plt.show()
Ejemplo n.º 2
0
def rm_otsu_sunshade(img, roi, config):
    shadow = get_shadow(img, roi)
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # get threshold for shadow region
    # print('thr_in_shadow')
    thr_in_shadow = masked_otsu_threshold(img_gray, shadow)
    # get road markings in shadow
    _, rm_in_shadow = cv2.threshold(img_gray, thr_in_shadow, 255,
                                    cv2.THRESH_BINARY)
    # kernel_erode = np.ones((7,7), np.uint8)
    # shadow_eroded = cv2.erode(roi_shadow, kernel_erode)
    rm_in_shadow = cv2.bitwise_and(rm_in_shadow, rm_in_shadow, mask=shadow)
    # get threshold for sunlight region
    shadow_inv = cv2.bitwise_not(shadow)
    shadow_inv = cv2.bitwise_and(shadow_inv, shadow_inv, mask=roi)
    thr_out_shadow = masked_otsu_threshold(img_gray, shadow_inv)
    # get road markings not in shadow
    _, rm_out_shadow = cv2.threshold(img_gray, int(
        (thr_out_shadow * 1.5) % 255), 255, cv2.THRESH_BINARY)
    # rm_out_shadow = cv2.bitwise_and(rm_out_shadow, rm_out_shadow, mask=shadow_inv)
    # combine markings in shadow and not in shadow
    rm = cv2.bitwise_or(rm_in_shadow, rm_out_shadow)
    rm = cv2.bitwise_and(rm, rm, mask=roi)

    return rm
Ejemplo n.º 3
0
def get_rm_shadow(img, roi):
    # convert to hsv
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # Gaussian blur
    hsv = cv2.GaussianBlur(hsv, (5, 5), 0)
    # get saturation
    _, S, _ = cv2.split(hsv)
    # threshold saturation, shadows have higher sat than road
    _, shadow_mask = cv2.threshold(S, 70, 255, cv2.THRESH_BINARY)
    # get rid of noise
    kernel = np.ones((7, 7), np.uint8)
    shadow_mask = cv2.morphologyEx(shadow_mask,
                                   cv2.MORPH_CLOSE,
                                   kernel,
                                   iterations=1)
    # mask roi
    shadow_mask = cv2.bitwise_and(shadow_mask, roi)
    # get shadows from img
    shadows = cv2.bitwise_and(img, img, mask=shadow_mask)
    # make shadows grayscale
    shadows_gray = cv2.cvtColor(shadows, cv2.COLOR_BGR2GRAY)
    # get road markings in shadow
    _, rm_mask = cv2.threshold(shadows_gray, 100, 255, cv2.THRESH_BINARY)
    # clean up noise
    kernel = np.ones((2, 2), np.uint8)
    rm_mask = cv2.morphologyEx(rm_mask, cv2.MORPH_OPEN, kernel, iterations=1)
    # increase mask size a bit
    kernel = np.ones((3, 3), np.uint8)
    rm_mask = cv2.dilate(rm_mask, kernel)

    return rm_mask
Ejemplo n.º 4
0
def s2(img):  #segmentacionWarershed
    img = img
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(gray, 0, 255,
                                cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    # Eliminación del ruido
    kernel = np.ones((3, 3), np.uint8)
    opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
    # Encuentra el área del fondo
    sure_bg = cv2.dilate(opening, kernel, iterations=3)
    # Encuentra el área del primer
    dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
    ret, sure_fg = cv2.threshold(dist_transform, 0.7 * dist_transform.max(),
                                 255, 0)
    # Encuentra la región desconocida (bordes)
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg, sure_fg)
    # Etiquetado
    ret, markers = cv2.connectedComponents(sure_fg)
    # Adiciona 1 a todas las etiquetas para asegurra que el fondo sea 1 en lugar de cero
    markers = markers + 1
    # Ahora se marca la región desconocida con ceros
    markers[unknown == 255] = 0
    markers = cv2.watershed(img, markers)
    img[markers == -1] = [255, 0, 0]
    return img
Ejemplo n.º 5
0
def demo_thersholding(img,
                      threshold=None,
                      show=True,
                      thresh_type=cv2.THRESH_BINARY_INV):
    plt.figure(654)

    hist, bins = np.histogram(img.ravel(), 256, [0, 256])

    threshold = threshold if threshold is not None else bins[hist.argmax()]
    # threshold = threshold if threshold is not None else np.median(img)
    # aimg = cv2.cvtColor(img/255, cv2.COLOR_BGR2GRAY)  # or convert
    equ = cv2.equalizeHist(img.astype(np.uint8))

    threshold, equ_threshed = cv2.threshold(equ, threshold, equ.max(),
                                            thresh_type)

    if show:
        # Show histogram
        cdf = hist.cumsum()
        cdf_normalized = cdf * hist.max() / cdf.max()
        plt.plot(cdf_normalized, color='b')
        plt.hist(img.flatten(), 256, [0, 256], color='r')
        plt.xlim([0, 256])
        plt.legend(('cdf', 'histogram'), loc='upper left')
        plt.show()

        # Compare images
        res = np.hstack(
            (img, equ.astype(np.float)))  # stacking images side-by-side
        threshold, ret = cv2.threshold(res, threshold, res.max(), thresh_type)
        ret = ret / 255.
        cv2.imshow('try...', ret)
        cv2.waitKey(0)
    return equ_threshed
Ejemplo n.º 6
0
def watershed_demo(image):
    blurred = cv.pyrMeanShiftFiltering(image, 10, 100)
    gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
    cv.imshow("binary", binary)

    kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
    mb = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel, iterations=2)
    sure_bg = cv.dilate(binary, kernel, iterations=3)
    cv.imshow("mor", sure_bg)

    dist = cv.distanceTransform(mb, cv.DIST_L2, 3)
    dist_output = cv.normalize(dist, 0, 1.0, cv.NORM_MINMAX)
    cv.imshow("dist", dist_output * 50)

    ret, surface = cv.threshold(dist, dist.max() * 0.6, 255, cv.THRESH_BINARY)
    cv.imshow("interface", surface)

    surface_fg = np.uint8(surface)
    unknow = cv.subtract(sure_bg, surface_fg)
    ret, markers = cv.connectedComponents(surface_fg)
    print(ret)

    markers += 1
    markers[unknow == 255] = 0
    markers = cv.watershed(image, markers=markers)
    image[markers == -1] = [0, 0, 255]
    cv.imshow("result", image)
Ejemplo n.º 7
0
def extract_all_words(image, filter='CAPS'):
    """extract all of the words form the image.

    extracts words from the inverse and normal threshold of the input image so
    and joins these lists together. this is because pytesseract likes black text
    on a white background.

    :param image: input image
    :type image: cv2 image
    :return: extracted words
    :rtype: list
    """
    # standard and inverse threshold so that at least one of the images
    # is black text on white background tessearact likes this more
    variations = []
    _, thresh = cv2.threshold(convert.bgr_to_gray(image), 127, 255,
                              cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    variations.append(thresh)
    _, thresh_inv = cv2.threshold(convert.bgr_to_gray(image), 127, 255,
                                  cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
    variations.append(thresh_inv)

    # get the text from each version of the image
    if filter is 'CAPS':
        text = []
        for v in variations:
            text.append(get_caps(v))
        return text
    elif filter is 'NUMS':
        text = []
        for v in variations:
            text.append(get_nums(v))
        return text
Ejemplo n.º 8
0
 def ink_test(self, location):
     """
     Function checks the ink spillage in tire. 
     Returns 0 or 1, 1 for defective.
     """
     frame = cv2.imread(location)
     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
     _, thresh = cv2.threshold(gray, 150, 200, 0)
     contours, _ = cv2.findContours(thresh, cv2.RETR_TREE,
                                    cv2.CHAIN_APPROX_SIMPLE)
     cv2.fillPoly(frame, pts=[contours[17]], color=(0, 0, 0))
     cv2.fillPoly(frame, pts=[contours[34]], color=(0, 0, 0))
     cv2.fillPoly(frame, pts=[contours[44]], color=(0, 0, 0))
     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
     _, thresh = cv2.threshold(gray, 165, 165, 0)
     _, contours = cv2.findContours(thresh, cv2.RETR_TREE,
                                    cv2.CHAIN_APPROX_SIMPLE)
     count = 0
     for _ in contours:
         count = count + 1
     if count > 0:
         flag = 1
     else:
         flag = 0
     return flag
Ejemplo n.º 9
0
def process_image(path, out_path):

    orig_im = Image.open(path)
    scale, im = downscale_image(orig_im)

    edges = cv2.Canny(np.asarray(im), 100, 200)

    # TODO: dilate image _before_ finding a border. This is crazy sensitive!
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
    borders = find_border_components(contours, edges)
    borders.sort(key=lambda i_x1_y1_x2_y2: (i_x1_y1_x2_y2[3] - i_x1_y1_x2_y2[
        1]) * (i_x1_y1_x2_y2[4] - i_x1_y1_x2_y2[2]))

    border_contour = None
    if len(borders):
        border_contour = contours[borders[0][0]]
        edges = remove_border(border_contour, edges)

    edges = 255 * (edges > 0).astype(np.uint8)

    # Remove ~1px borders using a rank filter.
    maxed_rows = rank_filter(edges, -4, size=(1, 20))
    maxed_cols = rank_filter(edges, -4, size=(20, 1))
    debordered = np.minimum(np.minimum(edges, maxed_rows), maxed_cols)
    edges = debordered

    contours = find_components(edges)
    if len(contours) == 0:
        print('%s -> (no text!)' % path)
        return

    crop = find_optimal_components_subset(contours, edges)
    crop = pad_crop(crop, contours, edges, border_contour)

    crop = [int(x / scale)
            for x in crop]  # upscale to the original image size.

    #draw = ImageDraw.Draw(im)
    #c_info = props_for_contours(contours, edges)
    #for c in c_info:
    #    this_crop = c['x1'], c['y1'], c['x2'], c['y2']
    #    draw.rectangle(this_crop, outline='blue')
    #draw.rectangle(crop, outline='red')
    #im.save(out_path)
    #draw.text((50, 50), path, fill='red')
    #orig_im.save(out_path)
    #im.show()
    text_im = orig_im.crop(crop)
    text_im = text_im.convert('RGB')
    bytesim = cv2.cvtColor(np.asarray(text_im), cv2.COLOR_BGR2GRAY)
    ret, bytesim = cv2.threshold(np.asarray(bytesim), 127, 255,
                                 cv2.THRESH_BINARY)
    if img_estim(bytesim, 127) == 'dark':
        ret, bytesim = cv2.threshold(np.asarray(text_im), 127, 255,
                                     cv2.THRESH_BINARY_INV)
    text_im = Image.fromarray(bytesim)
    text_im.save(out_path)
    print('%s -> %s' % (path, out_path))
Ejemplo n.º 10
0
 def generateMasks(self, iCOR, iBG, THRESHOLD_ONE):
     iRES = iCOR - iBG
     absiRES = np.abs(iRES)
     temp = THRESHOLD_ONE - absiRES
     temp = np.clip(temp, a_min=0, a_max=255)
     update_BG_mask = cv2.threshold(temp, 0, 1, cv2.THRESH_BINARY_INV)[1]
     freeze_update_BG_mask = cv2.threshold(temp, 0, 1, cv2.THRESH_BINARY)[1]
     return update_BG_mask, freeze_update_BG_mask
Ejemplo n.º 11
0
 def mine_remain(self):
     self.look()
     digit_1 = self.sight[78 * self.pixel_size // 20: 106 * self.pixel_size // 20, 26 * self.pixel_size // 20: 41 * self.pixel_size // 20, 2]
     digit_2 = self.sight[78 * self.pixel_size // 20: 106 * self.pixel_size // 20, 42 * self.pixel_size // 20: 57 * self.pixel_size // 20, 2]
     digit_3 = self.sight[78 * self.pixel_size // 20: 106 * self.pixel_size // 20, 59 * self.pixel_size // 20: 74 * self.pixel_size // 20, 2]
     _, th_1 = cv2.threshold(digit_1, 140, 255, cv2.THRESH_BINARY)
     _, th_2 = cv2.threshold(digit_2, 140, 255, cv2.THRESH_BINARY)
     _, th_3 = cv2.threshold(digit_3, 140, 255, cv2.THRESH_BINARY)
     return digit_ocr(th_1) * 100 + digit_ocr(th_2) * 10 + digit_ocr(th_3)
Ejemplo n.º 12
0
def process_image(maze_raw):
    maze_raw_G = maze_raw[:, :, 1]
    _, maze_raw_G = cv2.threshold(maze_raw_G, 100, 255, 0)

    maze_raw_B = maze_raw[:, :, 2]
    _, maze_raw_B = cv2.threshold(maze_raw_B, 100, 255, 0)

    processed_image = maze_raw_B + maze_raw_G
    return processed_image
Ejemplo n.º 13
0
def check_border(file_name):
    global a4_y_size_px
    img = imread(file_name)
    a4_y_size_px = img.shape[0]
    a4_x_size_px = img.shape[1]
    gray_image = cvtColor(img, COLOR_BGR2GRAY)
    ret, mask = cv2.threshold(gray_image, 180, 255, cv2.THRESH_BINARY)
    image_final = cv2.bitwise_and(gray_image, gray_image, mask=mask)
    ret, gray_image = cv2.threshold(
        image_final, 180, 255,
        cv2.THRESH_BINARY_INV)  # for black text , cv.THRESH_BINARY_INV
    kernel = cv2.getStructuringElement(
        cv2.MORPH_CROSS, (3, 3)
    )  # to manipulate the orientation of dilution , large x means horizonatally dilating  more, large y means vertically dilating more
    gray_image = cv2.dilate(
        gray_image, kernel,
        iterations=15)  # dilate , more the iteration more the dilation
    gray_image = Canny(gray_image, 20, 150, apertureSize=7)
    lines = HoughLines(gray_image, 10, np.pi / 180, 54)
    horisontal_lines = []
    vertical_lines = []
    for line in lines:
        rho, theta = line[0]
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a * rho
        y0 = b * rho
        x1 = int(x0 + 3000 * (-b))
        y1 = int(y0 + 3000 * (a))
        y2 = int(y0 - 3000 * (a))
        x2 = int(x0 - 3000 * (-b))
        if abs(y1 - y2) < 2 or x2 == x1:
            if x2 != x1:
                horisontal_lines.append((x1, x2, y1, y2))
            else:
                vertical_lines.append((x1, x2, y1, y2))
            #cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
    horisontal_lines.sort(key=lambda x: x[2])
    vertical_lines.sort(key=lambda x: x[0])
    horisontal_lines = map(
        lambda x: (x[0], x[1], float(x[2]) / a4_y_size_px * a4_y_size_mm,
                   float(x[3]) / a4_y_size_px * a4_y_size_mm),
        horisontal_lines)
    vertical_lines = map(
        lambda x: (float(x[0]) / a4_y_size_px * a4_y_size_mm, float(x[1]) /
                   a4_y_size_px * a4_y_size_mm, x[2], x[3]), vertical_lines)
    check_upper_border(img, horisontal_lines[0], a4_y_size_mm, a4_y_size_px,
                       20.0, 0.5)
    check_lower_border(img, horisontal_lines[-1], a4_y_size_mm, a4_y_size_px,
                       15.0, 0.5)
    check_left_border(img, vertical_lines[0], a4_x_size_mm, a4_x_size_px, 30,
                      0.5)
    check_right_border(img, vertical_lines[-1], a4_x_size_mm, a4_x_size_px, 15,
                       0.5)
    plt.imshow(img, cmap='gray')
    plt.show()
Ejemplo n.º 14
0
def onthreshold(x):
    value = cv2.getTrackbarPos("value", "Threshold")
    a, binary = cv2.threshold(gauss, value, maxvalue, cv2.THRESH_BINARY)
    b, binary_inv = cv2.threshold(gauss, value, maxvalue,
                                  cv2.THRESH_BINARY_INV)
    c, trunc = cv2.threshold(gauss, value, maxvalue, cv2.THRESH_TRUNC)
    d, to_zero = cv2.threshold(gauss, value, maxvalue, cv2.THRESH_TOZERO)
    e, to_zero_inv = cv2.threshold(gauss, value, maxvalue,
                                   cv2.THRESH_TOZERO_INV)
    if (a):
        cv2.imshow("Binary", binary)
Ejemplo n.º 15
0
def image_processing(imageA, img0, lang, csv_file):
    count = 0
    img = imageA.copy()
    # prepare image quality for OCR
    img = cv2.bitwise_not(img)
    _, img_recog = cv2.threshold(img, 210, 255, cv2.THRESH_BINARY)
    _, img = cv2.threshold(img, 224, 255, cv2.THRESH_BINARY)

    #find text areas
    imgBi = cv2.bitwise_not(imageA)
    _, binary2 = cv2.threshold(imgBi, 0, 255, cv2.THRESH_BINARY)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 20))
    eroded = cv2.erode(binary2, kernel, iterations=1)
    erodedBi = cv2.bitwise_not(eroded)
    contours2, hierarchy2 = cv2.findContours(erodedBi, cv2.RETR_EXTERNAL,
                                             cv2.CHAIN_APPROX_SIMPLE)

    # find head area for OCR text with color
    headArea = img_recog[104:204, 319:1493]
    erodedHead = cv2.erode(headArea, kernel, iterations=1)
    erodedHead = cv2.bitwise_not(erodedHead)
    contours, hierarchy = cv2.findContours(erodedHead, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)

    for i in range(len(contours)):
        x, y, w, h = cv2.boundingRect(contours[i])
        if w < 1000:
            count += 1
            cv2.rectangle(img0, (x + 319, y + 104), (x + 319 + w, y + 104 + h),
                          (0, 255, 0), 2)
            crop_img = headArea[y:y + h, x:x + w]
            cv2.imwrite('ref.png', crop_img)
            text = tesserocr.image_to_text(Image.open('ref.png'), lang)
            text = text.replace(" ", "")
            text = text.replace("\n", " ")
            csv_file.write('{}:,{},{},{},{},{}\n'.format(
                count, x, y, w, h, text.encode('utf-8')))

    for j in range(len(contours2)):
        cnt2 = contours2[j]
        x2, y2, w2, h2 = cv2.boundingRect(cnt2)
        if x2 > 120 and y2 > 200 and 2 < w2 and 2 < h2 < 450:
            count += 1
            cv2.rectangle(img0, (x2, y2), (x2 + w2, y2 + h2), (0, 255, 0), 2)
            crop_img = img_recog[y2:y2 + h2, x2:x2 + w2]
            cv2.imwrite('ref.png', crop_img)
            text = tesserocr.image_to_text(Image.open('ref.png'), lang)
            text = text.replace(" ", "")
            text = text.replace("\n", " ")
            csv_file.write('{}:,{},{},{},{},{}\n'.format(
                count, x2, y2, w2, h2, text.encode('utf-8')))
        else:
            pass
Ejemplo n.º 16
0
def image_transformation(file):
    # open image and apply filter
    img = cv2.imdecode(numpy.frombuffer(file, numpy.uint8),
                       cv2.IMREAD_UNCHANGED)
    img = cv2.resize(img, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    kernel = numpy.ones((1, 1), numpy.uint8)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)
    img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

    # get coordinates
    coords = numpy.column_stack(numpy.where(img > 0))
    angle = cv2.minAreaRect(coords)[-1]
    # the `cv2.minAreaRect` function returns values in the
    # range [-90, 0); as the rectangle rotates clockwise the
    # returned angle trends to 0 -- in this special case we
    # need to add 90 degrees to the angle
    if angle < -45:
        angle = -(90 + angle)
    # otherwise, just take the inverse of the angle to make
    # it positive
    else:
        angle = -angle

    (h, w) = img.shape[:2]
    center = (w // 2, h // 2)
    m = cv2.getRotationMatrix2D(center, angle, 1.0)
    rotated = cv2.warpAffine(img,
                             m, (w, h),
                             flags=cv2.INTER_CUBIC,
                             borderMode=cv2.BORDER_REPLICATE)
    img = rotated

    ret, thresh_binary = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    value_thresh_binary = cv2.Laplacian(thresh_binary, cv2.CV_64F).var()

    blur = cv2.GaussianBlur(img, (5, 5), 0)
    # blur = gaussian_blur(img, 5)
    # img = otsu(blur)
    ret3, thresh_otsu = cv2.threshold(blur, 0, 255,
                                      cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    value_otsu = cv2.Laplacian(thresh_otsu, cv2.CV_64F).var()

    if value_thresh_binary > value_otsu:
        print(value_thresh_binary)
        img = thresh_binary
    else:
        print(value_otsu)
        img = thresh_otsu
    return img
Ejemplo n.º 17
0
def threshold(gray, whiteBG = False):
    if(whiteBG == False):
        _, thres = cv2.threshold(gray,0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
        kernel = np.ones((2, 2), np.uint8)
        thres = cv2.erode(thres, kernel)
    
    else:
        inverted = cv2.bitwise_not(gray)
        _, thres = cv2.threshold(inverted,40,255,cv2.THRESH_BINARY_INV)
        kernel = np.ones((2, 2), np.uint8)
        thres = cv2.erode(thres, kernel)

    cv2.imwrite(path + "thres.jpg", thres)
    return thres
Ejemplo n.º 18
0
def sizeGrowth(image):

    # gray = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
    gray8UC1 = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ret, thresh1 = cv2.threshold(gray8UC1, 95, 30, cv2.THRESH_BINARY)
    pixelCountValue = cv2.countNonZero(thresh1)
    contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_NONE)
    img = cv2.drawContours(image, contours, -1, (255, 255, 255), 1)
    gray8UC1_test = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret1, thresh12 = cv2.threshold(gray8UC1_test, 254, 255, cv2.THRESH_BINARY)
    pixelContourValue = cv2.countNonZero(thresh12)

    return pixelCountValue - pixelContourValue  #pixel value of filament
Ejemplo n.º 19
0
def _BIN_and_OTSU(img: object):

    # Aplicando o filtro gaussian blur para preservar os edges
    gauss_blur = cv2.GaussianBlur(img, (3, 3), 0)

    # Aplicando o threshold Binário + OTSU na imagem com filtro gaussiano
    img_th2 = cv2.threshold(gauss_blur, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

    black_pixels = _count_black_pixels(img_th2)

    if black_pixels > 0.80 * img.size:
        img_th2 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

    return img_th2
Ejemplo n.º 20
0
def genNeedImg(img_path,
               img_type='binary',
               binary_therhold=127,
               binary_revese=False,
               size=None,
               save=False,
               path='./'):
    '''
    用于生成指定大小的灰度图或二值图, img_path为图像路径
    type为标志转换类型,默认为binary,可选的值为binary或gray
    binary_therhold为二值图划分阈值,默认127(即大于127的像素设置为255,否则置0)
    binary_revese默认为False,True时黑白颠倒(即大于127的像素设置为0,否则置255)
    size为tuple类型,用于指定生成图像的尺寸, 如:(512,512),默认为None表示输出原图像尺寸
    save为保存标志,默认为False,为true时将生成的图保存到path(默认为当前文件夹)
    '''
    img_raw = cv.imread(img_path)
    if size != None:  # 调整图像尺寸
        img_raw = cv.resize(img_raw, size)
    img_gray = cv.cvtColor(img_raw, cv.COLOR_RGB2GRAY)  # 转换颜色空间为灰度
    # Add some extra padding around the image
    # img_gray = cv.copyMakeBorder(img_gray, 8, 8, 8, 8, cv.BORDER_REPLICATE)
    img_name = img_path[9:].split('.')[0]  # 获取图像原始名称
    if img_type == 'gray':  # 生成灰度图
        if save:
            cv.imwrite(os.path.join(path, '{}_gray.bmp'.format(img_name)),
                       img_gray)
            print('Gray image saved at {}'.format(
                os.path.join(path, '{}_gray.bmp'.format(img_name))))
        else:
            print('Gray image generated!')
            return img_gray
    else:  # 生成二值图
        if binary_revese:
            ret, img_binary = cv.threshold(img_gray, binary_therhold, 255,
                                           cv.THRESH_BINARY_INV)  #反二进制阈值化
        else:
            ret, img_binary = cv.threshold(img_gray, binary_therhold, 255,
                                           cv.THRESH_BINARY)  # 二进制阈值化
        if save:
            cv.imwrite(os.path.join(path, '{}_binary.bmp'.format(img_name)),
                       img_binary)
            print('threshold:{}'.format(ret))  # 输出转换阈值
            print('Binary image savd at {}'.format(
                os.path.join(path, '{}_binary.bmp'.format(img_name))))
        else:
            print('Binary image generated!')
            print('threshold:{}'.format(ret))  # 输出转换阈值
            return img_binary
Ejemplo n.º 21
0
def id_verification(filepath):
    try:
        img = cv2.imread(filepath)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        th, threshed = cv2.threshold(gray, 127, 255, cv2.THRESH_TRUNC)

        res = {}
        result = pytesseract.image_to_string(threshed)
        for word in result.split("\n"):
            # normalize NIK
            if "NIK" in word:
                nik_char = word.split()
                if "D" in word:
                    word = word.replace("D", "0")
                if "?" in word:
                    word = word.replace("?", "7")
                if "b" in word:
                    word = word.replace("b", "6")
            kata = " ".join(word.split())
            if ">" in kata and ":" not in kata:
                data = kata.split('>')
                res[data[0].strip()] = data[1].strip()
            if ":" in kata and ">" not in kata:
                data = kata.split(':')
                res[data[0].strip()] = data[1].strip()
            if "-" in kata and ">" not in kata and ":" not in kata:
                data = kata.split('-')
                res[data[0].strip()] = data[1].strip()
        ret = {'status': 200, 'message': "Extracted Data", 'results': res}
        return ret
    except Exception as e:
        ret = {'status': 500, 'message': e.args}
        return ret
Ejemplo n.º 22
0
def get_OCR(filename):
    custom_config = r'--oem 3 --psm 6 -l kor+kor_vert+eng'

    img = cv2.imread(filename)
    bi_img = rgb2binary(img)
    bi_img2 = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_RGB2GRAY), 0, 255,
                            cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

    msg = pytesseract.image_to_string(bi_img, config=custom_config)
    words1 = pytesseract.image_to_data(img,
                                       config=custom_config,
                                       output_type=pytesseract.Output.DICT)
    words2 = pytesseract.image_to_data(bi_img,
                                       config=custom_config,
                                       output_type=pytesseract.Output.DICT)
    words3 = pytesseract.image_to_data(bi_img2,
                                       config=custom_config,
                                       output_type=pytesseract.Output.DICT)
    print(msg)

    bi_img = cv2.cvtColor(bi_img, cv2.COLOR_GRAY2RGB)
    bi_img2 = cv2.cvtColor(bi_img2, cv2.COLOR_GRAY2RGB)
    img_box1 = boxing(img, words1)
    img_box2 = boxing(bi_img, words2)
    img_box3 = boxing(bi_img2, words3)

    # img1 = np.hstack((img, bi_img, bi_img2))
    img = np.hstack((img_box1, img_box2, img_box3))
    # img = np.vstack((img1, img2))
    cv2.imshow('img', img)
    cv2.waitKey()
Ejemplo n.º 23
0
def main(argv):
    results = img_cmp(argv[1], argv[2])
    score = results[0]
    diff = results[1]
    diff = (diff * 255).astype("uint8")
    print("Structural Similarity Index: {}".format(score))

    thresh = cv2.threshold(diff, 0, 255,
        cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)

    image_orig = cv2.imread(argv[1])
    image_mod = cv2.imread(argv[2])
    resized_orig = cv2.resize(image_orig, (300, 200))   
    resized_mod = cv2.resize(image_mod, (300, 200))

    for c in cnts:
        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(resized_orig, (x, y), (x + w, y + h), (0, 0, 255), 2)
        cv2.rectangle(resized_mod, (x, y), (x + w, y + h), (0, 0, 255), 2)
    cv2.imshow("Original", resized_orig)
    cv2.imshow("Modified", resized_mod)
    cv2.imshow("Diff", diff)
    #cv2.imshow("Thresh", thresh)
    cv2.waitKey(0)

    plt.show()
Ejemplo n.º 24
0
    def extract_section_coordinates_from_image(self, image,
                                               threshold_breakpoint):
        # Takes an raw image with a white bright background and looks for the contour of
        # a rectangular object and returns the coordinates representing a polygon shape of that object.
        # A threshold needs to be provided representing an acceptable breaking point at which the coordinates will be returned

        # The width of the border to wrap the image in case the object overflows the image
        BORDER_WIDTH = 100

        image = cv2.copyMakeBorder(image,
                                   BORDER_WIDTH,
                                   BORDER_WIDTH,
                                   BORDER_WIDTH,
                                   BORDER_WIDTH,
                                   cv2.BORDER_CONSTANT,
                                   value=[255, 255, 255])
        image_grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # Calculate the image area
        image_height, image_width = image.shape[:2]
        image_area = image_height * image_width
        # Repeat to find the right threshold value for finding a rectangle
        found = False
        # Increment the threshold until a contour is found
        threshold_current = threshold_breakpoint
        while found is False:
            if threshold_breakpoint < 200:
                threshold_breakpoint = threshold_current + 5
                threshold_current = threshold_breakpoint
            # Extract contours using threshold
            _, threshold = cv2.threshold(image_grayscale, threshold_breakpoint,
                                         255, cv2.THRESH_BINARY)
            contours, _ = cv2.findContours(threshold, cv2.RETR_LIST,
                                           cv2.CHAIN_APPROX_NONE)
            # Go through each contour that could be extracted from the image
            for contour in contours:
                contour_area = cv2.contourArea(contour)
                if contour_area > (image_area /
                                   6) and contour_area < (image_area / 1.01):
                    epsilon = 0.1 * cv2.arcLength(contour, True)
                    # Close open lines into a complete wrapped shade
                    approx = cv2.approxPolyDP(contour, epsilon, True)
                    # When the shape can be wrapped the contour rectangle has been found
                    if len(approx) == 4:
                        found = True
                    # Otherwise keep decrementing the threshold value until it's found
                    else:
                        threshold_breakpoint = threshold_breakpoint - 1
                        break
                    # Set and return coordinates from approximate
                    coordinates = numpy.empty((4, 2), dtype="float32")
                    # Top-left
                    coordinates[0] = approx[0] - BORDER_WIDTH
                    # Top-right
                    coordinates[1] = approx[1] - BORDER_WIDTH
                    # Bottom-right
                    coordinates[2] = approx[2] - BORDER_WIDTH
                    # Bottom-left
                    coordinates[3] = approx[3] - BORDER_WIDTH
                    return coordinates
Ejemplo n.º 25
0
 def to_counters(self, _img=None):
     # # 检测轮廓 边缘分割
     _img = _img if _img is not None else self.img
     # # 灰度和二值
     _gray = cv2.cvtColor(_img, cv2.COLOR_BGR2GRAY)
     _, _binary = cv2.threshold(_gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
     # cv_show(_binary, "binary test")
     # # Canny边缘检测
     # edges = cv2.Canny(_img, 50, 150, )
     # cv_show(edges)
     # # 轮廓检测, 画出轮廓
     _contours, _ = cv2.findContours(_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
     _draw_img = _img.copy()
     # # 计算轮廓的大小, 选定阈值 对轮廓进行选择
     _greatest_contour_idx = np.argmax([cv2.contourArea(_contour) for _contour in _contours])
     # # 求轮廓的外接矩形
     _greatest_contour = _contours[_greatest_contour_idx]
     _x, _y, _w, _h = cv2.boundingRect(_greatest_contour)
     _ret = cv2.rectangle(_img, (_x, _y), (_x + _w, _y + _h), color=(0, 255, 255), thickness=1)
     # # 上下
     _ret[0: _y, :] = (0, 0, 0)
     _ret[_y + _h: -1, :] = (0, 0, 0)
     # # 左右
     _ret[:, 0: _x] = (0, 0, 0)
     _ret[:, _x + _w: -1] = (0, 0, 0)
     # _ret = cv2.drawContours(_draw_img, _contours, _greatest_contour_idx, (255, 0, 255), 2)
     cv_show(_ret)
Ejemplo n.º 26
0
    def convexityDetectionImg(self):
        try:
            img = cv2.imread(self.filename)
            img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            ret, thresh = cv2.threshold(img_gray, 127, 255, 0)
            contours, hierarchy = cv2.findContours(thresh, 2, 1)
            cnt = contours[0]

            hull = cv2.convexHull(cnt, returnPoints=False)
            defects = cv2.convexityDefects(cnt, hull)

            for i in range(defects.shape[0]):
                s, e, f, d = defects[i, 0]
                start = tuple(cnt[s][0])
                end = tuple(cnt[e][0])
                far = tuple(cnt[f][0])
                cv2.line(img, start, end, [0, 255, 0], 2)
                cv2.circle(img, far, 5, [0, 0, 255], -1)

            cv2.imwrite('img/dist/disbukey.png', img,
                        [cv2.IMWRITE_JPEG_QUALITY, 100])
            cv2.waitKey(0)
            cv2.destroyAllWindows()
        except:
            pass
Ejemplo n.º 27
0
def contours(img, adaptive=True):
    """
    Finds contours given an img

    :param img: image
    :return contours: contours of the image
    :return hierarchy:
    """

    blur = cv2.medianBlur(img, 5)
    grayscale = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)

    if adaptive is False:
        _, thresh = cv2.threshold(grayscale, 0, 255,
                                  cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    else:
        thresh = cv2.adaptiveThreshold(grayscale, 255,
                                       cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                       cv2.THRESH_BINARY_INV, 3, 2)

    # kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25,25))
    # opening = cv2.morpholfirst_frameyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=3)

    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)
    return contours, hierarchy
Ejemplo n.º 28
0
def imgSkeleton(original_img):
    ret, binary_img = cv2.threshold(original_img, 0, 1,
                                    cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
    extracted_img = skeletonize(binary_img)
    skeleton = extracted_img.astype(np.uint8) * 255
    skeleton = cv2.bitwise_not(skeleton)
    return skeleton
Ejemplo n.º 29
0
    def _preprocess(self, warped_img):
        '''
        Preprocess the warped and rotated image.

        @warped_img:
            np.array, it should be the output of self._polar_warp_and_rotate().
        @return:
            (s_mask, output_img), saturation mask and image after preprocessing.
        '''
        warped_img = cv.GaussianBlur(warped_img, (3, 3), 1.5)
        hsv = cv.cvtColor(warped_img, cv.COLOR_BGR2HSV)
        warped_img = cv.cvtColor(warped_img, cv.COLOR_BGR2GRAY)
        warped_img = cv.equalizeHist(warped_img)  # Enhance contrast

        _, s, _ = cv.split(hsv)
        _, s = cv.threshold(s, 0, 255, cv.THRESH_OTSU)
        s = cv.morphologyEx(s, cv.MORPH_ERODE, np.ones((5, 5)))
        _, contours, _ = cv.findContours(s, cv.RETR_TREE,
                                         cv.CHAIN_APPROX_SIMPLE)
        contours = sorted(contours, key=lambda ctr: cv.contourArea(ctr)
                          )  # Sort to choose the largest area
        mask = cv.drawContours(np.zeros((warped_img.shape), np.uint8),
                               contours,
                               len(contours) - 1, (255, 255, 255),
                               thickness=1)
        box = cv.boundingRect(get_points(mask))  # Largest area box-bouding
        mask = cv.rectangle(mask, (box[0], box[1]),
                            (box[0] + box[2], box[1] + box[3]),
                            (255, 255, 255),
                            cv.FILLED)  # Fill the area that is to be removed
        mask = cv.bitwise_not(mask)  # Ensure tooth existing area
        return mask, warped_img
Ejemplo n.º 30
0
def convex1(ImgNo, defThr=127):
    img = cv2.imread(impDef.select_img(ImgNo))
    imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    ret, thr = cv2.threshold(imgray, defThr, 255, 0)
    contours, _ = cv2.findContours(thr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    # 원본 이미지의 6번째 contour인 contours[5]가 단풍잎 모양의 외곽을 에워싸고 있는 contour
    cnt = contours[5]

    # contour에 외접하는 똑바로 세워진 사각형을 얻기 위해 cv2.boundingRect() 함수를 이용합니다.
    x, y, w, h = cv2.boundingRect(cnt)
    #cv2.boudingRect()함수는 인자로 받은 contour에 외접하고 똑바로 세워진 직사각형의
    # 좌상단 꼭지점 좌표 (x, y)와 가로 세로 폭을 리턴합니다.
    # 이렇게 얻은 좌표를 이용해 원본 이미지에 빨간색으로 사각형을 그립니다.
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3)

    rect = cv2.minAreaRect(cnt)
    #cv2.minAreaRect() 함수는 인자로 입력한 contour에 외접하면서 면적이 가장 작은 직사각형을 구하는데 활용됩니다.
    # 이 함수의 리턴값은 좌상단 꼭지점 좌표 (x, y), 가로 세로 폭과 이 사각형이 기울어진 각도입니다.
    box = cv2.boxPoints(rect)
    #v2.boxPoints() 함수는 cv2.minAreaRect() 함수로 얻은 직사각형의 꼭지점 4개의 좌표를 얻기 위해 사용됩니다.
    box = np.int0(box)
    #좌표는 float형으로 리턴되므로 np.int0()로 정수형 값으로 전환한 후, 원본 이미지에 초록색 사각형을 그리는 겁니다.

    cv2.drawContours(img, [box], 0, (0, 255, 2), 3)
    cv2.imshow('retangle', img)

    impDef.close_window()