def feature_extraction_single_rolled(self, img_file):
        block_size = 16

        if not os.path.exists(img_file):
            return None
        img = io.imread(img_file)
        h, w = img.shape
        mask = get_maps.get_quality_map_intensity(img)
        #if np.max(mask) == 0:
        #    print img_file
        #return None
        start = timeit.default_timer()
        mnt = self.minu_model.run_whole_image(img, minu_thr=0.2)
        stop = timeit.default_timer()
        minu_time = stop - start
        # show.show_minutiae(img, mnt, mask=mask, block=True, fname=None)
        # show.show_minutiae(img,mnt)

        # start = timeit.default_timer()
        des = descriptor.minutiae_descriptor_extraction(
            img,
            mnt,
            self.patch_types,
            self.des_models,
            self.patchIndexV,
            batch_size=256,
            patch_size=self.patch_size)
        # stop = timeit.default_timer()
        # des_time = stop - start
        print minu_time  #, des_time
        #texture_img = preprocessing.FastCartoonTexture(img, sigma=2.5, show=False)

        dir_map, _ = get_maps.get_maps_STFT(img,
                                            patch_size=64,
                                            block_size=block_size,
                                            preprocess=True)
        #stop = timeit.default_timer()

        blkH = h // block_size
        blkW = w // block_size
        # dir_map = np.zeros((blkH,blkW))
        # print stop - start
        #blkH, blkW = dir_map.shape

        minu_template = template.MinuTemplate(h=h,
                                              w=w,
                                              blkH=blkH,
                                              blkW=blkW,
                                              minutiae=mnt,
                                              des=des,
                                              oimg=dir_map,
                                              mask=mask)

        rolled_template = template.Template()
        rolled_template.add_minu_template(minu_template)

        return rolled_template
    def feature_extraction_longitudinal(self, img_file):
        block_size = 16

        img = io.imread(img_file)
        #print img.shape
        img = preprocessing.adjust_image_size(img, block_size)
        h, w = img.shape
        texture_img = preprocessing.FastCartoonTexture(img,
                                                       sigma=2.5,
                                                       show=False)

        contrast_img_guassian = preprocessing.local_constrast_enhancement_gaussian(
            img)

        mask = get_maps.get_quality_map_intensity(img)
        #show.show_mask(mask, img, fname=None, block=True)
        quality_map, dir_map, fre_map = get_maps.get_quality_map_dict(
            texture_img,
            self.dict_all,
            self.dict_ori,
            self.dict_spacing,
            block_size=16,
            process=False)

        enh_constrast_img = filtering.gabor_filtering_pixel(
            contrast_img_guassian,
            dir_map + math.pi / 2,
            fre_map,
            mask=np.ones((h, w), np.int),
            block_size=16,
            angle_inc=3)

        mnt = self.minu_model.run(img, minu_thr=0.2)

        #show.show_minutiae(img,mnt)
        des = descriptor.minutiae_descriptor_extraction(img,
                                                        mnt,
                                                        self.patch_types,
                                                        self.des_models,
                                                        self.patchIndexV,
                                                        batch_size=128)

        blkH, blkW = dir_map.shape

        minu_template = template.MinuTemplate(h=h,
                                              w=w,
                                              blkH=blkH,
                                              blkW=blkW,
                                              minutiae=mnt,
                                              des=des,
                                              oimg=dir_map,
                                              mask=mask)

        rolled_template = template.Template()
        rolled_template.add_minu_template(minu_template)

        return rolled_template, texture_img, enh_constrast_img
    def feature_extraction_single_rolled(self, img_file):
        block_size = 16

        img = io.imread(img_file)
        h, w = img.shape
        mask = get_maps.get_quality_map_intensity(img)
        mnt = self.minu_model.run(img, minu_thr=0.2)

        # show.show_minutiae(img,mnt)
        des = descriptor.minutiae_descriptor_extraction(img,
                                                        mnt,
                                                        self.patch_types,
                                                        self.des_models,
                                                        self.patchIndexV,
                                                        batch_size=128)

        #texture_img = preprocessing.FastCartoonTexture(img, sigma=2.5, show=False)

        start = timeit.default_timer()
        dir_map, _ = get_maps.get_maps_STFT(img,
                                            patch_size=64,
                                            block_size=block_size,
                                            preprocess=True)
        stop = timeit.default_timer()
        print stop - start
        blkH, blkW = dir_map.shape

        minu_template = template.MinuTemplate(h=h,
                                              w=w,
                                              blkH=blkH,
                                              blkW=blkW,
                                              minutiae=mnt,
                                              des=des,
                                              oimg=dir_map,
                                              mask=mask)

        rolled_template = template.Template()
        rolled_template.add_minu_template(minu_template)

        return rolled_template
    def feature_extraction_single_rolled_enhancement(self, img_file):
        block_size = 16

        img = io.imread(img_file)
        # print img.shape

        img = preprocessing.adjust_image_size(img, block_size)

        h, w = img.shape
        #texture_img = preprocessing.FastCartoonTexture(img, sigma=2.5, show=False)

        contrast_img_guassian = preprocessing.local_constrast_enhancement_gaussian(
            img)

        mask = get_maps.get_quality_map_intensity(img)

        #show.show_mask(mask, img, fname=None, block=True)
        start = timeit.default_timer()
        quality_map, dir_map, fre_map = get_maps.get_quality_map_dict(
            contrast_img_guassian,
            self.dict_all,
            self.dict_ori,
            self.dict_spacing,
            block_size=16,
            process=False)
        stop = timeit.default_timer()
        OF_time = stop - start
        print 'of estimate time: %f' % (OF_time)
        start = timeit.default_timer()
        enh_constrast_img = filtering.gabor_filtering_pixel2(
            contrast_img_guassian,
            dir_map + math.pi / 2,
            fre_map,
            mask=mask,
            gabor_filters=self.gabor_filters)
        stop = timeit.default_timer()
        filtering_time = stop - start
        print 'filtering time: %f' % (filtering_time)
        mnt = self.minu_model.run_whole_image(img, minu_thr=0.2)

        # show.show_minutiae(img,mnt)
        des = descriptor.minutiae_descriptor_extraction(img,
                                                        mnt,
                                                        self.patch_types,
                                                        self.des_models,
                                                        self.patchIndexV,
                                                        batch_size=128)

        blkH, blkW = dir_map.shape

        minu_template = template.MinuTemplate(h=h,
                                              w=w,
                                              blkH=blkH,
                                              blkW=blkW,
                                              minutiae=mnt,
                                              des=des,
                                              oimg=dir_map,
                                              mask=mask)

        rolled_template = template.Template()
        rolled_template.add_minu_template(minu_template)

        # texture templates
        stride = 32
        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[ofY][ofX]
                # print("ori = " + str(ori))
                virtual_minutiae.append([x_i, y_i,
                                         ori])  #, distFromBg[y_i,x_i]
        virtual_minutiae = np.asarray(virtual_minutiae)
        if len(virtual_minutiae) > 3:
            virtual_des = descriptor.minutiae_descriptor_extraction(
                img,
                virtual_minutiae,
                self.patch_types,
                self.des_models,
                self.patchIndexV,
                batch_size=128)
            #show.show_minutiae(img,virtual_minutiae)
            texture_template = template.TextureTemplate(
                h=h,
                w=w,
                minutiae=virtual_minutiae,
                des=virtual_des,
                mask=mask)
            rolled_template.add_texture_template(texture_template)
        return rolled_template, enh_constrast_img
def feature_extraction_single_latent(raw_img_file,
                                     AEC_img_file,
                                     mask_file,
                                     patch_types=None,
                                     des_models=None):
    ###
    #  input:
    # raw_img, original latent image
    # AEC_img, enhanced latent image by Autoencoder
    # mask:    ROI
    # main idea:
    # 1) Use AEC_img to estimate ridge flow and ridge spacing
    # 2) use AEC_image and raw_img to extract two different minutiae set
    ###
    raw_img = io.imread(raw_img_file)
    AEC_img = io.imread(AEC_img_file)
    mask = io.imread(mask_file)
    #mask = mask_dilation(mask, block_size=16)

    texture_img = preprocessing.FastCartoonTexture(raw_img,
                                                   sigma=2.5,
                                                   show=False)

    dir_map, fre_map, rec_img = get_maps.get_maps_STFT(AEC_img,
                                                       patch_size=64,
                                                       block_size=16,
                                                       preprocess=True)

    descriptor_img = filtering.gabor_filtering_pixel(texture_img,
                                                     dir_map + math.pi / 2,
                                                     fre_map,
                                                     mask=mask,
                                                     block_size=16,
                                                     angle_inc=3)

    bin_img = binarization.binarization(texture_img,
                                        dir_map,
                                        block_size=16,
                                        mask=mask)

    enhanced_img = filtering.gabor_filtering_block(bin_img,
                                                   dir_map + math.pi / 2,
                                                   fre_map,
                                                   patch_size=64,
                                                   block_size=16)
    enhanced_img = filtering.gabor_filtering_block(enhanced_img,
                                                   dir_map + math.pi / 2,
                                                   fre_map,
                                                   patch_size=64,
                                                   block_size=16)

    # plt.subplot(131), plt.imshow(raw_img, cmap='gray')
    # plt.title('Input image'), plt.xticks([]), plt.yticks([])
    # plt.subplot(132), plt.imshow(descriptor_img, cmap='gray')
    # plt.title('Feature image'), plt.xticks([]), plt.yticks([])
    #
    # plt.subplot(133), plt.imshow(enhanced_img, cmap='gray')
    # plt.title('Feature image'), plt.xticks([]), plt.yticks([])
    # plt.show(block=True)
    # plt.close()

    enhanced_AEC_img = filtering.gabor_filtering_block(AEC_img,
                                                       dir_map + math.pi / 2,
                                                       fre_map,
                                                       patch_size=64,
                                                       block_size=16)
    bin_img = binarization.binarization(enhanced_AEC_img,
                                        dir_map,
                                        block_size=16,
                                        mask=mask)
    # plt.imshow(AEC_img,cmap='gray')
    # plt.show()
    # plt.close()

    bin_img2 = 1 - bin_img
    thin_img = skeletonize(bin_img2)
    # thin_img2 = thin_img.astype(np.uint8)
    # thin_img2[thin_img2 > 0] = 255

    mnt, thin_img2 = crossnumber.extract_minutiae(1 - thin_img,
                                                  mask=mask,
                                                  R=10)
    crossnumber.show_minutiae(thin_img, mnt)

    patchSize = 160
    oriNum = 64
    patchIndexV = descriptor.get_patch_index(patchSize,
                                             patchSize,
                                             oriNum,
                                             isMinu=1)
    if len(descriptor_img.shape) == 2:
        h, w = descriptor_img.shape
        ret = np.empty((h, w, 3), dtype=np.float)
        ret[:, :, :] = descriptor_img[:, :, np.newaxis]
        descriptor_img = ret

    if len(enhanced_AEC_img.shape) == 2:
        h, w = enhanced_AEC_img.shape
        ret = np.empty((h, w, 3), dtype=np.float)
        ret[:, :, :] = enhanced_AEC_img[:, :, np.newaxis]
        enhanced_AEC_img = ret

    des = descriptor.minutiae_descriptor_extraction(enhanced_AEC_img,
                                                    mnt,
                                                    patch_types,
                                                    des_models,
                                                    patchIndexV,
                                                    batch_size=128)

    h, w = mask.shape
    blkH, blkW = dir_map.shape
    minu_template = template.MinuTemplate(h=h,
                                          w=w,
                                          blkH=blkH,
                                          blkW=blkW,
                                          minutiae=mnt,
                                          des=des,
                                          oimg=dir_map,
                                          mask=mask)

    latent_template = template.Template()
    latent_template.add_minu_template(minu_template)

    print des
Beispiel #6
0
    def feature_extraction_single_latent_evaluation(self,img_file, mask_file, AEC_img_file,output_path = None ):

        img = io.imread(img_file)
        name = os.path.basename(img_file)
        AEC_img = io.imread(AEC_img_file)
        mask = io.imread(mask_file)
        h,w = mask.shape
        #mask = mask_dilation(mask, block_size=16)
        latent_template = template.Template()
        block = False
        minu_thr = 0.3

        contrast_img = preprocessing.local_constrast_enhancement(img)
        # Two ways for orientation field estimation
        #  Use the AEC_img and STFT on texture image
        dir_map_sets = []
        texture_img = preprocessing.FastCartoonTexture(img, sigma=2.5, show=False)
        dir_map, fre_map = get_maps.get_maps_STFT(texture_img, patch_size=64, block_size=16, preprocess=True)
        dir_map_sets.append(dir_map)

        blkH, blkW = dir_map.shape

        dir_map, fre_map = get_maps.get_maps_STFT(AEC_img, patch_size=64, block_size=16, preprocess=True)
        dir_map_sets.append(dir_map)

        #dir_map, fre_map = get_maps.get_maps_STFT(contrast_img, patch_size=64, block_size=16, preprocess=True)
        #dir_map_sets.append(dir_map)

        # based on the OF, we can use texture image and AEC image for frequency field estimation

        fre_map_sets = []
        quality_map, fre_map = get_maps.get_quality_map_ori_dict(AEC_img, self.dict, self.spacing, dir_map=dir_map_sets[0],
                                                                 block_size=16)
        fre_map_sets.append(fre_map)

        quality_map, fre_map = get_maps.get_quality_map_ori_dict(contrast_img, self.dict, self.spacing,
                                                                 dir_map=dir_map_sets[1],
                                                                 block_size=16)
        fre_map_sets.append(fre_map)

        descriptor_imgs = [texture_img]
        descriptor_imgs.append(contrast_img)
        enh_texture_img = filtering.gabor_filtering_pixel(texture_img, dir_map + math.pi / 2, fre_map_sets[0], mask=mask,
                                                         block_size=16, angle_inc=3)
        descriptor_imgs.append(enh_texture_img)
        enh_contrast_img = filtering.gabor_filtering_pixel(contrast_img, dir_map + math.pi / 2, fre_map_sets[1], mask=mask,
                                                           block_size=16, angle_inc=3)
        descriptor_imgs.append(enh_contrast_img)

        minutiae_sets = []
        mnt = self.minu_model.run(texture_img, minu_thr=0.1)
        mnt = self.remove_spurious_minutiae(mnt, mask)
        minutiae_sets.append(mnt)
        fname = output_path + os.path.splitext(name)[0] + '_texture_img.jpeg'
        show.show_minutiae(texture_img, mnt, block=block, fname=fname)

        mnt = self.minu_model.run(contrast_img, minu_thr=0.1)
        mnt = self.remove_spurious_minutiae(mnt, mask)
        minutiae_sets.append(mnt)
        fname = output_path + os.path.splitext(name)[0] + '_contrast_img.jpeg'
        show.show_minutiae(contrast_img, mnt, block=block, fname=fname)

        mnt = self.minu_model.run(enh_texture_img, minu_thr=minu_thr)
        mnt = self.remove_spurious_minutiae(mnt, mask)
        minutiae_sets.append(mnt)
        fname = output_path + os.path.splitext(name)[0] + '_enh_texture_img.jpeg'
        show.show_minutiae(enh_texture_img, mnt, block=block, fname=fname)

        mnt = self.minu_model.run(enh_contrast_img, minu_thr=minu_thr)
        mnt = self.remove_spurious_minutiae(mnt, mask)
        minutiae_sets.append(mnt)
        fname = output_path + os.path.splitext(name)[0] + '_enh_contrast_img.jpeg'
        show.show_minutiae(enh_contrast_img, mnt, block=block, fname=fname)

        mnt = self.minu_model.run(AEC_img, minu_thr=minu_thr)
        mnt = self.remove_spurious_minutiae(mnt, mask)
        minutiae_sets.append(mnt)

        fname = output_path + os.path.splitext(name)[0] + '_AEC_img.jpeg'
        show.show_minutiae(AEC_img, mnt, block=block, fname=fname)



        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_sets[1], mask=mask)
                latent_template.add_minu_template(minu_template)

        return latent_template
Beispiel #7
0
    def feature_extraction_single_latent(self,img_file, output_path = None, show_processes=False ):
        #block = True
        img = io.imread(img_file)

        name = os.path.basename(img_file)
        mask_CNN,_ = self.ROI_model.run(img)
        h,w = mask_CNN.shape
        #mask = mask_dilation(mask, block_size=16)
        latent_template = template.Template()

        minu_thr = 0.3

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

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




        descriptor_imgs.append(texture_img)

        contrast_img_guassian = preprocessing.local_constrast_enhancement_gaussian(img)

        quality_map, _, _ = get_maps.get_quality_map_dict(texture_img, self.dict_all, self.dict_ori,
                                                                      self.dict_spacing, block_size=16, process=False)
        quality_map_pixel = cv2.resize(quality_map, (0, 0), fx=16, fy=16)
        #plt.imshow(quality_map_pixel,cmap='gray')
        #plt.show()
        mask_coarse = quality_map_pixel > 0.3
        mask_coarse = mask_coarse.astype(np.int)
        mask = mask_coarse * mask_CNN
        # show.show_mask(mask_CNN, img, fname='mask_RCNN.jpeg',block=block)
        # show.show_mask(mask_coarse,img,fname = 'mask_coarse.jpeg',block=block)
        # show.show_mask(mask, img, fname='mask.jpeg',block=block)




        #show.show_mask(mask, AEC_img, fname='mask_AEC.jpeg',block=block)
        # plt.imshow(AEC_img,cmap = 'gray')
        # plt.show(block=block)
        # plt.close()



        #show.show_mask(mask_CNN, img, fname='mask_RCNN.jpeg',block=block)

        # AEC_img[mask == 0] = 128
        # plt.imshow(AEC_img, cmap='gray')
        # plt.show(block=block)
        # plt.close()

        AEC_img = self.enhancement_model.run(texture_img)
        quality_map, dir_map, fre_map = get_maps.get_quality_map_dict(AEC_img, self.dict_all, self.dict_ori,self.dict_spacing, block_size=16, process=False)

        blkH, blkW = dir_map.shape

        if show_processes:
            show.show_orientation_field(img, dir_map,mask = mask,fname='OF.jpeg')




        # mnt = self.minu_model.run(contrast_img_mean, minu_thr=0.1)
        # mnt = self.remove_spurious_minutiae(mnt, mask)
        # minutiae_sets.append(mnt)
        #
        # fname = output_path + os.path.splitext(name)[0] + '_contrast_img_mean.jpeg'
        # show.show_minutiae(contrast_img_mean, mnt, block=block, fname=fname)


        enh_contrast_img = filtering.gabor_filtering_pixel(contrast_img_guassian, dir_map + math.pi / 2, fre_map,
                                                          mask=mask,
                                                          block_size=16, angle_inc=3)

        enh_texture_img = filtering.gabor_filtering_pixel(texture_img, dir_map + math.pi / 2, fre_map,
                                                          mask=mask,
                                                          block_size=16, angle_inc=3)

        if show_processes:
            show.show_image(texture_img, mask=mask, block=True, fname='cropped_texture_image.jpeg')
            show.show_image(AEC_img, mask=mask, block=True, fname='cropped_AEC_image.jpeg')
            show.show_image(enh_contrast_img, mask=mask, block=True, fname='cropped_enh_image.jpeg')

        #np.ones((h, w), np.int)
        descriptor_imgs.append(enh_contrast_img)


        quality_map2, _ , _ = get_maps.get_quality_map_dict(enh_contrast_img, self.dict_all,self.dict_ori,self.dict_spacing, block_size=16,
                                                                      process=False)
        quality_map_pixel2 = cv2.resize(quality_map2, (0, 0), fx=16, fy=16)

        mask2 = quality_map_pixel2 > 0.50

        #mask = mask*mask2

        minutiae_sets = []
        mnt = self.minu_model.run(contrast_img_guassian, minu_thr=0.05)
        mnt = self.remove_spurious_minutiae(mnt, mask)
        minutiae_sets.append(mnt)
        if show_processes:
            fname = 'minutiae_texture_img.jpeg'
            show.show_minutiae(texture_img, mnt, mask=mask,block=block, fname=fname)

        mnt = self.minu_model.run(AEC_img, minu_thr=0.3)
        mnt = self.remove_spurious_minutiae(mnt, mask2)
        minutiae_sets.append(mnt)
        if show_processes:
            fname = 'minutiae_AEC_img.jpeg'
            show.show_minutiae(AEC_img, mnt, mask=mask, block=block, fname=fname)

        mnt = self.minu_model.run(enh_contrast_img, minu_thr=0.3)
        mnt = self.remove_spurious_minutiae(mnt, mask2)
        minutiae_sets.append(mnt)
        if show_processes:
            fname = 'minutiae_enh_contrast_img.jpeg'
            show.show_minutiae(enh_contrast_img, mnt, mask=mask,block=block, fname=fname)

        mnt = self.minu_model.run(enh_texture_img, minu_thr=0.3)
        mnt = self.remove_spurious_minutiae(mnt, mask2)
        minutiae_sets.append(mnt)

        # minutiae template 1
        des = descriptor.minutiae_descriptor_extraction(texture_img, minutiae_sets[0], self.patch_types, self.des_models,
                                                         self.patchIndexV, batch_size=128)

        minu_template = template.MinuTemplate(h=h, w=w, blkH=blkH, blkW=blkW, minutiae=minutiae_sets[0],
                                                   des=des, oimg=dir_map, mask=mask)
        latent_template.add_minu_template(minu_template)

        # minutiae template 2
        des = descriptor.minutiae_descriptor_extraction(texture_img, minutiae_sets[1], self.patch_types,
                                                        self.des_models,
                                                        self.patchIndexV, batch_size=128)

        minu_template = template.MinuTemplate(h=h, w=w, blkH=blkH, blkW=blkW, minutiae=minutiae_sets[1],
                                              des=des, oimg=dir_map, mask=mask)
        latent_template.add_minu_template(minu_template)

        # minutiae template 3
        des = descriptor.minutiae_descriptor_extraction(enh_texture_img, minutiae_sets[2], self.patch_types,
                                                        self.des_models,
                                                        self.patchIndexV, batch_size=128)

        minu_template = template.MinuTemplate(h=h, w=w, blkH=blkH, blkW=blkW, minutiae=minutiae_sets[2],
                                              des=des, oimg=dir_map, mask=mask)
        latent_template.add_minu_template(minu_template)

        # minutiae template 4
        des = descriptor.minutiae_descriptor_extraction(enh_texture_img, minutiae_sets[3], self.patch_types,
                                                        self.des_models,
                                                        self.patchIndexV, batch_size=128)

        minu_template = template.MinuTemplate(h=h, w=w, blkH=blkH, blkW=blkW, minutiae=minutiae_sets[3],
                                              des=des, oimg=dir_map, mask=mask)
        latent_template.add_minu_template(minu_template)



        return latent_template
Beispiel #8
0
    def feature_extraction_single_latent_evaluation_AEM18T(self,img_file, mask_file, AEC_img_file,output_path = None ):

        img = io.imread(img_file)
        name = os.path.basename(img_file)
        AEC_img = io.imread(AEC_img_file)
        mask_CNN = io.imread(mask_file)
        h,w = mask_CNN.shape
        #mask = mask_dilation(mask, block_size=16)
        latent_template = template.Template()
        block = False
        minu_thr = 0.3

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

        descriptor_imgs = []
        texture_img = preprocessing.FastCartoonTexture(img, sigma=2.5, show=False)
        descriptor_imgs.append(texture_img)
        contrast_img_mean = preprocessing.local_constrast_enhancement(img)
        contrast_img_guassian = preprocessing.local_constrast_enhancement_gaussian(img)

        quality_map, dir_map, fre_map = get_maps.get_quality_map_dict(texture_img, self.dict_all, self.dict_ori,
                                                                      self.dict_spacing, block_size=16, process=False)
        quality_map_pixel = cv2.resize(quality_map, (0, 0), fx=16, fy=16)
        mask_coarse = quality_map_pixel > 0.3
        mask_coarse = mask_coarse.astype(np.int)
        quality_map, dir_map, fre_map = get_maps.get_quality_map_dict(AEC_img, self.dict_all, self.dict_ori,self.dict_spacing, block_size=16, process=False)


        minutiae_sets = []
        mnt = self.minu_model.run(texture_img, minu_thr=0.1)
        mnt = self.remove_spurious_minutiae(mnt, mask_coarse)
        minutiae_sets.append(mnt)
        fname = output_path + os.path.splitext(name)[0] + '_texture_img.jpeg'
        show.show_minutiae(texture_img, mnt, block=block, fname=fname)

        mnt = self.minu_model.run(contrast_img_mean, minu_thr=0.1)
        mnt = self.remove_spurious_minutiae(mnt, mask_coarse)
        minutiae_sets.append(mnt)

        fname = output_path + os.path.splitext(name)[0] + '_contrast_img_mean.jpeg'
        show.show_minutiae(contrast_img_mean, mnt, block=block, fname=fname)


        mnt = self.minu_model.run(contrast_img_guassian, minu_thr=0.1)
        mnt = self.remove_spurious_minutiae(mnt, mask_coarse)
        minutiae_sets.append(mnt)
        fname = output_path + os.path.splitext(name)[0] + '_contrast_img_guassian.jpeg'
        show.show_minutiae(contrast_img_guassian, mnt, block=block, fname=fname)

        #show.show_orientation_field(AEC_img,dir_map)

        enh_texture_img = filtering.gabor_filtering_pixel(texture_img, dir_map + math.pi / 2, fre_map,
                                                          mask=np.ones((h, w), np.int),
                                                          block_size=16, angle_inc=3)

        descriptor_imgs.append(enh_texture_img)

        enh_constrast_img = filtering.gabor_filtering_pixel(contrast_img_guassian, dir_map + math.pi / 2, fre_map,
                                                          mask=np.ones((h,w),np.int),
                                                          block_size=16, angle_inc=3)

        descriptor_imgs.append(enh_constrast_img)

        quality_map2, _ , _ = get_maps.get_quality_map_dict(enh_texture_img, self.dict_all,self.dict_ori,self.dict_spacing, block_size=16,
                                                                      process=False)
        quality_map_pixel2 = cv2.resize(quality_map2, (0, 0), fx=16, fy=16)

        mask = quality_map_pixel2 > 0.55


        mask = mask.astype(np.int)
        mask = mask_coarse * mask
        mask = mask * mask_CNN

        mnt = self.minu_model.run(AEC_img, minu_thr=0.3)
        mnt = self.remove_spurious_minutiae(mnt, mask)
        minutiae_sets.append(mnt)
        fname = output_path + os.path.splitext(name)[0] + '_AEC_img.jpeg'
        show.show_minutiae(AEC_img, mnt, block=block, fname=fname)

        mnt = self.minu_model.run(enh_texture_img, minu_thr=0.3)
        mnt = self.remove_spurious_minutiae(mnt, mask)
        minutiae_sets.append(mnt)

        fname = output_path + os.path.splitext(name)[0] + '_enh_texture_img.jpeg'
        show.show_minutiae(enh_texture_img, mnt, block=block, fname=fname)

        mnt = self.minu_model.run(enh_constrast_img, minu_thr=0.3)
        mnt = self.remove_spurious_minutiae(mnt, mask)
        minutiae_sets.append(mnt)
        fname = output_path + os.path.splitext(name)[0] + '_enh_constrast_img.jpeg'
        show.show_minutiae(enh_constrast_img, mnt, block=block, fname=fname)


        blkH, blkW = dir_map.shape
        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, mask=mask)
                latent_template.add_minu_template(minu_template)

        return latent_template
    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
Beispiel #10
0
    def feature_extraction_single_rolled(self,
                                         img_file,
                                         output_dir=None,
                                         ppi=500):
        block_size = 16

        if not os.path.exists(img_file):
            return None
        img = io.imread(img_file, s_grey=True)
        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)
        if len(img.shape) > 2:
            img = rgb2gray(img)
        h, w = img.shape
        start = timeit.default_timer()
        mask = get_maps.get_quality_map_intensity(img)
        stop = timeit.default_timer()
        print('time for cropping : %f' % (stop - start))
        start = timeit.default_timer()
        contrast_img = preprocessing.local_constrast_enhancement(img)
        texture_img = preprocessing.FastCartoonTexture(img,
                                                       sigma=2.5,
                                                       show=False)
        mnt = self.minu_model.run_whole_image(texture_img, minu_thr=0.15)
        stop = timeit.default_timer()
        print('time for minutiae : %f' % (stop - start))

        start = timeit.default_timer()
        des = descriptor.minutiae_descriptor_extraction(
            img,
            mnt,
            self.patch_types,
            self.des_models,
            self.patchIndexV,
            batch_size=256,
            patch_size=self.patch_size)
        stop = timeit.default_timer()
        print('time for descriptor : %f' % (stop - start))

        dir_map, _ = get_maps.get_maps_STFT(img,
                                            patch_size=64,
                                            block_size=block_size,
                                            preprocess=True)

        blkH = h // block_size
        blkW = w // block_size

        minu_template = template.MinuTemplate(h=h,
                                              w=w,
                                              blkH=blkH,
                                              blkW=blkW,
                                              minutiae=mnt,
                                              des=des,
                                              oimg=dir_map,
                                              mask=mask)

        rolled_template = template.Template()
        rolled_template.add_minu_template(minu_template)

        start = timeit.default_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] <= 24):
                    continue
                ofY = int(y_i / 16)
                ofX = int(x_i / 16)

                ori = -dir_map[ofY][ofX]
                virtual_minutiae.append([x_i, y_i, ori])
        virtual_minutiae = np.asarray(virtual_minutiae)

        if len(virtual_minutiae) > 1000:
            virtual_minutiae = virtual_minutiae[:1000]
        print len(virtual_minutiae)
        if len(virtual_minutiae) > 3:
            virtual_des = descriptor.minutiae_descriptor_extraction(
                contrast_img,
                virtual_minutiae,
                self.patch_types,
                self.des_models,
                self.patchIndexV,
                batch_size=128)
            texture_template = template.TextureTemplate(
                h=h,
                w=w,
                minutiae=virtual_minutiae,
                des=virtual_des,
                mask=mask)
            rolled_template.add_texture_template(texture_template)
        stop = timeit.default_timer()
        print('time for texture : %f' % (stop - start))
        return rolled_template