Exemplo n.º 1
0
def compare_AEM_with_MMM(model_path, img_path='/Data/Latent/NISTSD27/image/',
                         output_path='/AutomatedLatentRecognition/Results/minutiae/NISTSD27_latents_Contrast/',
                         minu_path='/Data/Latent/NISTSD27/ManMinu', processing=None, thr=0.01):
    minu_model = ImportGraph(model_path)

    img_files = glob.glob(img_path + '*.bmp')
    img_files.sort()

    manu_files = glob.glob(minu_path + '*.txt')
    manu_files.sort()
    for i, img_file in enumerate(img_files):
        img = misc.imread(img_file, mode='L')
        if processing == 'contrast':
            img = LP.local_constrast_enhancement(img)
        elif processing == 'STFT':
            img = LP.STFT(img)
        elif processing == 'texture':
            img = LP.FastCartoonTexture(img)
        elif processing == 'texture_STFT':
            img = LP.FastCartoonTexture(img)
            img = LP.STFT(img)
        mnt = minu_model.run_whole_image(img, minu_thr=thr)
        img_name = os.path.basename(img_file)
        fname = output_path + os.path.splitext(img_name)[0] + '_minu.jpeg'

        minutiae_set = []
        minutiae_set.append(mnt)

        input_minu = np.loadtxt(manu_files[i])
        input_minu[:, 2] = input_minu[:, 2] / 180.0 * np.pi
        minutiae_set.append(input_minu)
        print i
        show.show_minutiae_sets(img, minutiae_set, mask=None, block=False, fname=fname)
        print fname
Exemplo n.º 2
0
def modify_minutiae_cylinder(input_file,
                             output_file,
                             angle=None,
                             processing=None):

    cylinder = np.load(input_file)
    img = cylinder[:, :, 0]
    if processing == 'STFT':
        img = LP.STFT(img)
    cylinder[:, :, 0] = img
    np.save(output_file, cylinder)
def generate_training_npy(datadir, output_path):
    finger_paths = glob.glob(datadir + '/MI*')
    imgs = []
    for finger_path in finger_paths:
        img_files = glob.glob(finger_path + '/high*.bmp')
        for img_file in img_files:
            img = cv2.imread(img_file, cv2.IMREAD_GRAYSCALE)
            img_STFT = preprocessing.STFT(img)

            matrix = np.concatenate((np.expand_dims(
                img_STFT, axis=2), np.expand_dims(img, axis=2)), 2)
            output_file = output_path + img_file.split('/')[-1][:-3] + 'npy'
            matrix = np.uint8(matrix)
            np.save(output_file, matrix)
Exemplo n.º 4
0
def extract_minutiae_cylinder(img_input,
                              minutiae_input,
                              ROI=None,
                              num_ori=12,
                              angle=None,
                              processing=None):
    # for the latent or the low quality rolled print
    minutiae = minutiae_input.copy()
    img = img_input.copy()
    if processing == 'STFT':
        img = LP.STFT(img)
    elif processing == 'contrast':
        img = LP.local_constrast_enhancement(img)
    elif processing == 'texture':
        img = LP.FastCartoonTexture(img)
    sigma = 5**2
    if ROI is not None:
        h, w = ROI.shape
        for i in range(h):
            for j in range(w):
                if ROI[i, j] == 0:
                    img[i, j] = 255

        h, w = ROI.shape
        col_sum = np.sum(ROI, axis=0)

        ind = [x for x in range(len(col_sum)) if col_sum[x] > 0]
        min_x = np.max([np.min(ind) - 32, 0])
        max_x = np.min([np.max(ind) + 32, w])

        row_sum = np.sum(ROI, axis=1)

        ind = [x for x in range(len(row_sum)) if row_sum[x] > 0]
        min_y = np.max([np.min(ind) - 32, 0])
        max_y = np.min([np.max(ind) + 32, h])

        ROI = ROI[min_y:max_y, min_x:max_x]
        img = img[min_y:max_y, min_x:max_x]
        minutiae[:, 0] = minutiae[:, 0] - min_x
        minutiae[:, 1] = minutiae[:, 1] - min_y
    else:
        h, w = img.shape[0:2]
        ROI = np.ones((h, w))

    # rotate the image and ROI, and also update minutiae points
    h0, w0 = ROI.shape
    if angle is not None:
        h02 = (h0 + 1) / 2
        w02 = (w0 + 1) / 2

        img = rotate(img, angle)
        ROI = rotate(ROI, angle)

        h, w = ROI.shape
        h2 = (h + 1) / 2
        w2 = (w + 1) / 2

        angle = -angle / 180.0 * math.pi
        cosTheta = math.cos(angle)
        sinTheta = math.sin(angle)
        xx = (minutiae[:, 0] - w02) * cosTheta - (minutiae[:, 1] -
                                                  h02) * sinTheta + w2

        yy = (minutiae[:, 0] - w02) * sinTheta + (minutiae[:, 1] -
                                                  h02) * cosTheta + h2
        ori = minutiae[:, 2] - angle
        #
        minutiae[:, 0] = xx
        minutiae[:, 1] = yy
        minutiae[:, 2] = ori
        show = 0
        if show:
            minu_num = minutiae.shape[0]
            fig, ax = plt.subplots(1)
            ax.set_aspect('equal')

            R = 10
            arrow_len = 15
            ax.imshow(img, cmap='gray')
            for i in range(0, minu_num):
                xx = minutiae[i, 0]
                yy = minutiae[i, 1]
                circ = Circle((xx, yy), R, color='r', fill=False)
                ax.add_patch(circ)

                ori = -minutiae[i, 2]
                dx = math.cos(ori) * arrow_len
                dy = math.sin(ori) * arrow_len
                ax.arrow(xx,
                         yy,
                         dx,
                         dy,
                         head_width=0.05,
                         head_length=0.1,
                         fc='r',
                         ec='r')
            plt.show()
    h, w = ROI.shape
    minutiae_cylinder = np.zeros((h, w, num_ori), dtype=float)
    cylinder_ori = np.asarray(range(num_ori)) * math.pi * 2 / num_ori

    Y, X = np.mgrid[0:h, 0:w]
    minu_num = minutiae.shape[0]
    for i in range(0, minu_num):
        xx = minutiae[i, 0]
        yy = minutiae[i, 1]
        if yy < 0 or xx < 0:
            continue
            print xx, yy
            minu_num = minutiae.shape[0]
            fig, ax = plt.subplots(1)
            ax.set_aspect('equal')

            R = 10
            arrow_len = 15
            ax.imshow(img, cmap='gray')
            for i in range(0, minu_num):
                xx = minutiae[i, 0]
                yy = minutiae[i, 1]
                circ = Circle((xx, yy), R, color='r', fill=False)
                ax.add_patch(circ)

                ori = -minutiae[i, 2]
                dx = math.cos(ori) * arrow_len
                dy = math.sin(ori) * arrow_len
                ax.arrow(xx,
                         yy,
                         dx,
                         dy,
                         head_width=0.05,
                         head_length=0.1,
                         fc='r',
                         ec='r')
            plt.show()
        weight = np.exp(-((X - xx) * (X - xx) + (Y - yy) * (Y - yy)) / sigma)

        ori = minutiae[i, 2]
        if ori < 0:
            ori += np.pi * 2
        if ori > np.pi * 2:
            ori -= np.pi * 2

        for j in range(num_ori):

            ori_diff = np.fabs(ori - cylinder_ori[j])

            if ori_diff > np.pi * 2:
                ori_diff = ori_diff - np.pi * 2

            ori_diff = np.min([ori_diff, np.pi * 2 - ori_diff])
            minutiae_cylinder[:, :,
                              j] += weight * np.exp(-ori_diff / np.pi * 6)
    show = 0
    if show:
        fig, ax = plt.subplots(1)
        ax.set_aspect('equal')

        R = 10
        arrow_len = 15
        ax.imshow(img, cmap='gray')
        for i in range(0, minu_num):
            xx = minutiae[i, 0]
            yy = minutiae[i, 1]
            circ = Circle((xx, yy), R, color='r', fill=False)
            ax.add_patch(circ)

            ori = -minutiae[i, 2]
            dx = math.cos(ori) * arrow_len
            dy = math.sin(ori) * arrow_len
            ax.arrow(xx,
                     yy,
                     dx,
                     dy,
                     head_width=0.05,
                     head_length=0.1,
                     fc='r',
                     ec='r')
        plt.show()

    return img, ROI, minutiae_cylinder
    def feature_extraction_single_latent(self,
                                         img_file,
                                         output_dir=None,
                                         ppi=500,
                                         show_processes=False,
                                         show_minutiae=False,
                                         minu_file=None):
        block = False
        block_size = 16
        img0 = io.imread(img_file, mode='L')  # / 255.0

        img = img0.copy()

        if ppi != 500:
            img = cv2.resize(img, (0, 0), fx=500.0 / ppi, fy=500.0 / ppi)
        img = preprocessing.adjust_image_size(img, block_size)
        name = os.path.basename(img_file)
        start = timer()
        h, w = img.shape

        if h > 1000 and w > 1000:
            return None, None

        # cropping using two dictionary based approach
        if minu_file is not None:
            manu_minu = np.loadtxt(minu_file)
            # #     # remove low quality minutiae points
            input_minu = np.array(manu_minu)
            input_minu[:, 2] = input_minu[:, 2] / 180.0 * np.pi
        else:
            input_minu = []

        descriptor_imgs = []
        texture_img = preprocessing.FastCartoonTexture(img,
                                                       sigma=2.5,
                                                       show=False)
        STFT_texture_img = preprocessing.STFT(texture_img)

        contrast_img_guassian = preprocessing.local_constrast_enhancement_gaussian(
            img)
        STFT_img = preprocessing.STFT(img)
        constrast_STFT_img = preprocessing.STFT(contrast_img_guassian)

        # step 1: enhance the latent based on our autoencoder
        AEC_img = self.enhancement_model.run_whole_image(STFT_texture_img)
        quality_map_AEC, dir_map_AEC, fre_map_AEC = get_maps.get_quality_map_dict(
            AEC_img, self.dict_all, self.dict_ori, self.dict_spacing, R=500.0)
        blkmask_AEC = quality_map_AEC > 0.45
        blkmask_AEC = binary_closing(blkmask_AEC, np.ones(
            (3, 3))).astype(np.int)
        blkmask_AEC = binary_opening(blkmask_AEC, np.ones(
            (3, 3))).astype(np.int)
        blkmask_SSIM = get_maps.SSIM(STFT_texture_img, AEC_img, thr=0.2)
        blkmask = blkmask_SSIM * blkmask_AEC
        blkH, blkW = blkmask.shape
        mask = cv2.resize(blkmask.astype(float),
                          (block_size * blkW, block_size * blkH),
                          interpolation=cv2.INTER_LINEAR)
        mask[mask > 0] = 1

        minutiae_sets = []

        mnt_STFT = self.minu_model[0].run_whole_image(STFT_img, minu_thr=0.05)
        minutiae_sets.append(mnt_STFT)
        if show_minutiae:
            fname = output_dir + os.path.splitext(name)[0] + '_STFT_img.jpeg'
            show.show_minutiae_sets(STFT_img, [input_minu, mnt_STFT],
                                    mask=None,
                                    block=block,
                                    fname=fname)

        mnt_STFT = self.minu_model[0].run_whole_image(constrast_STFT_img,
                                                      minu_thr=0.1)
        minutiae_sets.append(mnt_STFT)

        mnt_AEC = self.minu_model[1].run_whole_image(AEC_img, minu_thr=0.25)
        mnt_AEC = self.remove_spurious_minutiae(mnt_AEC, mask)
        minutiae_sets.append(mnt_AEC)
        if show_minutiae:
            fname = output_dir + os.path.splitext(name)[0] + '_AEC_img.jpeg'
            show.show_minutiae_sets(AEC_img, [input_minu, mnt_AEC],
                                    mask=mask,
                                    block=block,
                                    fname=fname)

        enh_contrast_img = filtering.gabor_filtering_pixel2(
            contrast_img_guassian,
            dir_map_AEC + math.pi / 2,
            fre_map_AEC,
            mask=np.ones((h, w)),
            block_size=16,
            angle_inc=3)
        mnt_contrast = self.minu_model[1].run_whole_image(enh_contrast_img,
                                                          minu_thr=0.25)
        mnt_contrast = self.remove_spurious_minutiae(mnt_contrast, mask)
        minutiae_sets.append(mnt_contrast)

        enh_texture_img = filtering.gabor_filtering_pixel2(
            texture_img,
            dir_map_AEC + math.pi / 2,
            fre_map_AEC,
            mask=np.ones((h, w)),
            block_size=16,
            angle_inc=3)

        mnt_texture = self.minu_model[1].run_whole_image(enh_texture_img,
                                                         minu_thr=0.25)
        mnt_texture = self.remove_spurious_minutiae(mnt_texture, mask)
        minutiae_sets.append(mnt_texture)

        h, w = img.shape
        latent_template = template.Template()

        # template set 1: no ROI and enhancement are required
        # texture image is used for coase segmentation
        descriptor_imgs = []

        descriptor_imgs.append(STFT_img)
        descriptor_imgs.append(texture_img)
        descriptor_imgs.append(enh_texture_img)
        descriptor_imgs.append(enh_contrast_img)

        mnt2 = self.get_common_minutiae(minutiae_sets, thr=2)

        mnt3 = self.get_common_minutiae(minutiae_sets, thr=3)

        minutiae_sets.append(mnt3)
        minutiae_sets.append(mnt2)
        if show_minutiae:
            fname = output_dir + os.path.splitext(name)[0] + '_common_2.jpeg'
            show.show_minutiae_sets(img, [input_minu, mnt2],
                                    mask=mask,
                                    block=block,
                                    fname=fname)
        end = timer()
        print('Time for minutiae extraction: %f' % (end - start))

        start = timer()
        for mnt in minutiae_sets:
            for des_img in descriptor_imgs:
                des = descriptor.minutiae_descriptor_extraction(
                    des_img,
                    mnt,
                    self.patch_types,
                    self.des_models,
                    self.patchIndexV,
                    batch_size=128)
                minu_template = template.MinuTemplate(h=h,
                                                      w=w,
                                                      blkH=blkH,
                                                      blkW=blkW,
                                                      minutiae=mnt,
                                                      des=des,
                                                      oimg=dir_map_AEC,
                                                      mask=mask)
                latent_template.add_minu_template(minu_template)
        end = timer()
        print('Time for minutiae descriptor generation: %f' % (end - start))

        start = timer()
        # texture templates
        stride = 16
        x = np.arange(24, w - 24, stride)
        y = np.arange(24, h - 24, stride)

        virtual_minutiae = []
        distFromBg = scipy.ndimage.morphology.distance_transform_edt(mask)
        for y_i in y:
            for x_i in x:
                if (distFromBg[y_i][x_i] <= 16):
                    continue
                ofY = int(y_i / 16)
                ofX = int(x_i / 16)

                ori = -dir_map_AEC[ofY][ofX]
                virtual_minutiae.append([x_i, y_i, ori])
                virtual_minutiae.append([x_i, y_i, math.pi + ori])
        virtual_minutiae = np.asarray(virtual_minutiae)

        texture_template = []
        if len(virtual_minutiae) > 3:
            virtual_des = descriptor.minutiae_descriptor_extraction(
                enh_contrast_img,
                virtual_minutiae,
                self.patch_types,
                self.des_models,
                self.patchIndexV,
                batch_size=128,
                patch_size=96)

            texture_template = template.TextureTemplate(
                h=h,
                w=w,
                minutiae=virtual_minutiae,
                des=virtual_des,
                mask=None)
            latent_template.add_texture_template(texture_template)

        end = timer()

        print('Time for texture template generation: %f' % (end - start))
        return latent_template, texture_template