コード例 #1
0
def get_subwindow(im, pos, sz, cos_window):
    """
    使用 replication padding 从图像中获得子窗口。子窗口以 [y, x] 为坐标中心,大小为 [height, width].
    如果子窗口超过图像边界,则复制图像的边界像素值。获得的子窗口将使用余弦窗口标准化到 [-0.5, 0.5]
    :param im: 输入图像
    :param pos: 子窗口中心点坐标 [y, x]
    :param sz: 子窗口大小 [height, width]
    :param cos_window: 余弦子窗口矩阵
    :return: 返回经过余弦子窗口截取的图像矩形框部分
    """
    # 如果不是高、宽组成的数组,而是一个一维数值,则转化为一个数组
    # 目标是子窗矩形化
    if pylab.isscalar(sz):  # square sub-window
        sz = [sz, sz]
    # 以 pos 为中心,以 sz 为窗口大小建立子窗
    ys = pylab.floor(pos[0]) + pylab.arange(sz[0], dtype=int) - pylab.floor(
        sz[0] / 2)
    xs = pylab.floor(pos[1]) + pylab.arange(sz[1], dtype=int) - pylab.floor(
        sz[1] / 2)
    ys = ys.astype(int)
    xs = xs.astype(int)
    # 如果子窗超过坐标,则设置为边界值
    ys[ys < 0] = 0
    ys[ys >= im.shape[0]] = im.shape[0] - 1
    xs[xs < 0] = 0
    xs[xs >= im.shape[1]] = im.shape[1] - 1
    # 提取子窗剪切的图像块
    out = im[pylab.ix_(ys, xs)]
    # 将图像像素值从 [0, 1] 平移到 [-0.5, 0.5]
    out = out.astype(pylab.float64) - 0.5
    # 余弦窗口化,论文公式 (18)

    return pylab.multiply(cos_window, out)
コード例 #2
0
    def Global_Stiffness(self):
        '''
        Generates Global Stiffness Matrix for the plane structure
        '''
        elem = self.element;
        B = py.zeros((6,6))
        for i in range (0,py.size(elem,0)): 
            #for each element find the stifness matrix
            K = py.zeros((self.n_nodes*2,self.n_nodes*2))            
            el = elem[i]
            
            #nodes formatted for input            
            [node1, node2, node3] = el;
            node1x = 2*(node1-1);node2x = 2*(node2-1);node3x = 2*(node3-1);
            node1y = 2*(node1-1)+1;node2y = 2*(node2-1)+1;node3y = 2*(node3-1)+1;
            
            #Area, Strain Matrix and E Matrix multiplied to get element stiffness            
            [J,B] = self.B(el)
            local_k =0.5*abs(J)*py.dot(py.transpose(B),py.dot(self.E_matrix,B))
            if self.debug:            
                print 'K for elem', el, '\n', local_k
                
            #Element K-Matrix converted into Global K-Matrix format 
            K[py.ix_([node1x,node1y,node2x,node2y,node3x,node3y],[node1x,node1y,node2x,node2y,node3x,node3y])] = K[py.ix_([node1x,node1y,node2x,node2y,node3x,node3y],[node1x,node1y,node2x,node2y,node3x,node3y])]+local_k

            #Adding contibution into Global Stiffness           
            self.k_global = self.k_global + K
            
        if self.debug:            
                print 'Global Stiffness','\n', self.k_global        
コード例 #3
0
    def get_subwindow(self, im, pos, sz):
        """
        Obtain sub-window from image, with replication-padding.
        Returns sub-window of image IM centered at POS ([y, x] coordinates),
        with size SZ ([height, width]). If any pixels are outside of the image,
        they will replicate the values at the borders.

        The subwindow is also normalized to range -0.5 .. 0.5, and the given
        cosine window COS_WINDOW is applied
        (though this part could be omitted to make the function more general).
        """

        if pylab.isscalar(sz):  # square sub-window
            sz = [sz, sz]

        ys = pylab.floor(pos[0]) \
            + pylab.arange(sz[0], dtype=int) - pylab.floor(sz[0]/2)
        xs = pylab.floor(pos[1]) \
            + pylab.arange(sz[1], dtype=int) - pylab.floor(sz[1]/2)

        ys = ys.astype(int)
        xs = xs.astype(int)

        # check for out-of-bounds coordinates,
        # and set them to the values at the borders
        ys[ys < 0] = 0
        ys[ys >= im.shape[0]] = im.shape[0] - 1

        xs[xs < 0] = 0
        xs[xs >= im.shape[1]] = im.shape[1] - 1
        #zs = range(im.shape[2])

        # extract image
        #out = im[pylab.ix_(ys, xs, zs)]
        out = im[pylab.ix_(ys, xs)]

        out = cv2.resize(out, dsize = (int(self.window_sz[1]), int(self.window_sz[0])), fx=0, fy=0)
        #cos_window = pylab.outer(pylab.hanning(sz[0]), pylab.hanning(sz[1]))

        if debug:
            print("Out max/min value==", out.max(), "/", out.min())
            pylab.figure()
            pylab.imshow(out, cmap=pylab.cm.gray)
            pylab.title("cropped subwindow")

        #pre-process window --
        # normalize to range -0.5 .. 0.5
        # pixels are already in range 0 to 1
        out = out.astype(pylab.float64) - 0.5

        # apply cosine window
        #out = pylab.multiply(cos_window, out)
        out = pylab.multiply(self.cos_window, out)

        return out
def get_subwindow(im, pos, sz, cos_window):
    """
    Obtain sub-window from image, with replication-padding.
    Returns sub-window of image IM centered at POS ([y, x] coordinates),
    with size SZ ([height, width]). If any pixels are outside of the image,
    they will replicate the values at the borders.

    The subwindow is also normalized to range -0.5 .. 0.5, and the given
    cosine window COS_WINDOW is applied
    (though this part could be omitted to make the function more general).
    """

    if pylab.isscalar(sz):  # square sub-window
        sz = [sz, sz]

    ys = pylab.floor(pos[0]) \
        + pylab.arange(sz[0], dtype=int) - pylab.floor(sz[0]/2)
    xs = pylab.floor(pos[1]) \
        + pylab.arange(sz[1], dtype=int) - pylab.floor(sz[1]/2)

    ys = ys.astype(int)
    xs = xs.astype(int)

    # check for out-of-bounds coordinates,
    # and set them to the values at the borders
    ys[ys < 0] = 0
    ys[ys >= im.shape[0]] = im.shape[0] - 1

    xs[xs < 0] = 0
    xs[xs >= im.shape[1]] = im.shape[1] - 1
    #zs = range(im.shape[2])

    # extract image
    #out = im[pylab.ix_(ys, xs, zs)]
    out = im[pylab.ix_(ys, xs)]

    if debug:
        print("Out max/min value==", out.max(), "/", out.min())
        pylab.figure()
        pylab.imshow(out, cmap=pylab.cm.gray)
        pylab.title("cropped subwindow")

    #pre-process window --
    # normalize to range -0.5 .. 0.5
    # pixels are already in range 0 to 1
    out = out.astype(pylab.float64) / 255 - 0.5

    # apply cosine window
    out = pylab.multiply(cos_window, out)

    return out
コード例 #5
0
def get_subwindow(image, box):
    xs = pylab.floor(box[0]) \
         + pylab.arange(box[4], dtype=int) - pylab.floor(box[4] / 2)
    ys = pylab.floor(box[1]) \
         + pylab.arange(box[5], dtype=int) - pylab.floor(box[5] / 2)

    xs = xs.astype(int)
    ys = ys.astype(int)

    xs[xs < 0] = 0
    xs[xs >= image.shape[1]] = image.shape[1] - 1

    ys[ys < 0] = 0
    ys[ys >= image.shape[0]] = image.shape[0] - 1

    return image[pylab.ix_(ys, xs, range(image.shape[2]))]
コード例 #6
0
    def get_imageROI(self, im, rect):

        ys = pylab.floor(rect[1])  + pylab.arange(rect[3], dtype=int)
        xs = pylab.floor(rect[0])  + pylab.arange(rect[2], dtype=int)

        ys = ys.astype(int)
        xs = xs.astype(int)

        # check for out-of-bounds coordinates,
        # and set them to the values at the borders
        ys[ys < 0] = 0
        ys[ys >= im.shape[0]] = im.shape[0] - 1

        xs[xs < 0] = 0
        xs[xs >= im.shape[1]] = im.shape[1] - 1
        roi = im[pylab.ix_(ys, xs)]
        return roi
コード例 #7
0
 def StiffnessAndStrain(self): 
     '''
     Function to obtain stiffness matrix and strains in each element
     '''
     for el in range(self.n_element): #enumerate through all the elements
         K = py.zeros((self.n_nodes*2,self.n_nodes*2))#Full stiffness matrix for one element
         if self._debug:            
             print 'bar element:', el+1
         startNode = self._connectivity[el,0]-1 #start and end node of the element
         endNode = self._connectivity[el,1]-1
         startXY = self._xy[startNode]        #start and end coordinates of the element
         endXY = self._xy[endNode]
         L = py.norm(endXY - startXY)    # Length of the element
         self.L[el]= L
         #new start and end coordinates of the element
         newstartXY=self._xy[startNode]+self._displacement[startNode] 
         newendXY=self._xy[endNode]+self._displacement[endNode] 
         newL=py.norm(newendXY - newstartXY) # Deformed Length of the element
         self.strain_global.append((newL-L)/L);     # Axial Strain in element
         T = py.zeros((4,2))
         T = self.Transform2D(startXY,endXY) #get the Transformation matrix for the element
         if self._debug: 
             print startNode, endNode, startXY, endXY
             print 'T'
             print T
         L_inv = 1.0/L
         k0=py.array([[L_inv, -L_inv],[-L_inv, L_inv]])#Stiffness matrix in 1D
         k_element = py.dot(T,py.dot(k0,py.transpose(T)) )#Stiffness matrix in 2D
         #Convert from 2x2 local K matrix to a full stiffness matrix but for single element 
         K[py.ix_([2*startNode,2*startNode+1,2*endNode,2*endNode+1],[2*startNode,2*startNode+1,2*endNode,2*endNode+1])] = K[py.ix_([2*startNode,2*startNode+1,2*endNode,2*endNode+1],[2*startNode,2*startNode+1,2*endNode,2*endNode+1])]+k_element 
         if self._debug:             
             print 'element stiffness', 'L_inv', L_inv
             print k_element
             print 'K'
             print K
         self.k_global = self.k_global + K  # Assemble all the stiffness into one Global Stiffness        
                 
     return self.k_global,self.strain_global