Example #1
0
def map_func1(coords):
    tform2 = transform.SimilarityTransform(scale=1. / 257.,
                                           rotation=0,
                                           translation=(-0.99, -0.99))
    return tform.inverse(np.arctanh(tform2(coords)))
margins = dict(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0, right=1)
"""
Basics
======

Several different geometric transformation types are supported: similarity,
affine, projective and polynomial.

Geometric transformations can either be created using the explicit parameters
(e.g. scale, shear, rotation and translation) or the transformation matrix:

First we create a transformation using explicit parameters:
"""

tform = tf.SimilarityTransform(scale=1,
                               rotation=math.pi / 2,
                               translation=(0, 1))
print tform._matrix
"""
Alternatively you can define a transformation by the transformation matrix
itself:
"""

matrix = tform._matrix.copy()
matrix[1, 2] = 2
tform2 = tf.SimilarityTransform(matrix)
"""
These transformation objects can then be used to apply forward and inverse
coordinate transformations between the source and destination coordinate
systems:
"""
Example #3
0
def imtranslate(im,tx,ty): # tx: columns, ty: rows
    tform = trfm.SimilarityTransform(translation = (-tx,-ty))
    return trfm.warp(im,tform,mode='constant')
Example #4
0
def trans(img: object = None, xaxis: int = 0, yaxis: int = 0):
    return transform.warp(
        img, transform.SimilarityTransform(translation=(-1 * xaxis, yaxis)))
def preprocess(img, bbox=None, landmark=None, **kwargs):
    #print(bbox)
    #print(landmark)
    if isinstance(img, str):
        img = read_image(img, **kwargs)
    M = None
    image_size = []
    str_image_size = kwargs.get('image_size', '')
    if len(str_image_size) > 0:
        image_size = [int(x) for x in str_image_size.split(',')]
        if len(image_size) == 1:
            image_size = [image_size[0], image_size[0]]
        assert len(image_size) == 2
        assert image_size[0] == 112
        assert image_size[0] == 112 or image_size[1] == 96
    if landmark is not None:
        assert len(image_size) == 2
        src = np.array(
            [[30.2946, 51.6963], [65.5318, 51.5014], [48.0252, 71.7366],
             [33.5493, 92.3655], [62.7299, 92.2041]],
            dtype=np.float32)
        if image_size[1] == 112:
            src[:, 0] += 8.0
        dst = landmark.astype(np.float32)

        tform = trans.SimilarityTransform()
        tform.estimate(dst, src)
        M = tform.params[0:2, :]
        #M = cv2.estimateRigidTransform( dst.reshape(1,5,2), src.reshape(1,5,2), False)

    if M is None:
        if bbox is None:  #use center crop
            det = np.zeros(4, dtype=np.int32)
            det[0] = int(img.shape[1] * 0.0625)
            det[1] = int(img.shape[0] * 0.0625)
            det[2] = img.shape[1] - det[0]
            det[3] = img.shape[0] - det[1]
        else:
            det = bbox
        margin = kwargs.get('margin', 44)
        bb = np.zeros(4, dtype=np.int32)
        bb[0] = np.maximum(det[0] - margin / 2, 0)
        bb[1] = np.maximum(det[1] - margin / 2, 0)
        bb[2] = np.minimum(det[2] + margin / 2, img.shape[1])
        bb[3] = np.minimum(det[3] + margin / 2, img.shape[0])
        ret = img[bb[1]:bb[3], bb[0]:bb[2], :]
        if len(image_size) > 0:
            ret = cv2.resize(ret, (image_size[1], image_size[0]))
        return ret
    else:  #do align using landmark
        assert len(image_size) == 2

        #src = src[0:3,:]
        #dst = dst[0:3,:]

        #print(src.shape, dst.shape)
        #print(src)
        #print(dst)
        #print(M)
        warped = cv2.warpAffine(img,
                                M, (image_size[1], image_size[0]),
                                borderValue=0.0)

        #tform3 = trans.ProjectiveTransform()
        #tform3.estimate(src, dst)
        #warped = trans.warp(img, tform3, output_shape=_shape)
        return warped
def cp2tform_v2(base, proj):
    tform = trans.SimilarityTransform()
    tform.estimate(base, proj)
    return tform.params.T, inv(tform.params.T)
Example #7
0
File: flca.py Project: xuejcak/flca
def onechannel(indir, outdir, firstfits='', x0=0, x1=-1, y0=0, y1=-1, every=1, skip=1, xoffset=0, 
               yoffset=0, sigma=20, threshold='0.', kr=0., biascorrect=0, interpolate=0, verbose=0):
    print('Single-channel coalignment.')
    absflag=0; filterflag=1; marg = 0.2 # Margins of the calculated shifts that are excluded.
    if indir[-1] != '/': indir += '/'
    if outdir[-1] != '/': outdir += '/'
    filin = glob(indir+"*.f*ts")
    if len(filin) == 0:
        print(f"The input folder{indir} is empty!")
        return -1
    filin.sort()
    ind0 = 0
    if firstfits == '': 
        firstfits = filin[0]
        ind0 = 0
    else:
        if filin.count(firstfits):
            ind0 = filin.index(firstfits)
        else:
            fname0 = os.path.basename(firstfits)
            for i in range(len(filin)):
                if os.path.basename(filin[i]) == fname0:
                    ind0 = i
                    break
            if ind0 != i:
                print("The first fits file should have the same name as one of the files in input folder!")
                return -1
            
    xoffset = xoffset % skip; yoffset = yoffset % skip
    if threshold[-1] == 'a':
        absflag = 1;
        threshold = float(threshold[0:-1])
    else:
        threshold = float(threshold)
    if kr <= 0. or kr > 20.:
        print("Nonsense value of kr, = {}".format(kr))
        return -1
    sigma = ct.c_double(sigma);
    thresh = ct.c_double(threshold);
    kr = ct.c_double(kr);
    
    # Notice that for 2-D array in python and C languages, the 1st dimension is indicated by y and the 2nd by x.
    # It is consistent with images.
    # But in flcasubs.c, the 1st dimension is marked with x and 2nd with y, following FLCT.
    ny = y1-y0
    nx = x1-x0
    nys = int(np.ceil((ny-yoffset)/skip)) # dimensions for the results
    nxs = int(np.ceil((nx-xoffset)/skip))
    indx0 = int(nxs*marg); indx1 = int(np.ceil(nxs-indx0)); indy0 = int(nys*marg); indy1 = int(np.ceil(nys-indy0))
    ArrayType = ct.c_double*(nx*ny)
    f1ct = ArrayType();
    f2ct = ArrayType();

    print("{} files will be coaligned".format(len(filin)))
    sxarr = np.zeros(len(filin)); syarr = np.zeros(len(filin))
    sxarr[ind0] = 0; syarr[ind0] = 0;
    
    if ind0 > 0:
        kk = 0 # whether change the reference fits file, correspondes to every
        data0, hdr = readfits(firstfits)
        referf = firstfits
        shiftx0 = 0; shifty0 = 0
        for ii in range(ind0, 0, -1):
            print(f"{ii-1}", end="\r")    
            data, hdr = readfits(filin[ii-1])
            tform = transform.SimilarityTransform(translation=(shiftx0,shifty0))
            data1 = transform.warp(data, tform, order=interpolate)
            f1 = data0[y0:y1, x0:x1]
            f2 = data1[y0:y1, x0:x1]
            # tate care, all the normal data are assumed to be larger than 0
            f1[np.logical_or(np.isnan(f1), np.isnan(f2))] = threshold-10
            f2[np.logical_or(np.isnan(f1), np.isnan(f2))] = threshold-10
            #f1 = detrend(f1)
            #f2 = detrend(f2)
            for i in range(ny):
                for j in range(nx):
                    f1ct[i*nx+j] = f1[i,j]
                    f2ct[i*nx+j] = f2[i,j]
            vxct = ArrayType()
            vyct = ArrayType()
            vmct = ArrayType()
            # The 1st dimension is y and the 2nd is x.
            tmp = flcas.flca(f1ct, f2ct, ny, nx, sigma, vyct, vxct, vmct, thresh, absflag, 
                             filterflag, kr, skip, yoffset, xoffset, biascorrect, verbose)
            
            # reshape and select
            vx = np.array(vxct).reshape([ny, nx])
            vy = np.array(vyct).reshape([ny, nx])
            vm = np.array(vmct).reshape([ny, nx]);
            vx = vx[yoffset::skip, xoffset::skip]
            vy = vy[yoffset::skip, xoffset::skip]
            vm = vm[yoffset::skip, xoffset::skip];
            vx = vx[indy0:indy1, indx0:indx1]
            vy = vy[indy0:indy1, indx0:indx1]
            vm = vm[indy0:indy1, indx0:indx1];
            vm[np.logical_or(np.isnan(vx), np.isnan(vy))] = 0
            shiftx = np.median(vx[vm == 1.])
            shifty = np.median(vy[vm == 1.])
            if shiftx == np.nan or shifty == np.nan:
                print("Shift X or shift Y is Nan, return.")
                return -1
            shiftx0 = shiftx+shiftx0; shifty0 = shifty+shifty0
            if interpolate == 0:
                shiftx0 = round(shiftx0); shifty0 = round(shifty0)
            tform = transform.SimilarityTransform(translation=(shiftx0,shifty0))
            data = transform.warp(data, tform, order=interpolate)
            # order = 0: Nearest-neighbor; order = 1: Bi-linear (default). Other choices are not included.
            fname0 = os.path.basename(filin[ii-1])
            fits.writeto(outdir+fname0, data, hdr, output_verify='fix', overwrite=True, checksum=False)
            sxarr[ii-1] = shiftx0; syarr[ii-1] = shifty0
            kk = kk+1
            if kk % every == 0:
                data0 = data.copy()
                referf = filin[ii-1]
    
    data0, hdr = readfits(firstfits)
    fname0 = os.path.basename(firstfits)
    referf = firstfits
    # copy the first fits file to the output folder.
    fits.writeto(outdir+fname0, data0, hdr, output_verify='fix', overwrite=True, checksum=False)
    
    if ind0 < len(filin)-1:
        kk = 0 # whether change the reference fits file, correspondes to every
        shiftx0 = 0; shifty0 = 0
        for ii in range(ind0+1, len(filin)):
            #print("{} {} {}".format(ii, os.path.basename(filin[ii]), os.path.basename(referf)), end="\n")  
            print(f"{ii}", end="\r")  
            data, hdr = readfits(filin[ii])
            tform = transform.SimilarityTransform(translation=(shiftx0,shifty0))
            data1 = transform.warp(data, tform, order=interpolate)
            f1 = data0[y0:y1, x0:x1]
            f2 = data1[y0:y1, x0:x1]
            # tate care, all the normal data are assumed to be larger than 0
            f1[np.logical_or(np.isnan(f1), np.isnan(f2))] = threshold-10
            f2[np.logical_or(np.isnan(f1), np.isnan(f2))] = threshold-10
            #f1 = detrend(f1)
            #f2 = detrend(f2)
            for i in range(ny):
                for j in range(nx):
                    f1ct[i*nx+j] = f1[i,j]
                    f2ct[i*nx+j] = f2[i,j]
            vxct = ArrayType()
            vyct = ArrayType()
            vmct = ArrayType()
            # The 1st dimension is y and the 2nd is x.
            tmp = flcas.flca(f1ct, f2ct, ny, nx, sigma, vyct, vxct, vmct, thresh, absflag, 
                             filterflag, kr, skip, yoffset, xoffset, biascorrect, verbose)
            
            # reshape and select
            vx = np.array(vxct).reshape([ny, nx])
            vy = np.array(vyct).reshape([ny, nx])
            vm = np.array(vmct).reshape([ny, nx]);
            vx = vx[yoffset::skip, xoffset::skip]
            vy = vy[yoffset::skip, xoffset::skip]
            vm = vm[yoffset::skip, xoffset::skip];
            vx = vx[indy0:indy1, indx0:indx1]
            vy = vy[indy0:indy1, indx0:indx1]
            vm = vm[indy0:indy1, indx0:indx1];
            vm[np.logical_or(np.isnan(vx), np.isnan(vy))] = 0
            shiftx = np.median(vx[vm == 1.])
            shifty = np.median(vy[vm == 1.])
            if shiftx == np.nan or shifty == np.nan:
                print("Shift X or shift Y is Nan, return.")
                return -1
            shiftx0 = shiftx+shiftx0; shifty0 = shifty+shifty0
            if interpolate == 0:
                shiftx0 = round(shiftx0); shifty0 = round(shifty0)
            tform = transform.SimilarityTransform(translation=(shiftx0,shifty0))
            data = transform.warp(data, tform, order=interpolate)
            # order = 0: Nearest-neighbor; order = 1: Bi-linear (default).
            # Other choices are not included.
            fname0 = os.path.basename(filin[ii])
            fits.writeto(outdir+fname0, data, hdr, output_verify='fix', overwrite=True, checksum=False)
            sxarr[ii] = shiftx0; syarr[ii] = shifty0
            kk = kk+1
            if kk % every == 0:
                data0 = data.copy()
                #referf = filin[ii]
    np.savez_compressed('shiftxy.npz', sx=sxarr, sy=syarr)
    return 0
Example #8
0
def main(args):
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    data_dir = os.path.expanduser(args.input_dir)
    multiFacesRF = open(os.path.join(output_dir, 'multi_record.txt'), 'w')
    forceFacesRF = open(os.path.join(output_dir, 'force_record.txt'), 'w')

    listFile = open(args.list_file, 'r')
    imgNames = [i.strip() for i in listFile.readlines()]

    print('Creating networks and loading parameters')
    detector = RetinaFace('R50', 0, 0, 'net3')

    # configuration for alignment
    threshold = 0.8  # retinaface threshold
    #image_size = [112,96]
    image_size = [112, 112]
    src = np.array([[30.2946, 51.6963], [65.5318, 51.5014], [48.0252, 71.7366],
                    [33.5493, 92.3655], [62.7299, 92.2041]],
                   dtype=np.float32)

    if image_size[1] == 112:
        src[:, 0] += 8.0

    rotateAngles = [-15, 15, -30, 30, -45, 45]
    for imgName in tqdm(imgNames):
        imgName = imgName + '.jpg'
        srcImgPath = os.path.join(data_dir, imgName)
        outImgPath = os.path.join(output_dir, imgName)

        try:
            img = cv2.imread(srcImgPath)
            if img is None:
                print("Path is error! ", srcImgPath)
                continue
        except:
            print("Something is error! ", srcImgPath)
        else:
            rotateImgs = [imutils.rotate(img, x) for x in rotateAngles]
            newFlag = True
            for img in rotateImgs:
                # fixed to 640*640 by padding
                maxSize = max(img.shape[0], img.shape[1])
                padTop = 0
                padBottom = 0
                padLeft = 0
                padRight = 0
                if img.shape[0] < maxSize:
                    rowDiff = maxSize - img.shape[0]
                    padTop = rowDiff // 2
                    padBottom = rowDiff - padTop
                if img.shape[1] < maxSize:
                    colDiff = maxSize - img.shape[1]
                    padLeft = colDiff // 2
                    padRight = colDiff - padLeft

                img = cv2.copyMakeBorder(img,
                                         padTop,
                                         padBottom,
                                         padLeft,
                                         padRight,
                                         cv2.BORDER_CONSTANT,
                                         value=[0, 0, 0])

                fixedSizes = [80, 160, 320, 480, 640, 800, 960]
                aligned_imgs = []
                for fixedSize in fixedSizes:
                    scale = float(fixedSize) / float(maxSize)
                    bounding_boxes, points = detector.detect(img,
                                                             threshold,
                                                             scales=[scale])
                    nrof_faces = bounding_boxes.shape[0]
                    det = bounding_boxes[:, 0:4]
                    scores = bounding_boxes[:, 4]

                    img_size = np.asarray(img.shape)[0:2]
                    #print(c)
                    if (nrof_faces > 0):
                        if nrof_faces > 1:
                            bounding_box_size = (det[:, 2] - det[:, 0]) * (
                                det[:, 3] - det[:, 1])
                            img_center = img_size / 2
                            offsets = np.vstack([
                                (det[:, 0] + det[:, 2]) / 2 - img_center[1],
                                (det[:, 1] + det[:, 3]) / 2 - img_center[0]
                            ])
                            offset_dist_squared = np.sum(
                                np.power(offsets, 2.0), 0)
                            index = np.argmax(
                                bounding_box_size - offset_dist_squared *
                                2.0)  # some extra weight on the centering

                            bb = np.squeeze(det[index])

                            bb[0] = max(0, bb[0])
                            bb[1] = max(0, bb[1])
                            bb[2] = min(bb[2], img_size[1])
                            bb[3] = min(bb[3], img_size[0])

                            if ((bb[0] >= img_size[1])
                                    or (bb[1] >= img_size[0])
                                    or (bb[2] > img_size[1])
                                    or (bb[3] > img_size[0])):
                                print('Unable to align "%s", bbox error' %
                                      image_path)
                                continue

                            h = bb[3] - bb[1]
                            w = bb[2] - bb[0]
                            x = bb[0]
                            y = bb[1]
                            _w = int(
                                (float(h) / image_size[0]) * image_size[1])
                            x += (w - _w) // 2
                            #x = min( max(0,x), img.shape[1] )
                            x = max(0, x)
                            xw = x + _w
                            xw = min(xw, img.shape[1])
                            roi = np.array((x, y, xw, y + h), dtype=np.int32)

                            faceImg = img[roi[1]:roi[3], roi[0]:roi[2], :]
                            dst = points[index, :]
                            tform = trans.SimilarityTransform()
                            tform.estimate(dst, src)
                            M = tform.params[0:2, :]
                            warped = cv2.warpAffine(
                                img,
                                M, (image_size[1], image_size[0]),
                                borderValue=0.0)
                            #M = tform.params
                            #warped = cv2.warpPerspective(img,M,(image_size[1],image_size[0]), borderValue = 0.0)
                            if (warped is None) or (np.sum(warped) == 0):
                                warped = faceImg
                                warped = cv2.resize(
                                    warped, (image_size[1], image_size[0]))

                            aligned_imgs.append(warped)
                            multiFacesRF.write(imgName.split('.')[0] + '\n')
                            break
                        else:
                            bb = np.squeeze(det[0])

                            bb[0] = max(0, bb[0])
                            bb[1] = max(0, bb[1])
                            bb[2] = min(bb[2], img_size[1])
                            bb[3] = min(bb[3], img_size[0])

                            if ((bb[0] >= img_size[1])
                                    or (bb[1] >= img_size[0])
                                    or (bb[2] > img_size[1])
                                    or (bb[3] > img_size[0])):
                                continue

                            h = bb[3] - bb[1]
                            w = bb[2] - bb[0]
                            x = bb[0]
                            y = bb[1]
                            _w = int(
                                (float(h) / image_size[0]) * image_size[1])
                            x += (w - _w) // 2
                            #x = min( max(0,x), img.shape[1] )
                            x = max(0, x)
                            xw = x + _w
                            xw = min(xw, img.shape[1])
                            roi = np.array((x, y, xw, y + h), dtype=np.int32)

                            faceImg = img[roi[1]:roi[3], roi[0]:roi[2], :]
                            dst = points[0, :]
                            tform = trans.SimilarityTransform()
                            tform.estimate(dst, src)
                            M = tform.params[0:2, :]
                            warped = cv2.warpAffine(
                                img,
                                M, (image_size[1], image_size[0]),
                                borderValue=0.0)
                            #M = tform.params
                            #warped = cv2.warpPerspective(img,M,(image_size[1],image_size[0]), borderValue = 0.0)
                            if (warped is None) or (np.sum(warped) == 0):
                                warped = faceImg
                                warped = cv2.resize(
                                    warped, (image_size[1], image_size[0]))

                            aligned_imgs.append(warped)
                            break

                if len(aligned_imgs) > 0:
                    cv2.imwrite(outImgPath, aligned_imgs[0])
                    break
                else:
                    if newFlag:
                        forceFacesRF.write(imgName.split('.')[0] + '\n')
                        newFlag = False

    multiFacesRF.close()
    forceFacesRF.close()
Example #9
0
def faceboxes_process(fd_net, o_net, landmark_net, img):
    img_width = img.shape[1]
    img_height = img.shape[0]

    im = img.copy()
    im = im.astype(np.float32)
    im[:, :, 0] = im[:, :, 0] - 104.0
    im[:, :, 1] = im[:, :, 1] - 117.0
    im[:, :, 2] = im[:, :, 2] - 123.0
    transformed_image = im.swapaxes(1, 2).swapaxes(0, 1)
    fd_net.blobs['data'].reshape(1, 3, img_height, img_width)
    fd_net.blobs['data'].data[...] = transformed_image
    fd_net.forward()
    detections = fd_net.get_blob('detection_out').data
    print(detections)
    # print(detections)

    bounding_boxes = detections[0, 0, :, 3:7] * np.array(
        [img_width, img_height, img_width, img_height])
    conf = detections[0, 0, :, 2]  # 计算出的置信度,在计算熵的时候会用到
    nrof_faces = bounding_boxes.shape[0]
    #print(nrof_faces)
    print(nrof_faces)
    crop_img_list = []
    for i in range(nrof_faces):
        if (conf[i] > 0.5):
            xmin = bounding_boxes[i][0]
            ymin = bounding_boxes[i][1]
            xmax = bounding_boxes[i][2]
            ymax = bounding_boxes[i][3]
            box_w = xmax - xmin
            box_h = ymax - ymin

            box_side = max(box_w, box_h)
            xmin += box_w * 0.5 - box_side * 0.5
            ymin += box_h * 0.5 - box_side * 0.5
            xmax = xmin + box_side
            ymax = ymin + box_side

            box = np.array([
                max(xmin, 0),
                max(ymin, 0),
                min(xmax, img_width),
                min(ymax, img_height)
            ])
            box = box.astype(np.int32)

            img_ = img.copy()
            crop_img = img_[box[1]:box[3], box[0]:box[2]]
            landmark_img = crop_img.copy()
            test_img = img_[box[1] - 20:box[3] + 20, box[0] - 20:box[2] + 20]
            image_hist(test_img)  # 绘制人脸的直方图

            cv2.imshow(test_img)
            cv2.waitKey(1)

            # 在这里画出添加一个区分背景和前景的,论文中使用掩模的方法
            # 这里裁剪的图像的大小是多少呢?
            test_mask_hist(test_img)

            # 画出人脸的点
            src_lmarks = forward_lnet106(img,
                                         landmark_net,
                                         box,
                                         is_minicaffe=True)
            for j in range(3, 30):
                cv2.circle(img, (src_lmarks[2 * j], src_lmarks[2 * j + 1]), 2,
                           (0, 0, 255), -1)

            cv2.imshow('landmark', img)
            # cv2.waitKey(0)

            image_hist(test_img)
            # cv2.imwrite("onet_result_"+ str(i) +".jpg", crop_img)
            cv2.imwrite("onet_result_" + str(i) + ".jpg", crop_img)
            # crop_img = cv2.cvtColor(crop_img, cv2.COLOR_BGR2RGB) # 这里没有转灰度图像
            crop_img = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)  # 这里转为灰度图像
            crop_img = (crop_img - 127.5) / 128  # 归一化
            scale_img = cv2.resize(crop_img, (48, 48))

            scale_img = np.swapaxes(scale_img, 0, 2)  # hwc chw
            o_net.blobs['data'].data[...] = scale_img
            out = o_net.forward()
            # src_lmarks = out['conv6-3']
            src_lmarks = o_net.get_blob('conv6-3').data  # 获取某一层的数据
            src_lmarks = src_lmarks.reshape(2, 5).T

            for j in range(5):
                cv2.circle(landmark_img,
                           (int(src_lmarks[j][0] * landmark_img.shape[1]),
                            int(src_lmarks[j][1] * landmark_img.shape[0])), 2,
                           (0, 0, 255), -1)
                src_lmarks[j][
                    0] = src_lmarks[j][0] * landmark_img.shape[1] + box[0]
                src_lmarks[j][
                    1] = src_lmarks[j][1] * landmark_img.shape[0] + box[1]

            tform = trans.SimilarityTransform()  # 这是相似转换?
            tform.estimate(src_lmarks, dst_lmarks)
            M = tform.params[0:2, :]
            crop_img = cv2.warpAffine(img, M, (112, 112))

            delt_w = (src_lmarks[1][0] - src_lmarks[0][0]) / 4
            delt_h = (src_lmarks[3][1] - src_lmarks[0][1]) / 4
            if src_lmarks[2][0] > src_lmarks[1][0] - delt_w or \
               src_lmarks[2][0] < src_lmarks[0][0] + delt_w or \
               src_lmarks[2][1] > src_lmarks[3][1] - delt_h or \
               src_lmarks[2][1] < src_lmarks[0][1] + delt_h:
                continue
            else:

                for j in range(5):
                    cv2.circle(img,
                               (int(src_lmarks[j][0]), int(src_lmarks[j][1])),
                               2, (0, 0, 255), -1)

            cv2.ellipse(img, (int((src_lmarks[0][0] + src_lmarks[1][0]) / 2), src_lmarks[2][1]), \
                             (int((box[2] - box[0])/2.2), int((box[3] - box[1])/2)), 0, 0, 360, (255, 255, 255), 1)
            cv2.imshow("landmark", crop_img)
            cv2.imwrite("onet_result_" + str(i) + ".jpg", crop_img)
            print(crop_img.shape)
            crop_img_list.append(crop_img)

            cv2.rectangle(img, (box[0], box[1]), (box[2], box[3]), (0, 255, 0),
                          5)
            bboxes = box
    return crop_img_list, box
# Compose transforms by multiplying their matrices
matrix = np.linalg.inv(shift.params) @ rotation.params @ shift.params
tform = transform.EuclideanTransform(matrix)
tf_img = transform.warp(img, tform.inverse)
fig, ax = plt.subplots()
_ = ax.imshow(tf_img)

######################################################################
# Similarity transformation
# =================================
#
# A `similarity transformation <https://en.wikipedia.org/wiki/Similarity_(geometry)>`_
# preserves the shape of objects. It combines scaling, translation and rotation.

tform = transform.SimilarityTransform(scale=0.5,
                                      rotation=np.pi / 12,
                                      translation=(100, 50))
print(tform.params)
tf_img = transform.warp(img, tform.inverse)
fig, ax = plt.subplots()
ax.imshow(tf_img)
_ = ax.set_title('Similarity transformation')

######################################################################
# Affine transformation
# =================================
#
# An `affine transformation <https://en.wikipedia.org/wiki/Affine_transformation>`_
# preserves lines (hence the alignment of objects), as well as parallelism
# between lines. It can be decomposed into a similarity transform and a
# `shear transformation <https://en.wikipedia.org/wiki/Shear_mapping>`_.
def main(args):
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    # Store some git revision info in a text file in the log directory
    src_path,_ = os.path.split(os.path.realpath(__file__))
    #facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))
    dataset = face_image.get_dataset(args.name, args.input_dir)
    print('dataset size', args.name, len(dataset))
    
    print('Creating networks and loading parameters')
    
    with tf.Graph().as_default():
        #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        #sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess = tf.Session()
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
    
    minsize = 100 # minimum size of face
    threshold = [ 0.6, 0.7, 0.7 ]  # three steps's threshold
    factor = 0.709 # scale factor
    #image_size = [112,96]
    image_size = [112,112]
    src = np.array([
      [30.2946, 51.6963],
      [65.5318, 51.5014],
      [48.0252, 71.7366],
      [33.5493, 92.3655],
      [62.7299, 92.2041] ], dtype=np.float32 )

    if image_size[1]==112:
      src[:,0] += 8.0

    # Add a random key to the filename to allow alignment using multiple processes
    #random_key = np.random.randint(0, high=99999)
    #bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    #output_filename = os.path.join(output_dir, 'faceinsight_align_%s.lst' % args.name)
    if not os.path.exists(args.output_dir):
      os.makedirs(args.output_dir)

    output_filename = os.path.join(args.output_dir, 'lst')
    
    
    with open(output_filename, "w") as text_file:
        nrof_images_total = 0
        nrof = np.zeros( (5,), dtype=np.int32)
        for fimage in dataset:
            if nrof_images_total%100==0:
              print("Processing %d, (%s)" % (nrof_images_total, nrof))
            nrof_images_total += 1
            #if nrof_images_total<950000:
            #  continue
            image_path = fimage.image_path
            if not os.path.exists(image_path):
              print('image not found (%s)'%image_path)
              continue
            filename = os.path.splitext(os.path.split(image_path)[1])[0]
            #print(image_path)
            try:
                img = misc.imread(image_path)
            except (IOError, ValueError, IndexError) as e:
                errorMessage = '{}: {}'.format(image_path, e)
                print(errorMessage)
            else:
                if img.ndim<2:
                    print('Unable to align "%s", img dim error' % image_path)
                    #text_file.write('%s\n' % (output_filename))
                    continue
                if img.ndim == 2:
                    img = to_rgb(img)
                img = img[:,:,0:3]
                _paths = fimage.image_path.split('/')
                a,b,c = _paths[-3], _paths[-2], _paths[-1]
                target_dir = os.path.join(args.output_dir, a, b)
                if not os.path.exists(target_dir):
                  os.makedirs(target_dir)
                target_file = os.path.join(target_dir, c)
                warped = None
                if fimage.landmark is not None:
                  dst = fimage.landmark.astype(np.float32)

                  tform = trans.SimilarityTransform()
                  tform.estimate(dst, src[0:3,:]*1.5+image_size[0]*0.25)
                  M = tform.params[0:2,:]
                  warped0 = cv2.warpAffine(img,M,(image_size[1]*2,image_size[0]*2), borderValue = 0.0)
                  _minsize = image_size[0]
                  bounding_boxes, points = detect_face.detect_face(warped0, _minsize, pnet, rnet, onet, threshold, factor)
                  if bounding_boxes.shape[0]>0:
                    bindex = 0
                    det = bounding_boxes[bindex,0:4]
                    #points need to be transpose, points = points.reshape( (5,2) ).transpose()
                    dst = points[:, bindex].reshape( (2,5) ).T
                    tform = trans.SimilarityTransform()
                    tform.estimate(dst, src)
                    M = tform.params[0:2,:]
                    warped = cv2.warpAffine(warped0,M,(image_size[1],image_size[0]), borderValue = 0.0)
                    nrof[0]+=1
                #assert fimage.bbox is not None
                if warped is None and fimage.bbox is not None:
                  _minsize = img.shape[0]//4
                  bounding_boxes, points = detect_face.detect_face(img, _minsize, pnet, rnet, onet, threshold, factor)
                  if bounding_boxes.shape[0]>0:
                    det = bounding_boxes[:,0:4]
                    bindex = -1
                    index2 = [0.0, 0]
                    for i in range(det.shape[0]):
                      _det = det[i]
                      iou = IOU(fimage.bbox, _det)
                      if iou>index2[0]:
                        index2[0] = iou
                        index2[1] = i
                    if index2[0]>0.3:
                      bindex = index2[1]
                    if bindex>=0:
                      dst = points[:, bindex].reshape( (2,5) ).T
                      tform = trans.SimilarityTransform()
                      tform.estimate(dst, src)
                      M = tform.params[0:2,:]
                      warped = cv2.warpAffine(img,M,(image_size[1],image_size[0]), borderValue = 0.0)
                      nrof[1]+=1
                      #print('1',target_file,index2[0])
                if warped is None and fimage.bbox is not None:
                  bb = fimage.bbox
                  #croped = img[bb[1]:bb[3],bb[0]:bb[2],:]
                  bounding_boxes, points = detect_face.detect_face_force(img, bb, pnet, rnet, onet)
                  assert bounding_boxes.shape[0]==1
                  _box = bounding_boxes[0]
                  if _box[4]>=0.3:
                    dst = points[:, 0].reshape( (2,5) ).T
                    tform = trans.SimilarityTransform()
                    tform.estimate(dst, src)
                    M = tform.params[0:2,:]
                    warped = cv2.warpAffine(img,M,(image_size[1],image_size[0]), borderValue = 0.0)
                    nrof[2]+=1
                    #print('2',target_file)

                if warped is None:
                  roi = np.zeros( (4,), dtype=np.int32)
                  roi[0] = int(img.shape[1]*0.06)
                  roi[1] = int(img.shape[0]*0.06)
                  roi[2] = img.shape[1]-roi[0]
                  roi[3] = img.shape[0]-roi[1]
                  if fimage.bbox is not None:
                    bb = fimage.bbox
                    h = bb[3]-bb[1]
                    w = bb[2]-bb[0]
                    x = bb[0]
                    y = bb[1]
                    #roi = np.copy(bb)
                    _w = int( (float(h)/image_size[0])*image_size[1] )
                    x += (w-_w)//2
                    #x = min( max(0,x), img.shape[1] )
                    x = max(0,x)
                    xw = x+_w
                    xw = min(xw, img.shape[1])
                    roi = np.array( (x, y, xw, y+h), dtype=np.int32)
                    nrof[3]+=1
                  else:
                    nrof[4]+=1
                  #print('3',bb,roi,img.shape)
                  #print('3',target_file)
                  warped = img[roi[1]:roi[3],roi[0]:roi[2],:]
                  #print(warped.shape)
                  warped = cv2.resize(warped, (image_size[1], image_size[0]))
                bgr = warped[...,::-1]
                cv2.imwrite(target_file, bgr)
                oline = '%d\t%s\t%d\n' % (1,target_file, int(fimage.classname))
                text_file.write(oline)
Example #12
0
        if len(train) < 200:
            k += 1
            continue
        output.append(classID)
        #105*122 features
        image = []
        for i in range(12810):
            image.append(0.0)
        for i in range(1, len(train)):
            pixel = train[i].split(':')
            image[int(pixel[0]) - 1] = float(pixel[1])
        im = np.array(image).reshape(122, 105)
        #im = crop(im)
        #im = imresize(im,(128,128))
        angle = random.randint(8, 20)
        locat_y = random.randint(-20, -10)
        rand_scale = random.uniform(0.9, 1.2)
        tform = tf.SimilarityTransform(scale=rand_scale,
                                       rotation=math.pi / angle,
                                       translation=(im.shape[0] / 20, locat_y))
        rotated = tf.warp(im, tform)
        #back_rotated = tf.warp(im, tform.inverse)
        #im = crop(im)
        #im = imresize(im,(128,128))
        #im = Image.fromarray(im)
        imsave('random_warp/warp_%d_%d.png' % ((k + 1), classID), rotated)
        #new = plt.imshow(back_rotated)
        #plt.show()
        k += 1
        print(k)
def applyGeometricTransformation(startXs, startYs, newXs, newYs, bbox):
    import numpy as np
    import skimage.transform as tf
    import random 

    [rows,nobj] = np.asarray(startXs.shape)
    temp_Xs = []
    temp_Ys = []
    r = []

    homography = []
    second_box = np.copy(bbox.astype(np.double))
    count = 0
    for i in range(nobj):
        inliers_count = []
        inliers_firstX = []
        inliers_firstY = []
        inliers_secondX = []
        inliers_secondY = []
        box_corners = np.matrix.transpose(bbox[i, :, :])
        firstX = startXs[:, i]
        firstY = startYs[:, i]
        firstX = firstX[firstX!=-1]
        firstY = firstY[firstY!=-1]
        secondX = newXs[:, i]
        secondY = newYs[:, i]
        secondX = secondX[secondX!=-1]
        secondY = secondY[secondY!=-1]
        for k in range(500):
            for j in range(4):
                r.append(random.randint(0,np.shape(firstX)[0])-1)
            four_firstX = [firstX[x] for x in r]
            four_firstY = [firstY[x] for x in r]
            four_secondX = [secondX[x] for x in r]
            four_secondY = [secondY[x] for x in r]
            H_matrix = tf.SimilarityTransform()
            four_first = np.matrix.transpose(np.vstack((four_firstX,four_firstY)))
            four_second = np.matrix.transpose(np.vstack((four_secondX,four_secondY)))
            H_matrix.estimate(four_first, four_second)
            H = H_matrix.params
            homog_first = np.vstack((firstX, firstY, np.ones(rows)))
            new_second = np.dot(H, homog_first)
            diff_vector = np.vstack((secondX, secondY)) - new_second[0:2, :]

            diff_vector=diff_vector*diff_vector#find the square of the difference
            squared_dist=diff_vector[0,:]+diff_vector[1,:]
    
            temp_firstX = firstX[squared_dist <= 16]
            temp_firstY = firstY[squared_dist <= 16]
            temp_secondX = secondX[squared_dist <= 16]
            temp_secondY = secondY[squared_dist <= 16]
            inliers_count.append(np.shape(firstX)[0])
            homography.append(H)
            inliers_firstX.append(temp_firstX)
            inliers_firstY.append(temp_firstY)
            inliers_secondX.append(temp_secondX)
            inliers_secondY.append(temp_secondY)
        max_count_index = inliers_count.index(max(inliers_count))
        firstX = inliers_firstX[max_count_index]
        firstY = inliers_firstY[max_count_index]
        secondX = inliers_secondX[max_count_index]
        secondY = inliers_secondY[max_count_index]

        H_matrix.estimate(np.matrix.transpose(np.vstack((firstX, firstY))), np.matrix.transpose(np.vstack((secondX, secondY))))
        count = max(count, len(secondX))
        temp_Xs.append(secondX)
        temp_Ys.append(secondY)
        homo_box_corners = np.vstack((box_corners, np.ones(2)))
        H = H_matrix.params
        second_homo_box_corners = np.dot(H, homo_box_corners)
        second_box[i, :, :] = np.matrix.transpose(second_homo_box_corners[0:2, :])

    Xs = np.ones([count, nobj])*-1
    Ys = np.ones([count, nobj])*-1
    for m in range(nobj):
        Xs[0:len(temp_Xs[m]), m] = temp_Xs[m]
        Ys[0:len(temp_Ys[m]), m] = temp_Ys[m]
    return Xs, Ys, second_box
Example #14
0
def map_func2(coords):
    tform2 = transform.SimilarityTransform(scale=257.,
                                           rotation=0,
                                           translation=(255.5, 255.5))
    return tform2(np.tanh(tform(coords)))
Example #15
0
img_cv2 = np.array(img)[..., ::-1]

import pdb
pdb.set_trace()
src = np.array([[30.2946, 51.6963], [65.5318, 51.5014], [48.0252, 71.7366],
                [33.5493, 92.3655], [62.7299, 92.2041]],
               dtype=np.float32)

src[:, 0] *= (img.size[0] / 96)
src[:, 1] *= (img.size[1] / 112)
print(img.size)
print(src)
import pdb
pdb.set_trace()

bounding_boxes, landmarks = detect_faces(img)
dst = landmarks[0].astype(np.float32)
facial5points = [[dst[j], dst[j + 5]] for j in range(5)]

tform = trans.SimilarityTransform()
tform.estimate(np.array(facial5points), src)
M = tform.params[0:2, :]
print('M', M)
warped = cv2.warpAffine(img_cv2,
                        M, (img.size[0], img.size[1]),
                        borderValue=0.0)
print('warped shaped', warped.shape)
temp = Image.fromarray(warped[..., ::-1])
print('temp', temp.shape)
print('img', img)
Example #16
0
def RndTform(img,val):
    Ih,Iw = img[0].shape[:2]
    
    sgn = torch.randint(0,2,(1,)).item() * 2 - 1

    if sgn>0:
        dw = val
        dh = 0
    else:
        dw = 0
        dh = val

    def rd(d): return torch.empty(1).uniform_(-d,d).item()
    def fd(d): return torch.empty(1).uniform_(-dw,d).item()

    # generate a random projective transform
    # adapted from https://navoshta.com/traffic-signs-classification/
    tl_top = rd(dh)
    tl_left = fd(dw)
    bl_bottom = rd(dh)
    bl_left = fd(dw)
    tr_top = rd(dh)
    tr_right = fd( min(Iw * 3/4 - tl_left,dw) )
    br_bottom = rd(dh)
    br_right = fd( min(Iw * 3/4 - bl_left,dw) )

    tform = stf.ProjectiveTransform()
    tform.estimate(np.array((
        (tl_left, tl_top),
        (bl_left, Ih - bl_bottom),
        (Iw - br_right, Ih - br_bottom),
        (Iw - tr_right, tr_top)
    )), np.array((
        [0, 0 ],
        [0, Ih - 1 ],
        [Iw-1, Ih-1 ],
        [Iw-1, 0]
    )))

    # determine shape of output image, to preserve size
    # trick take from the implementation of skimage.transform.rotate
    corners = np.array([
        [0, 0 ],
        [0, Ih - 1 ],
        [Iw-1, Ih-1 ],
        [Iw-1, 0]
    ])

    corners = tform.inverse(corners)
    minc = corners[:, 0].min()
    minr = corners[:, 1].min()
    maxc = corners[:, 0].max()
    maxr = corners[:, 1].max()
    out_rows = maxr - minr + 1
    out_cols = maxc - minc + 1
    output_shape = np.around((out_rows, out_cols))

    # fit output image in new shape
    translation = (minc, minr)
    tform4 = stf.SimilarityTransform(translation=translation)
    tform = tform4 + tform
    # normalize
    tform.params /= tform.params[2, 2]
    

    ret = []
    for i in range(len(img)):
        img2 = stf.warp(img[i], tform, output_shape=output_shape, cval=1.0)
        img2 = stf.resize(img2, (Ih,Iw), preserve_range=True).astype(np.float32)
        ret.append(img2)


    return ret
Example #17
0
def image_deformation(image):
    tform = tf.SimilarityTransform(scale=1,
                                   rotation=np.random.random() * np.pi / 12,
                                   translation=(0, .1))

    return tf.warp(image, tform)
def apply_translation(img):
    translated = 255*warp(img, transform.SimilarityTransform(translation=(np.random.uniform(-5, 5), np.random.uniform(-5, 5))),mode='edge')
    translated = translated.astype(np.uint8)
    return translated.astype(np.uint8)
Example #19
0
    def augment_img_batch(self, img_batch, augment_methods):
        imgs = []
        for sample_ind in range(img_batch.shape[0]):
            img = img_batch[sample_ind, :, :, :]

            if VFLIP in augment_methods:
                if np.random.uniform() > 0.5:
                    img = np.flipud(img)

            if HFLIP in augment_methods:
                if np.random.uniform() > 0.5:
                    img = np.fliplr(img)

            if ROTATE90 in augment_methods:
                nb_rotations = np.random.randint(0, 4)
                img = np.rot90(img, nb_rotations)

            skimage_augment_methods = [ROTATE, TRANSLATE, ZOOM]
            if set(skimage_augment_methods).intersection(set(augment_methods)):
                # skimage requires that float images have values
                # in [-1, 1] so we have to scale and then unscale the image to
                # achieve this.
                max_val = np.max(np.absolute(img.ravel()))
                img = img / max_val
                nb_rows, nb_cols = img.shape[0:2]

                if TRANSLATE in augment_methods:
                    max_trans_ratio = 0.1
                    trans_row_bound = int(nb_rows * max_trans_ratio)
                    trans_col_bound = int(nb_cols * max_trans_ratio)
                    translation = (np.random.randint(-trans_row_bound,
                                                     trans_row_bound),
                                   np.random.randint(-trans_col_bound,
                                                     trans_col_bound))
                    tf = transform.SimilarityTransform(translation=translation)
                    img = transform.warp(img, tf, mode='reflect')

                if ZOOM in augment_methods:
                    shift_x = shift_y = int(nb_rows / 2)
                    scale_x = scale_y = np.random.uniform(-0.15, 0.15) + 1.0
                    matrix_to_topleft = transform.SimilarityTransform(
                        translation=[-shift_x, -shift_y])
                    matrix_transforms = transform.AffineTransform(
                        scale=(scale_x, scale_y))
                    matrix_to_center = transform.SimilarityTransform(
                        translation=[shift_x, shift_y])
                    matrix = (matrix_to_topleft + matrix_transforms +
                              matrix_to_center)
                    matrix = matrix.inverse
                    img = transform.warp(img, matrix, mode='reflect')

                if ROTATE in augment_methods:
                    degrees = np.random.uniform(0, 360)
                    img = transform.rotate(img, degrees, mode='reflect')

                img = img * max_val

            imgs.append(np.expand_dims(img, axis=0))

        img_batch = np.concatenate(imgs, axis=0)
        return img_batch
    def keyPressEvent(self, event):
        #        print(event.key())

        self.mask2 = self.mask2.astype(np.float64)
        self.mask = self.mask.astype(np.float64)

        if event.key() == QtCore.Qt.Key_Right:

            self.Hoffset -= 1
            self.tform = tr.SimilarityTransform(
                translation=(int(self.Hoffset), int(self.Voffset)))
            self.mask2 = tr.warp(self.mask,
                                 self.tform,
                                 output_shape=(int(512), int(512)))
            [self.mask2, n] = msr.label(self.mask2, return_num=True)
            self.mask2 = sgm.clear_border(self.mask2)
            self.updateCanvas2()
            self.updateCanvas1()

        if event.key() == QtCore.Qt.Key_Left:

            self.Hoffset += 1

            self.tform = tr.SimilarityTransform(translation=(self.Hoffset,
                                                             self.Voffset))
            self.mask2 = tr.warp(self.mask,
                                 self.tform,
                                 output_shape=(int(512), int(512)))
            [self.mask2, n] = msr.label(self.mask2, return_num=True)
            self.mask2 = sgm.clear_border(self.mask2)
            self.updateCanvas2()
            self.updateCanvas1()

        if event.key() == QtCore.Qt.Key_Up:

            self.Voffset += 1

            self.tform = tr.SimilarityTransform(translation=(self.Hoffset,
                                                             self.Voffset))
            self.mask2 = tr.warp(self.mask,
                                 self.tform,
                                 output_shape=(int(512), int(512)))
            [self.mask2, n] = msr.label(self.mask2, return_num=True)
            self.mask2 = sgm.clear_border(self.mask2)
            self.updateCanvas2()
            self.updateCanvas1()

        if event.key() == QtCore.Qt.Key_Down:

            self.Voffset -= 1

            self.tform = tr.SimilarityTransform(translation=(self.Hoffset,
                                                             self.Voffset))
            self.mask2 = tr.warp(self.mask,
                                 self.tform,
                                 output_shape=(int(512), int(512)))
            [self.mask2, n] = msr.label(self.mask2, return_num=True)
            self.mask2 = sgm.clear_border(self.mask2)
            self.updateCanvas2()
            self.updateCanvas1()

        self.setFocus()
Example #21
0
File: flca.py Project: xuejcak/flca
def doublechannel(indir0, indir, outdir, x0=0, x1=-1, y0=0, y1=-1, time0a=-23, time0b=-8, time1a=None,
               time1b=None, skip=1, xoffset=0, yoffset=0, sigma=20, threshold='0.', kr=0., 
               biascorrect=0, interpolate=0, verbose=0):
    print('Double-channel coalignment.')
    import datetime
    def is_digit(stri):
        return stri.isdigit()
    absflag=0; filterflag=1; marg = 0.2 # Margins of the calculated shifts that are excluded.
    if indir0[-1] != '/': indir0 += '/'
    if indir[-1] != '/': indir += '/'
    if outdir[-1] != '/': outdir += '/'
    filin0 = glob(indir0+"*.f*ts")
    nfilin0 = len(filin0)
    if nfilin0 == 0:
        print(f"The reference folder {indir0} is empty!")
        return -1
    filin0.sort()
    filin = glob(indir+"*.f*ts")
    nfilin = len(filin)
    if nfilin == 0:
        print(f"The input folder {indir} is empty!")
        return -1
    filin.sort()
    #ind0 = 0
    
    tims0 = "".join(filter(is_digit, filin0[0][time0a:time0b]))
    tims1 = "".join(filter(is_digit, filin[0][time1a:time1b]))
    if len(tims0) != 14 or len(tims1) != 14:
        print("time0a, time0b, time1a, time1b: time should be from year to second")
        return -1
    timarr = [];
    for ii in range(nfilin0):
        tims = "".join(filter(is_digit, filin0[ii][time0a:time0b]))
        timdate=datetime.datetime(int(tims[0:4]), int(tims[4:6]), int(tims[6:8]), int(tims[8:10]), 
                                  int(tims[10:12]), int(tims[12:14]))
        timarr.append(timdate)
    timarr = np.array(timarr, dtype=datetime.datetime)
            
    xoffset = xoffset % skip; yoffset = yoffset % skip
    if threshold[-1] == 'a':
        absflag = 1;
        threshold = float(threshold[0:-1])
    else:
        threshold = float(threshold)
    if kr <= 0. or kr > 20.:
        print("Nonsense value of kr, = {}".format(kr))
        return -1
    sigma = ct.c_double(sigma);
    thresh = ct.c_double(threshold);
    kr = ct.c_double(kr);
    
    # Notice that for 2-D array in python and C languages, the 1st dimension is indicated by y and the 2nd by x.
    # It is consistent with images.
    # But in flcasubs.c, the 1st dimension is marked with x and 2nd with y, following FLCT.
    ny = y1-y0
    nx = x1-x0
    nys = int(np.ceil((ny-yoffset)/skip)) # dimensions for the results
    nxs = int(np.ceil((nx-xoffset)/skip))
    indx0 = int(nxs*marg); indx1 = int(np.ceil(nxs-indx0)); indy0 = int(nys*marg); indy1 = int(np.ceil(nys-indy0))
    ArrayType = ct.c_double*(nx*ny)
    f1ct = ArrayType();
    f2ct = ArrayType();

    print("{} files will be coaligned".format(nfilin))
    
    shiftx0 = 0; shifty0 = 0
    sxarr = np.zeros(nfilin); syarr = np.zeros(nfilin)
    i0 = 0
    for ii in range(nfilin):
        tims1 = "".join(filter(is_digit, filin[ii][time1a:time1b]))
        timdate=datetime.datetime(int(tims1[0:4]), int(tims1[4:6]), int(tims1[6:8]), int(tims1[8:10]), 
                                  int(tims1[10:12]), int(tims1[12:14]))
        for i1 in range(i0, nfilin0):
            if timarr[i1]>timdate: break
        if i1 > i0:
            timd1 = timdate-timarr[i1-1]
            timd2 = timarr[i1]-timdate
            if timd1.seconds < timd2.seconds:
                i0 = i1-1
            else:
                i0 = i1
        print("{} {} {}".format(ii, os.path.basename(filin[ii]), os.path.basename(filin0[i0])), end="\n")    
        data, hdr = readfits(filin0[i0])
        f1 = data[y0:y1, x0:x1]
        data, hdr = readfits(filin[ii])
        tform = transform.SimilarityTransform(translation=(shiftx0,shifty0))
        data1 = transform.warp(data, tform, order=interpolate)
        f2 = data1[y0:y1, x0:x1]
        # tate care, all the normal data are assumed to be larger than 0
        f1[np.logical_or(np.isnan(f1), np.isnan(f2))] = threshold-10
        f2[np.logical_or(np.isnan(f1), np.isnan(f2))] = threshold-10
        #f1 = detrend(f1)
        #f2 = detrend(f2)
        for i in range(ny):
            for j in range(nx):
                f1ct[i*nx+j] = f1[i,j]
                f2ct[i*nx+j] = f2[i,j]
        vxct = ArrayType()
        vyct = ArrayType()
        vmct = ArrayType()
        # The 1st dimension is y and the 2nd is x.
        tmp = flcas.flca(f1ct, f2ct, ny, nx, sigma, vyct, vxct, vmct, thresh, absflag, 
                         filterflag, kr, skip, yoffset, xoffset, biascorrect, verbose)
        
        # reshape and select
        vx = np.array(vxct).reshape([ny, nx])
        vy = np.array(vyct).reshape([ny, nx])
        vm = np.array(vmct).reshape([ny, nx]);
        vx = vx[yoffset::skip, xoffset::skip]
        vy = vy[yoffset::skip, xoffset::skip]
        vm = vm[yoffset::skip, xoffset::skip];
        vx = vx[indy0:indy1, indx0:indx1]
        vy = vy[indy0:indy1, indx0:indx1]
        vm = vm[indy0:indy1, indx0:indx1];
        vm[np.logical_or(np.isnan(vx), np.isnan(vy))] = 0
        shiftx = np.median(vx[vm == 1.])
        shifty = np.median(vy[vm == 1.])
        if shiftx == np.nan or shifty == np.nan:
            print("Shift X or shift Y is Nan, return.")
            return -1
        shiftx0 = shiftx+shiftx0; shifty0 = shifty+shifty0
        if interpolate == 0:
            shiftx0 = round(shiftx0); shifty0 = round(shifty0)
        tform = transform.SimilarityTransform(translation=(shiftx0,shifty0))
        data = transform.warp(data, tform, order=interpolate)
        # order = 0: Nearest-neighbor; order = 1: Bi-linear (default). Other choices are not included.
        fname0 = os.path.basename(filin[ii])
        fits.writeto(outdir+fname0, data, hdr, output_verify='fix', overwrite=True, checksum=False)
        sxarr[ii] = shiftx0; syarr[ii] = shifty0
    np.savez_compressed('shiftxy.npz', sx=sxarr, sy=syarr)
    return 0
    def transform(self, Xb, yb):
        Xb, yb = super(DataAugmentationBatchIterator, self).transform(Xb, yb)

        augmentation_params = {
            'zoom_range': (1.0, 1.1),
            'rotation_range': (0, 360),
            'shear_range': (0, 20),
            'translation_range': (-4, 4),
        }

        IMAGE_WIDTH = PIXELS
        IMAGE_HEIGHT = PIXELS

        def fast_warp(img, tf, output_shape=(PIXELS,PIXELS), mode='nearest'):
            """
            This wrapper function is about five times faster than skimage.transform.warp, for our use case.
            """
            #m = tf._matrix
            m = tf.params
            img_wf = np.empty((output_shape[0], output_shape[1]), dtype='float32')
            #for k in xrange(1):
            #    img_wf[..., k] = skimage.transform._warps_cy._warp_fast(img[..., k], m, output_shape=output_shape, mode=mode)
            img_wf = skimage.transform._warps_cy._warp_fast(img, m, output_shape=output_shape, mode=mode)
            return img_wf

        def random_perturbation_transform(zoom_range, rotation_range, shear_range, translation_range, do_flip=True):
            # random shift [-10, 10] - shift no longer needs to be integer!
            shift_x = np.random.uniform(*translation_range)
            shift_y = np.random.uniform(*translation_range)
            translation = (shift_x, shift_y)

            # random rotation [0, 360]
            rotation = np.random.uniform(*rotation_range) # there is no post-augmentation, so full rotations here!

            # random shear [0, 20]
            shear = np.random.uniform(*shear_range)

            # random zoom [0.9, 1.1]
            # zoom = np.random.uniform(*zoom_range)
            log_zoom_range = [np.log(z) for z in zoom_range]
            zoom = np.exp(np.random.uniform(*log_zoom_range)) # for a zoom factor this sampling approach makes more sense.
            # the range should be multiplicatively symmetric, so [1/1.1, 1.1] instead of [0.9, 1.1] makes more sense.

            translation = (0,0)
            rotation = 0.0
            shear = 0.0
            zoom = 1.0

            
            rotate =  np.random.randint(4)
            if rotate == 0:
                rotation = 0.0
            elif rotate == 1:
                rotation = 90.0
            elif rotate == 2:
                rotation = 180.0
            else:
                rotation = 270.0

            '''
            # only translate 40% of the cases
            trans =  np.random.randint(10)
            if trans == 0:
                translation = (-2,-2)
            elif trans == 1:
                translation = (2,2)
            else:
                translation = (0,0)
            '''         

            '''
            zooming =  np.random.randint(3)
            if zooming == 0:
                shear = 0
            elif zooming == 1 and rotate == 0:
                shear = 10
            elif zooming ==2 and rotate == 0:
                shear = 20
            else:
                shear = 0
            '''
            
            '''
            trans =  np.random.randint(5)
            if trans == 0:
                translation = (0,0)
            elif trans == 1:
                translation = (-4,0)
            elif trans == 2:
                translation = (0,-4)
            elif trans == 3:
                translation = (4,0)
            else:
                translation = (0,4)
            
            rotate =  np.random.randint(8)
            if rotate == 0:
                rotation = 0.0
            elif rotate == 1:
                rotation = 90.0
            elif rotate == 2:
                rotation = 180.0
            elif rotate == 3:
                rotation = 45.0
            elif rotate == 4:
                rotation = 135.0
            elif rotate == 5:
                rotation = 225.0
            elif rotate == 6:
                rotation = 315.0
            else:
                rotation = 270.0
            '''

            ## flip
            if do_flip and (np.random.randint(2) > 0): # flip half of the time
                shear += 180
                rotation += 180
                # shear by 180 degrees is equivalent to rotation by 180 degrees + flip.
                # So after that we rotate it another 180 degrees to get just the flip.            

            '''
            print "translation = ", translation
            print "rotation = ", rotation
            print "shear = ",shear
            print "zoom = ",zoom
            print ""
            '''

            return build_augmentation_transform(zoom, rotation, shear, translation)


        center_shift   = np.array((IMAGE_HEIGHT, IMAGE_WIDTH)) / 2. - 0.5
        tform_center   = transform.SimilarityTransform(translation=-center_shift)
        tform_uncenter = transform.SimilarityTransform(translation=center_shift)

        def build_augmentation_transform(zoom=1.0, rotation=0, shear=0, translation=(0, 0)):
            tform_augment = transform.AffineTransform(scale=(1/zoom, 1/zoom), 
                                                      rotation=np.deg2rad(rotation), 
                                                      shear=np.deg2rad(shear), 
                                                      translation=translation)
            tform = tform_center + tform_augment + tform_uncenter # shift to center, augment, shift back (for the rotation/shearing)
            return tform

        tform_augment  = random_perturbation_transform(**augmentation_params)
        tform_identity = skimage.transform.AffineTransform()
        tform_ds       = skimage.transform.AffineTransform()
        
        for i in range(Xb.shape[0]):
            new = fast_warp(Xb[i][0], tform_ds + tform_augment + tform_identity, output_shape=(PIXELS,PIXELS), mode='nearest').astype('float32')
            Xb[i,:] = new
            
        return Xb, yb
Example #23
0
if __name__ == "__main__":
    detector = RetinaFace(0)

    imgname = "examples/obama.png"
    img = cv2.imread(imgname)

    faces = detector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

    face = faces[0]

    box, landmark, score = face

    std_points_256 = np.array([
        [85.82991, 85.7792],
        [169.0532, 84.3381],
        [127.574, 137.0006],
        [90.6964, 174.7014],
        [167.3069, 173.3733],
    ])

    trans = transform.SimilarityTransform()

    res = trans.estimate(landmark, std_points_256)
    M = trans.params

    new_img = cv2.warpAffine(img, M[:2, :], dsize=(256, 256))

    cv2.imshow("new_img", new_img)
    cv2.waitKey(0)
Example #24
0
def gen_aug_images(img):
    '''
    Data augmentation batch iterator for feeding images into CNN.
    This example will randomly rotate all images in a given batch between -9 and 9 degrees
    and to random translations between -2 and 2 pixels in all directions.
    Be sure to remove part that displays the augmented images, it's only there to check
    that everything works correctly.
    '''

    img_aug = np.zeros((PIXELS, PIXELS, 3))
    # color intensity augmentation
    r_intensity = random.randint(0, 1)
    g_intensity = random.randint(0, 1)
    b_intensity = random.randint(0, 1)
    intensity_scaler = random.randint(-20, 20) / 255.

    # pad and crop settings
    trans_1 = random.randint(0, (PAD_CROP * 2))
    trans_2 = random.randint(0, (PAD_CROP * 2))
    crop_x1 = trans_1
    crop_x2 = (PIXELS + trans_1)
    crop_y1 = trans_2
    crop_y2 = (PIXELS + trans_2)

    # shearing
    shear_deg = random.uniform(-5, 5)

    # random rotations betweein -15 and 15 degrees
    dorotate = random.randint(-15, 15)

    # brightness settings
    bright = random.uniform(0.8, 1.2)

    # flip left-right choice
    #flip_lr = random.randint(0,1)

    # set the transform parameters for skimage.transform.warp
    # have to shift to center and then shift back after transformation otherwise
    # rotations will make image go out of frame
    center_shift = np.array((PIXELS, PIXELS)) / 2. - 0.5
    tform_center = transform.SimilarityTransform(translation=-center_shift)
    tform_uncenter = transform.SimilarityTransform(translation=center_shift)

    tform_aug = transform.AffineTransform(shear=np.deg2rad(shear_deg),
                                          rotation=np.deg2rad(dorotate))

    tform = tform_center + tform_aug + tform_uncenter

    # images in the batch do the augmentation
    #img = crop(img, crop_amt)

    for i in range(img_aug.shape[2]):
        img_aug[:, :, i] = fast_warp(img[:, :, i],
                                     tform,
                                     output_shape=(PIXELS, PIXELS))
        # pad and crop images
        img_pad = np.pad(img_aug[:, :, i],
                         pad_width=((PAD_CROP, PAD_CROP), (PAD_CROP,
                                                           PAD_CROP)),
                         mode='constant')
        img_aug[:, :, i] = img_pad[crop_x1:crop_x2, crop_y1:crop_y2]

    # adjust brightness
    img_aug = img_aug * bright

    if r_intensity == 1:
        img_aug[:, :, 0] += intensity_scaler
    if g_intensity == 1:
        img_aug[:, :, 1] += intensity_scaler
    if b_intensity == 1:
        img_aug[:, :, 2] += intensity_scaler

    img_aug[img_aug > 1] = 1.0

    #choice = randint(0,2)
    #if choice == 1:
    #    img_aug = np.fliplr(img_aug)

    return img_aug
Example #25
0
def create_aug_matrices(nb_matrices, img_width_px, img_height_px,
                        scale_to_percent=1.0, scale_axis_equally=False,
                        rotation_deg=0, shear_deg=0,
                        translation_x_px=0, translation_y_px=0,
                        seed=None):
    """Creates the augmentation matrices that may later be used to transform
    images.

    This is a wrapper around scikit-image's transform.AffineTransform class.
    You can apply those matrices to images using the apply_aug_matrices()
    function.

    Args:
        nb_matrices: How many matrices to return, e.g. 100 returns 100 different
            random-generated matrices (= 100 different transformations).
        img_width_px: Width of the images that will be transformed later
            on (same as the width of each of the matrices).
        img_height_px: Height of the images that will be transformed later
            on (same as the height of each of the matrices).
        scale_to_percent: Same as in ImageAugmenter.__init__().
            Up to which percentage the images may be
            scaled/zoomed. The negative scaling is automatically derived
            from this value. A value of 1.1 allows scaling by any value
            between -10% and +10%. You may set min and max values yourself
            by using a tuple instead, like (1.1, 1.2) to scale between
            +10% and +20%. Default is 1.0 (no scaling).
        scale_axis_equally: Same as in ImageAugmenter.__init__().
            Whether to always scale both axis (x and y)
            in the same way. If set to False, then e.g. the Augmenter
            might scale the x-axis by 20% and the y-axis by -5%.
            Default is False.
        rotation_deg: Same as in ImageAugmenter.__init__().
            By how much the image may be rotated around its
            center (in degrees). The negative rotation will automatically
            be derived from this value. E.g. a value of 20 allows any
            rotation between -20 degrees and +20 degrees. You may set min
            and max values yourself by using a tuple instead, e.g. (5, 20)
            to rotate between +5 und +20 degrees. Default is 0 (no
            rotation).
        shear_deg: Same as in ImageAugmenter.__init__().
            By how much the image may be sheared (in degrees). The
            negative value will automatically be derived from this value.
            E.g. a value of 20 allows any shear between -20 degrees and
            +20 degrees. You may set min and max values yourself by using a
            tuple instead, e.g. (5, 20) to shear between +5 und +20
            degrees. Default is 0 (no shear).
        translation_x_px: Same as in ImageAugmenter.__init__().
            By up to how many pixels the image may be
            translated (moved) on the x-axis. The negative value will
            automatically be derived from this value. E.g. a value of +7
            allows any translation between -7 and +7 pixels on the x-axis.
            You may set min and max values yourself by using a tuple
            instead, e.g. (5, 20) to translate between +5 und +20 pixels.
            Default is 0 (no translation on the x-axis).
        translation_y_px: Same as in ImageAugmenter.__init__().
            See translation_x_px, just for the y-axis.
        seed: Seed to use for python's and numpy's random functions.

    Returns:
        List of augmentation matrices.
    """
    assert nb_matrices > 0
    assert img_width_px > 0
    assert img_height_px > 0
    assert is_minmax_tuple(scale_to_percent) or scale_to_percent >= 1.0
    assert is_minmax_tuple(rotation_deg) or rotation_deg >= 0
    assert is_minmax_tuple(shear_deg) or shear_deg >= 0
    assert is_minmax_tuple(translation_x_px) or translation_x_px >= 0
    assert is_minmax_tuple(translation_y_px) or translation_y_px >= 0

    if seed is not None:
        random.seed(seed)
        np.random.seed(seed)

    result = []

    shift_x = int(img_width_px / 2.0)
    shift_y = int(img_height_px / 2.0)

    # prepare min and max values for
    # scaling/zooming (min/max values)
    if is_minmax_tuple(scale_to_percent):
        scale_x_min = scale_to_percent[0]
        scale_x_max = scale_to_percent[1]
    else:
        scale_x_min = scale_to_percent
        scale_x_max = 1.0 - (scale_to_percent - 1.0)
    assert scale_x_min > 0.0
    #if scale_x_max >= 2.0:
    #     warnings.warn("Scaling by more than 100 percent (%.2f)." % (scale_x_max,))
    scale_y_min = scale_x_min # scale_axis_equally affects the random value generation
    scale_y_max = scale_x_max

    # rotation (min/max values)
    if is_minmax_tuple(rotation_deg):
        rotation_deg_min = rotation_deg[0]
        rotation_deg_max = rotation_deg[1]
    else:
        rotation_deg_min = (-1) * int(rotation_deg)
        rotation_deg_max = int(rotation_deg)

    # shear (min/max values)
    if is_minmax_tuple(shear_deg):
        shear_deg_min = shear_deg[0]
        shear_deg_max = shear_deg[1]
    else:
        shear_deg_min = (-1) * int(shear_deg)
        shear_deg_max = int(shear_deg)

    # translation x-axis (min/max values)
    if is_minmax_tuple(translation_x_px):
        translation_x_px_min = translation_x_px[0]
        translation_x_px_max = translation_x_px[1]
    else:
        translation_x_px_min = (-1) * translation_x_px
        translation_x_px_max = translation_x_px

    # translation y-axis (min/max values)
    if is_minmax_tuple(translation_y_px):
        translation_y_px_min = translation_y_px[0]
        translation_y_px_max = translation_y_px[1]
    else:
        translation_y_px_min = (-1) * translation_y_px
        translation_y_px_max = translation_y_px

    # create nb_matrices randomized affine transformation matrices
    for _ in range(nb_matrices):
        # generate random values for scaling, rotation, shear, translation
        scale_x = random.uniform(scale_x_min, scale_x_max)
        scale_y = random.uniform(scale_y_min, scale_y_max)
        if not scale_axis_equally:
            scale_y = random.uniform(scale_y_min, scale_y_max)
        else:
            scale_y = scale_x
        rotation = np.deg2rad(random.randint(rotation_deg_min, rotation_deg_max))
        shear = np.deg2rad(random.randint(shear_deg_min, shear_deg_max))
        translation_x = random.randint(translation_x_px_min, translation_x_px_max)
        translation_y = random.randint(translation_y_px_min, translation_y_px_max)

        # create three affine transformation matrices
        # 1st one moves the image to the top left, 2nd one transforms it, 3rd one
        # moves it back to the center.
        # The movement is neccessary, because rotation is applied to the top left
        # and not to the image's center (same for scaling and shear).
        matrix_to_topleft = tf.SimilarityTransform(translation=[-shift_x, -shift_y])
        matrix_transforms = tf.AffineTransform(scale=(scale_x, scale_y),
                                               rotation=rotation, shear=shear,
                                               translation=(translation_x,
                                                            translation_y))
        matrix_to_center = tf.SimilarityTransform(translation=[shift_x, shift_y])

        # Combine the three matrices to one affine transformation (one matrix)
        matrix = matrix_to_topleft + matrix_transforms + matrix_to_center

        # one matrix is ready, add it to the result
        result.append(matrix.inverse)

    return result
Example #26
0
def show_radon_registration(img1, rotation_ccw_deg, translation_pixels,
                            num_angles):
    '''The function  get image and transform parameters and apply transformation and radon transform,then show the results and the diffirences betwwen them'''

    imgGrey = img1
    allSize = imgGrey.shape[0]
    padSize = imgGrey.shape[0] / 2
    #pad the image to get correct transform
    imgPad = np.pad(imgGrey, padSize, mode="edge")
    #get the transformation
    tform = tf.SimilarityTransform(scale=1,
                                   rotation=math.radians(rotation_ccw_deg),
                                   translation=translation_pixels)

    #apply the transformation
    imgWarped = tf.warp(imgPad, tform.inverse)

    #show results
    plt.figure(1)
    #plt.title("Fixed Brain With Padding")
    plt.title("Phantom With Padding")
    plt.imshow(imgPad, cmap=cm.gray)
    plt.figure(2)
    #plt.title("Fixed Brain After Padding and transformation")
    plt.title("Phantom After Padding and transformation")
    plt.imshow(imgWarped, cmap=cm.gray)
    plt.show()

    #return images to original size
    unpadImgPad = imgPad[padSize:allSize - padSize + 1,
                         padSize:allSize - padSize + 1]
    unpadImWraped = imgWarped[padSize:allSize - padSize + 1,
                              padSize:allSize - padSize + 1]

    fixed_angles_deg = np.arange(180)
    moving_angles_deg = np.arange(0, 180, 180 / num_angles)

    #apply radon trnasform with angles range
    fixed_sinogram = tf.radon(unpadImgPad, fixed_angles_deg, circle=True)
    moving_sinogram = tf.radon(unpadImWraped, moving_angles_deg, circle=True)

    #get the best radon parameters for transfromation
    theta, trans = radon_register(fixed_sinogram, fixed_angles_deg,
                                  moving_sinogram, moving_angles_deg)

    tform1 = tf.SimilarityTransform(scale=1,
                                    rotation=math.radians(theta),
                                    translation=trans)

    #apply the trans
    imgWarped1 = tf.warp(imgPad, tform1.inverse)

    #show results
    plt.figure(3)
    #plt.title("Fixed Brain After Radon tranformation")
    plt.title("Phantom After Radon tranformation")
    plt.imshow(imgWarped1, cmap=cm.gray)
    plt.show()

    #show diffirences
    plt.imshow(imgPad, cmap=cm.gray)
    plt.imshow(sobel(imgWarped1), alpha=0.5)
    #plt.title("Differences Between Original Brain and Radon Transform Brain")
    plt.title(
        "Differences Between Original Phantom and Radon Transform Phantom")
    plt.show()
Example #27
0
 def map_func1(coords):
     tform2 = transform.SimilarityTransform(scale=1 / 32,
                                            rotation=0,
                                            translation=(-1.0, -1.0))
     return tform.inverse(tform2(coords))
Example #28
0
def align_seq(prj,
              ang,
              fdir='.',
              iters=10,
              pad=(0, 0),
              blur=True,
              save=False,
              debug=True):
    """Aligns the projection image stack using the sequential
    re-projection algorithm :cite:`Gursoy:17`.

    Parameters
    ----------
    prj : ndarray
        3D stack of projection images. The first dimension
        is projection axis, second and third dimensions are
        the x- and y-axes of the projection image, respectively.
    ang : ndarray
        Projection angles in radians as an array.
    iters : scalar, optional
        Number of iterations of the algorithm.
    pad : list-like, optional
        Padding for projection images in x and y-axes.
    blur : bool, optional
        Blurs the edge of the image before registration.
    save : bool, optional
        Saves projections and corresponding reconstruction
        for each algorithm iteration.
    debug : book, optional
        Provides debugging info such as iterations and error.

    Returns
    -------
    ndarray
        3D stack of projection images with jitter.
    ndarray
        Error array for each iteration.
    """

    from skimage import transform as tf
    from skimage.feature import register_translation
    import tomopy
    import dxchange
    import numpy as np

    # Needs scaling for skimage float operations.
    prj, scl = scale(prj)

    # Shift arrays
    sx = np.zeros((prj.shape[0]))
    sy = np.zeros((prj.shape[0]))

    conv = np.zeros((iters))

    # Pad images.
    npad = ((0, 0), (pad[1], pad[1]), (pad[0], pad[0]))
    prj = np.pad(prj, npad, mode='constant', constant_values=0)
    #prj = np.pad(prj, npad, mode='edge')

    # Register each image frame-by-frame.
    for n in range(iters):
        # Reconstruct image.
        rec = tomopy.recon(prj, ang, algorithm='sirt')
        ##rec = tomopy.recon(prj, ang, algorithm='sirt', num_iter=2)
        #rec = tomopy.recon(prj, ang, algorithm='gridrec')

        # Re-project data and obtain simulated data.
        sim = tomopy.project(rec, ang, pad=False)

        # Blur edges.
        if blur:
            _prj = blur_edges(prj, 0.1, 0.5)
            _sim = blur_edges(sim, 0.1, 0.5)
        else:
            _prj = prj
            _sim = sim

        # Initialize error matrix per iteration.
        err = np.zeros((prj.shape[0]))

        # For each projection
        for m in range(prj.shape[0]):

            # Register current projection in sub-pixel precision
            shift, error, diffphase = register_translation(_prj[m], _sim[m], 2)
            err[m] = np.sqrt(shift[0] * shift[0] + shift[1] * shift[1])
            sx[m] += shift[0]
            sy[m] += shift[1]

            # Register current image with the simulated one
            tform = tf.SimilarityTransform(translation=(shift[1], shift[0]))
            prj[m] = tf.warp(prj[m], tform, order=5)
            ##prj[m] = tf.warp(prj[m], tform, order=0, mode='edge')

        if debug:
            print('iter=' + str(n) + ', err=' + str(np.linalg.norm(err)))
            conv[n] = np.linalg.norm(err)

        if save:
            dxchange.write_tiff(prj, fdir + '/tmp/iters/prj/prj')
            dxchange.write_tiff(sim, fdir + '/tmp/iters/sim/sim')
            dxchange.write_tiff(rec, fdir + '/tmp/iters/rec/rec')

    # Re-normalize data
    prj *= scl
    return prj, sx, sy, conv
Example #29
0
def align_joint(prj,
                ang,
                fdir='.',
                iters=10,
                pad=(0, 0),
                blur=True,
                center=None,
                algorithm='sirt',
                upsample_factor=10,
                rin=0.5,
                rout=0.8,
                save=False,
                debug=True):
    """
    Aligns the projection image stack using the joint
    re-projection algorithm :cite:`Gursoy:17`.

    Parameters
    ----------
    prj : ndarray
        3D stack of projection images. The first dimension
        is projection axis, second and third dimensions are
        the x- and y-axes of the projection image, respectively.
    ang : ndarray
        Projection angles in radians as an array.
    iters : scalar, optional
        Number of iterations of the algorithm.
    pad : list-like, optional
        Padding for projection images in x and y-axes.
    blur : bool, optional
        Blurs the edge of the image before registration.
    center: array, optional
        Location of rotation axis.
    algorithm : {str, function}
        One of the following string values.

        'art'
            Algebraic reconstruction technique :cite:`Kak:98`.
        'gridrec'
            Fourier grid reconstruction algorithm :cite:`Dowd:99`,
            :cite:`Rivers:06`.
        'mlem'
            Maximum-likelihood expectation maximization algorithm
            :cite:`Dempster:77`.
        'sirt'
            Simultaneous algebraic reconstruction technique.
        'tv'
            Total Variation reconstruction technique
            :cite:`Chambolle:11`.
        'grad'
            Gradient descent method with a constant step size

    upsample_factor : integer, optional
        The upsampling factor. Registration accuracy is
        inversely propotional to upsample_factor.
    rin : scalar, optional
        The inner radius of blur function. Pixels inside
        rin is set to one.
    rout : scalar, optional
        The outer radius of blur function. Pixels outside
        rout is set to zero.
    save : bool, optional
        Saves projections and corresponding reconstruction
        for each algorithm iteration.
    debug : book, optional
        Provides debugging info such as iterations and error.

    Returns
    -------
    ndarray
        3D stack of projection images with jitter.
    ndarray
        Error array for each iteration.
    """

    # Needs scaling for skimage float operations.
    prj, scl = scale(prj)

    # Shift arrays
    sx = np.zeros((prj.shape[0]))
    sy = np.zeros((prj.shape[0]))

    conv = np.zeros((iters))

    # Pad images.
    npad = ((0, 0), (pad[1], pad[1]), (pad[0], pad[0]))
    prj = np.pad(prj, npad, mode='constant', constant_values=0)

    # Initialization of reconstruction.
    rec = 1e-12 * np.ones((prj.shape[1], prj.shape[2], prj.shape[2]))

    extra_kwargs = {}
    if algorithm != 'gridrec':
        extra_kwargs['num_iter'] = 1

    # Register each image frame-by-frame.
    for n in range(iters):

        if np.mod(n, 1) == 0:
            _rec = rec

        # Reconstruct image.
        rec = recon(prj,
                    ang,
                    center=center,
                    algorithm=algorithm,
                    init_recon=_rec,
                    **extra_kwargs)

        # Re-project data and obtain simulated data.
        sim = project(rec, ang, center=center, pad=False)

        # Blur edges.
        if blur:
            _prj = blur_edges(prj, rin, rout)
            _sim = blur_edges(sim, rin, rout)
        else:
            _prj = prj
            _sim = sim

        # Initialize error matrix per iteration.
        err = np.zeros((prj.shape[0]))

        # For each projection
        for m in range(prj.shape[0]):

            # Register current projection in sub-pixel precision
            shift, error, diffphase = phase_cross_correlation(
                _prj[m], _sim[m], upsample_factor)
            err[m] = np.sqrt(shift[0] * shift[0] + shift[1] * shift[1])
            sx[m] += shift[0]
            sy[m] += shift[1]

            # Register current image with the simulated one
            tform = tf.SimilarityTransform(translation=(shift[1], shift[0]))
            prj[m] = tf.warp(prj[m], tform, order=5)

        if debug:
            print('iter=' + str(n) + ', err=' + str(np.linalg.norm(err)))
            conv[n] = np.linalg.norm(err)

        if save:
            write_tiff(prj, 'tmp/iters/prj', n)
            write_tiff(sim, 'tmp/iters/sim', n)
            write_tiff(rec, 'tmp/iters/rec', n)

    # Re-normalize data
    prj *= scl
    return prj, sx, sy, conv
Example #30
0
    plt.close()


# Re use previous coordinates, if found
if not os.path.exists('_reg_coords.npz'):
    choose_corresponding_points()

coords = np.load('_reg_coords.npz')

# Estimate the transformation between the two sets of coordinates,
# assuming it is an affine transform
tf = transform.estimate_transform('similarity', coords['source'], coords['target'])

# Use a translation transformation to center both images for display purposes
offset = transform.SimilarityTransform(translation=(-200, -170))

img0_warped = transform.warp(img0, inverse_map=offset,
                             output_shape=(600, 600))

img1_warped = transform.warp(img1, inverse_map=offset + tf,
                             output_shape=(600, 600))


# Find where both images overlap; in that region average their values
mask = (img0_warped != 0) & (img1_warped != 0)
registered = img0_warped + img1_warped
registered[mask] /= 2

# Display the results
f, (ax0, ax1, ax2) = plt.subplots(1, 3, subplot_kw={'xticks': [], 'yticks': []})