Beispiel #1
0
def getFilterMatrix(imgList, const):
    xlim,ylim = const.imgSize
    shortToLongM = np.zeros((xlim*ylim,len(imgList)))
    longToShortM = np.zeros((len(imgList),xlim*ylim))

    for k in range(len(imgList)):
        i, j, _, _ = imgList[k]
        shortToLongM[i*ylim+j][k] = 1
        longToShortM[k][i*ylim+j] = 1

    return commons.sMatrix(shortToLongM), commons.sMatrix(longToShortM)
Beispiel #2
0
def getPSFMatrix(psfFileName,imgList,const):
    psfMatrix, _= commons.readFitsImage(psfFileName)
    normalizedPSF = psfMatrix/np.sum(psfMatrix)
    M, N  = const.imgSize
    fullB = np.zeros((M*N, M*N))
    xlim, ylim = normalizedPSF.shape

    #B = np.zeros((const.length, const.length))
    for u in range(xlim):
        for v in range(ylim):
            for h in range(M*N):
                if h+u>=0 and h+u<=M-1 and h+v>=0 and h+v<=N-1:
                    g=(h+u)+(h+v-1)*(M-1)
                    fullB[g][h]=normalizedPSF[u][v]
    fullB = commons.sMatrix(fullB)

    shortTolongM, longToShortM = commons.getFilterMatrix(imgList, const)
    B = longToShortM*fullB*shortTolongM
    #print longToShortM.todense()
    #print scipy.linalg.norm((longToShortM*fullB*shortTolongM).todense())
    #print shortTolongM.todense()
    return B
Beispiel #3
0
def getLensOperator(mappingDict, srcBrightNess):
    dim = len(mappingDict)
    L = np.zeros((dim, 2*dim))
    imgPointList = mappingDict.keys()
    srcPointList = mappingDict.values()
    srcPosition = []
    firstOrderWeightList = []
    secondOrderWeightList = []
    Hsy1 = np.zeros((dim, dim))
    Hsy2 = np.zeros((dim, dim))
    d =  np.zeros(dim)

    normV = [[] for i in range(dim)]

    #srcBrightNess = np.ones(dim)
    varList = []

    RTR = np.zeros((2*dim, 2*dim))

    for i in range(dim):
        imgX, imgY = imgPointList[i]
        srcX, srcY, imgBrightNess, type, var = srcPointList[i]
        srcPosition.append((srcX, srcY))
        varList.append(var)
        d[i] = imgBrightNess

        if type=='o':
            if (imgX, imgY-1) in imgPointList and (imgX, imgY+1) in imgPointList and (imgX+1, imgY) in imgPointList:
                Hsy1[i][i]=1
                Hsy2[i][i]=1
                w1Index = imgPointList.index((imgX, imgY-1))
                w2Index = imgPointList.index((imgX, imgY+1))
                w3Index = imgPointList.index((imgX+1, imgY))
                A = (mappingDict[(imgX,imgY-1)][0], mappingDict[(imgX,imgY-1)][1])
                B = (mappingDict[(imgX,imgY+1)][0], mappingDict[(imgX,imgY+1)][1])
                C = (mappingDict[(imgX+1,imgY)][0], mappingDict[(imgX+1,imgY)][1])

                L[i][w1Index], L[i][w2Index],L[i][w3Index] = commons.getTriWeight(A,B,C, (srcX, srcY))
            # update the normV
                An = (A[0], A[1], srcBrightNess[w1Index])
                Bn = (B[0], B[1], srcBrightNess[w2Index])
                Cn = (C[0], C[1], srcBrightNess[w3Index])

                n0, n1, n2 = commons.getNormVectors(An, Bn, Cn)
                normV[w1Index].append((n0, n1, n2))
                normV[w2Index].append((n0, n1, n2))
                normV[w3Index].append((n0, n1, n2))
                firstOrderWeightList.append((w1Index, w2Index, w3Index, L[i][w1Index], L[i][w2Index],L[i][w3Index]))
            else:
                L[i][i] =1
                firstOrderWeightList.append((i, i, i , 1/3.,1/3.,1/3.))


        if type=='v':
            L[i][i] =1
            firstOrderWeightList.append((i, i, i , 1/3.,1/3.,1/3.))
            if (imgX-1, imgY-1) in imgPointList and (imgX-1, imgY+1) in imgPointList and (imgX+1, imgY-1) in imgPointList and (imgX+1, imgY+1) in imgPointList:
                wAIndex = imgPointList.index((imgX-1, imgY-1))
                wBIndex = imgPointList.index((imgX-1, imgY+1))
                wCIndex = imgPointList.index((imgX+0, imgY+0))
                wDIndex = imgPointList.index((imgX+1, imgY-1))
                wEIndex = imgPointList.index((imgX+1, imgY+1))


                A = (mappingDict[(imgX-1,imgY-1)][0], mappingDict[(imgX-1,imgY-1)][1])
                B = (mappingDict[(imgX-1,imgY+1)][0], mappingDict[(imgX-1,imgY+1)][1])
                C = (mappingDict[(imgX+0,imgY+0)][0], mappingDict[(imgX+0,imgY+0)][1])
                D = (mappingDict[(imgX+1,imgY-1)][0], mappingDict[(imgX+1,imgY-1)][1])
                E = (mappingDict[(imgX+1,imgY+1)][0], mappingDict[(imgX+1,imgY+1)][1])
                Hsy1[i][wAIndex], Hsy1[i][wBIndex], Hsy1[i][wCIndex], Hsy1[i][wDIndex],Hsy1[i][wEIndex],\
                Hsy2[i][wAIndex], Hsy2[i][wBIndex], Hsy2[i][wCIndex], Hsy2[i][wDIndex],Hsy2[i][wEIndex] = commons.getPentWeigth(A, B, C, D, E)


    HsTHs = scipy.sparse.coo_matrix(Hsy1.transpose())*scipy.sparse.coo_matrix(Hsy1) + \
                    scipy.sparse.coo_matrix(Hsy2.transpose())*scipy.sparse.coo_matrix(Hsy2)

    HsTHs = HsTHs.toarray()
    #Hs = np.linalg.cholesky(tt)


    for i in range(dim):
        for j in range(dim):
            RTR[i][j] = HsTHs[i][j]


        # Diagonal covariance matrix:
    C = commons.listToDiagonalMatrix(varList)
    normV = commons.getMeanNorm(normV)

    return srcPosition, srcPointList, commons.sMatrix(L), normV, C, firstOrderWeightList, d, RTR, HsTHs, imgPointList