Python cv2.fastNlMeansDenoising() Examples

The following are 8 code examples of cv2.fastNlMeansDenoising(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cv2 , or try the search function .
Example #1
Source File: preprocessor.py    From signature-recognition with MIT License 7 votes vote down vote up
def prepare(input):
    # preprocessing the image input
    clean = cv2.fastNlMeansDenoising(input)
    ret, tresh = cv2.threshold(clean, 127, 1, cv2.THRESH_BINARY_INV)
    img = crop(tresh)

    # 40x10 image as a flatten array
    flatten_img = cv2.resize(img, (40, 10), interpolation=cv2.INTER_AREA).flatten()

    # resize to 400x100
    resized = cv2.resize(img, (400, 100), interpolation=cv2.INTER_AREA)
    columns = np.sum(resized, axis=0)  # sum of all columns
    lines = np.sum(resized, axis=1)  # sum of all lines

    h, w = img.shape
    aspect = w / h

    return [*flatten_img, *columns, *lines, aspect] 
Example #2
Source File: squeeze.py    From EvadeML-Zoo with MIT License 6 votes vote down vote up
def non_local_means_bw_py(imgs, search_window, block_size, photo_render):
    import cv2
    ret_imgs = opencv_wrapper(imgs, cv2.fastNlMeansDenoising, [None,photo_render,block_size,search_window])
    return ret_imgs 
Example #3
Source File: main.py    From sbb_textline_detection with Apache License 2.0 5 votes vote down vote up
def cleaning_probs(self, probs: np.ndarray, sigma: float) -> np.ndarray:
        # Smooth
        if sigma > 0.:
            return cv2.GaussianBlur(probs, (int(3 * sigma) * 2 + 1, int(3 * sigma) * 2 + 1), sigma)
        elif sigma == 0.:
            return cv2.fastNlMeansDenoising((probs * 255).astype(np.uint8), h=20) / 255
        else:  # Negative sigma, do not do anything
            return probs 
Example #4
Source File: binarization.py    From dhSegment with GNU General Public License v3.0 5 votes vote down vote up
def cleaning_probs(probs: np.ndarray, sigma: float) -> np.ndarray:
    # Smooth
    if sigma > 0.:
        return cv2.GaussianBlur(probs, (int(3*sigma)*2+1, int(3*sigma)*2+1), sigma)
    elif sigma == 0.:
        return cv2.fastNlMeansDenoising((probs*255).astype(np.uint8), h=20)/255
    else:  # Negative sigma, do not do anything
        return probs 
Example #5
Source File: nonlocalmeans_functionals.py    From odl with Mozilla Public License 2.0 5 votes vote down vote up
def proximal(self):
        func = self

        class NLMProximal(Operator):
            def __init__(self, stepsize):
                super(NLMProximal, self).__init__(
                    func.domain, func.domain, linear=False)
                self.stepsize = stepsize

            def _call(self, x):
                h = func.h * self.stepsize

                if func.impl == 'skimage':
                    from skimage.restoration import denoise_nl_means
                    x_arr = x.asarray()
                    return denoise_nl_means(
                        x_arr,
                        patch_size=func.patch_size,
                        patch_distance=func.patch_distance,
                        h=h,
                        multichannel=False)
                elif func.impl == 'opencv':
                    import cv2
                    x_arr = x.asarray()
                    xmin, xmax = np.min(x_arr), np.max(x_arr)
                    x_arr = (x_arr - xmin) * 255.0 / (xmax - xmin)
                    x_arr = x_arr.astype('uint8')

                    h_scaled = h * 255.0 / (xmax - xmin)
                    res = cv2.fastNlMeansDenoising(
                        x_arr,
                        templateWindowSize=func.patch_size,
                        searchWindowSize=2 * func.patch_distance + 1,
                        h=h_scaled)

                    return res * (xmax - xmin) / 255.0 + xmin
        return NLMProximal 
Example #6
Source File: idcardocr.py    From idmatch with MIT License 5 votes vote down vote up
def recognize_card(idcard):
    result = []
    # TODO: 
    # process_image(original_image, cropped_image)
    # idcard = cv2.imread(cropped_, cv2.COLOR_BGR2GRAY)

    # In some cases resized image gives worse results
    # idcard = resize(idcard, width=720)

    gray = cv2.cvtColor(idcard, cv2.COLOR_BGR2GRAY)
    denoised = cv2.fastNlMeansDenoising(gray, None, 3, 7, 21)
    
    contours, hierarchy = recognize_text(gray)
    mask = np.zeros(gray.shape, np.uint8)

    for index, contour in enumerate(contours):
        [x, y, w, h] = cv2.boundingRect(contour)
        if h < 16 or w < 16:
            continue

        mskRoi = mask[y:y+h, x:x+w]
        cv2.drawContours(mask, [contour], 0, 255, -1) #CV_FILLED
        nz = cv2.countNonZero(mskRoi)
        ratio = (float)(nz) / (float)(h*w)
        
        # got this value from left heel
        if ratio > 0.55 and ratio < 0.9:
            roi = denoised[y:y+h, x:x+w] 
            text = pytesseract.image_to_string(Image.fromarray(roi), lang="kir+eng", config="-psm 7")
            if text:                
                item = {'x': x, 'y': y, 'w': w, 'h': h, 'text': text}
                result.append(item)
                cv2.rectangle(idcard, (x, y), (x + w, y + h), (255, 0, 255), 2)
    # need to restore settings
    hash_object = hashlib.sha256(idcard)
    hex_dig = hash_object.hexdigest()
    cv2.imwrite("/webapp/web/static/"+hex_dig+".jpeg", idcard)
    return "static/"+hex_dig+".jpeg", result 
Example #7
Source File: idcardocr.py    From idcardocr with GNU General Public License v3.0 4 votes vote down vote up
def get_result_fix_length(red, fix_length, langset, custom_config=''):
    red_org = red
    cv2.fastNlMeansDenoising(red, red, 4, 7, 35)
    rec, red = cv2.threshold(red, 127, 255, cv2.THRESH_BINARY_INV)
    image, contours, hierarchy = cv2.findContours(red, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # print(len(contours))
    # 描边一次可以减少噪点
    cv2.drawContours(red, contours, -1, (0, 255, 0), 1)
    color_img = cv2.cvtColor(red, cv2.COLOR_GRAY2BGR)
    # for x, y, w, h in contours:
    #     imgrect = cv2.rectangle(color_img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    # showimg(imgrect)

    h_threshold = 54
    numset_contours = []
    calcu_cnt = 1
    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        if h > h_threshold:
            numset_contours.append((x, y, w, h))
    while len(numset_contours) != fix_length:
        if calcu_cnt > 50:
            print(u'计算次数过多!目前阈值为:', h_threshold)
            break
        numset_contours = []
        calcu_cnt += 1
        if len(numset_contours) > fix_length:
            h_threshold += 1
            contours_cnt = 0
            for cnt in contours:
                x, y, w, h = cv2.boundingRect(cnt)
                if h > h_threshold:
                    contours_cnt += 1
                    numset_contours.append((x, y, w, h))
        if len(numset_contours) < fix_length:
            h_threshold -= 1
            contours_cnt = 0
            for cnt in contours:
                x, y, w, h = cv2.boundingRect(cnt)
                if h > h_threshold:
                    contours_cnt += 1
                    numset_contours.append((x, y, w, h))
    result_string = ''
    numset_contours.sort(key=lambda num: num[0])
    for x, y, w, h in numset_contours:
        result_string += pytesseract.image_to_string(cv2.UMat.get(red_org)[y-10:y + h + 10, x-10:x + w + 10], lang=langset, config=custom_config)
    # print(new_r)
    # cv2.imwrite('fixlengthred.png', cv2.UMat.get(red_org)[y-10:y + h +10 , x-10:x + w + 10])
    print(result_string)
    return result_string 
Example #8
Source File: idcardocr.py    From idcardocr with GNU General Public License v3.0 4 votes vote down vote up
def get_result_vary_length(red, langset, org_img, custom_config=''):
    red_org = red
    # cv2.fastNlMeansDenoising(red, red, 4, 7, 35)
    rec, red = cv2.threshold(red, 127, 255, cv2.THRESH_BINARY_INV)
    image, contours, hierarchy = cv2.findContours(red, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # print(len(contours))
    # 描边一次可以减少噪点
    cv2.drawContours(red, contours, -1, (255, 255, 255), 1)
    color_img = cv2.cvtColor(red, cv2.COLOR_GRAY2BGR)
    numset_contours = []
    height_list=[]
    width_list=[]
    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        height_list.append(h)
        # print(h,w)
        width_list.append(w)
    height_list.remove(max(height_list))
    width_list.remove(max(width_list))
    height_threshold = 0.70*max(height_list)
    width_threshold = 1.4 * max(width_list)
    # print('height_threshold:'+str(height_threshold)+'width_threshold:'+str(width_threshold))
    big_rect=[]
    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        if h > height_threshold and w < width_threshold:
            # print(h,w)
            numset_contours.append((x, y, w, h))
            big_rect.append((x, y))
            big_rect.append((x + w, y + h))
    big_rect_nparray = np.array(big_rect, ndmin=3)
    x, y, w, h = cv2.boundingRect(big_rect_nparray)
    # imgrect = cv2.rectangle(color_img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    # showimg(imgrect)
    # showimg(cv2.UMat.get(org_img)[y:y + h, x:x + w])

    result_string = ''
    result_string += pytesseract.image_to_string(cv2.UMat.get(red_org)[y-10:y + h + 10, x-10:x + w + 10], lang=langset,
                                                 config=custom_config)
    print(result_string)
    # cv2.imwrite('varylength.png', cv2.UMat.get(org_img)[y:y + h, x:x + w])
    # cv2.imwrite('varylengthred.png', cv2.UMat.get(red_org)[y:y + h, x:x + w])
    # numset_contours.sort(key=lambda num: num[0])
    # for x, y, w, h in numset_contours:
    #     result_string += pytesseract.image_to_string(cv2.UMat.get(color_img)[y:y + h, x:x + w], lang=langset, config=custom_config)
    return punc_filter(result_string)