Exemplo n.º 1
0
    def __lp__(self,x,y,radius,l,a,w,retObj=True):
        ang=a*num.pi/180.

        (A,B)=self.data.shape

        a=max(0,int(y-1)-w)
        b=min(A,int(y-1)+w+1)
        c=max(0,int(x-1)-w)
        d=min(B,int(x-1)+w+1)
        data=self.data[a:b,c:d]


        #repData=num.repeat(num.repeat(data,self.repFact,axis=0),self.repFact,axis=1)/(self.repFact*self.repFact)
        repData=expand2d(data,self.repFact)
        (A,B)=repData.shape

         if ((x<w) and (y<w)):
           cx=num.array([x*self.repFact, y*self.repFact])
         elif (x<w):
           cx=num.array([x*self.repFact, (y-int(y)+w)*self.repFact])
         elif (y<w):
           cx=num.array([(x-int(x)+w)*self.repFact, y*self.repFact])
         else: 
           cx=num.array([(x-int(x)+w)*self.repFact, (y-int(y)+w)*self.repFact])
Exemplo n.º 2
0
    def __lp__(self, x, y, radius, l, a, w, retObj=True):
        ang = a * num.pi / 180.0

        (A, B) = self.data.shape

        a = max(0, int(y - 1) - w)
        b = min(A, int(y - 1) + w + 1)
        c = max(0, int(x - 1) - w)
        d = min(B, int(x - 1) + w + 1)
        data = self.data[a:b, c:d]

        # repData=num.repeat(num.repeat(data,self.repFact,axis=0),self.repFact,axis=1)/(self.repFact*self.repFact)
        repData = expand2d(data, self.repFact)
        (A, B) = repData.shape

        if (x < w) and (y < w):
            cx = num.array([x * self.repFact, y * self.repFact])
        elif x < w:
            cx = num.array([x * self.repFact, (y - int(y) + w) * self.repFact])
        elif y < w:
            cx = num.array([(x - int(x) + w) * self.repFact, y * self.repFact])
        else:
            cx = num.array([(x - int(x) + w) * self.repFact, (y - int(y) + w) * self.repFact])
        h = self.repFact * (radius ** 2 + (l / 2.0) ** 2) ** 0.5
        beta = num.arctan2(num.array(radius), num.array(l / 2.0))

        x0 = cx + num.array([num.cos(beta + ang), num.sin(beta + ang)]) * h
        x1 = cx + num.array([num.cos(ang - beta + num.pi), num.sin(ang - beta + num.pi)]) * h
        x2 = cx + num.array([num.cos(ang + beta + num.pi), num.sin(beta + ang + num.pi)]) * h
        x3 = cx + num.array([num.cos(ang - beta), num.sin(ang - beta)]) * h

        map = num.zeros((A, B)).astype("float")
        # draw the box
        if abs(ang) % num.pi in [0, num.pi / 2, num.pi, -num.pi / 2, -num.pi]:
            corners = num.concatenate([[x0], [x1], [x2], [x3]])
            map[num.min(corners[:, 1]) : num.max(corners[:, 1]), num.min(corners[:, 0]) : num.max(corners[:, 0])] = 1.0
        else:
            l0 = line(x0, x1)
            l1 = line(x1, x2)
            l2 = line(x2, x3)
            l3 = line(x3, x0)
            self.l0 = l0
            self.l1 = l1
            self.l2 = l2
            self.l3 = l3

            perimeter = []
            for ii in num.arange(l0.xlim[0], l0.xlim[1]):
                perimeter.append([ii + 0.5, l0(ii)])
            for ii in num.arange(l1.xlim[0], l1.xlim[1]):
                perimeter.append([ii + 0.5, l1(ii)])
            for ii in num.arange(l2.xlim[0], l2.xlim[1]):
                perimeter.append([ii + 0.5, l2(ii)])
            for ii in num.arange(l3.xlim[0], l3.xlim[1]):
                perimeter.append([ii + 0.5, l3(ii)])

            perimeter = num.array(perimeter).astype("int")
            ux = num.unique(perimeter[:, 0])
            for ii in range(len(ux)):
                if (ux[ii] >= len(map[0, :])) or (ux[ii] < 0):
                    continue
                ww = num.where(perimeter[:, 0] == ux[ii])
                y = perimeter[ww][:, 1]
                map[num.max([0, num.min(y)]) : num.max([0, num.max(y) + 1]), ux[ii]] = 1.0
                # ADDIN double check for pixels beyond 1 of the lines.

        p0 = cx + self.repFact * (l / 2.0) * num.array([num.cos(ang), num.sin(ang)])
        p1 = cx + self.repFact * (l / 2.0) * num.array([num.cos(ang + num.pi), num.sin(ang + num.pi)])

        xeval = num.linspace(
            max(0.0, p0[0] - radius * self.repFact), min(p0[0] + radius * self.repFact, B - 1), radius * 100 * 2
        )
        for ii in range(len(xeval)):
            val = (radius * self.repFact) ** 2 - (xeval[ii] - p0[0]) ** 2
            if val < 0:
                continue
            y = val ** 0.5
            y0 = -y + p0[1] + 0.5
            y1 = y + p0[1] + 0.5
            if (y0 < 0) and (y1 < 0):
                continue
            if y0 < 0:
                y0 = 0
            if int(y0) == int(y1):
                y1 += 1
            map[y0:y1, xeval[ii] + 0.5] = 1.0

        xeval = num.linspace(
            max(0.0, p1[0] - radius * self.repFact), min(p1[0] + radius * self.repFact, B - 1), radius * 100 * 2
        )
        for ii in range(len(xeval)):
            val = (radius * self.repFact) ** 2 - (xeval[ii] - p1[0]) ** 2
            if val < 0:
                continue
            y = val ** 0.5
            y0 = -y + p1[1] + 0.5
            y1 = y + p1[1] + 0.5
            if (y0 < 0) and (y1 < 0):
                continue
            if y0 < 0:
                y0 = 0
            if int(y0) == int(y1):
                y1 += 1
            map[y0:y1, xeval[ii] + 0.5] = 1.0

        # print p0,p1,radius*self.repFact
        # print x0,x1,x2,x3

        self.mask = map * 1.0
        if retObj:
            return map * repData
        omap = num.equal(map, 0.0)
        self.bgmask = omap * 1.0
        # pyl.imshow(omap*repData)
        # pyl.show()
        return omap * repData

        pyl.scatter(x0[0], x0[1], marker="^")
        pyl.scatter(x1[0], x1[1], marker="s")
        pyl.scatter(x2[0], x2[1])
        pyl.scatter(x3[0], x3[1])
        pyl.scatter(cx[0], cx[1])
        pyl.show()
        sys.exit()
Exemplo n.º 3
0
    def __lp__(self, x, y, radius, l, a, w, retObj=True):
        #Single-letter variables = super painful debugging
        #I'll leave them in the function call for backwards compatibility,
        #but will remove below.
        angle = a
        length = l
        del (a, l)
        ang = num.radians(angle)

        (A, B) = self.data.shape

        a = max(0, int(y - 1) - w)
        b = min(A, int(y - 1) + w + 1)
        c = max(0, int(x - 1) - w)
        d = min(B, int(x - 1) + w + 1)
        data = self.data[a:b, c:d]

        #repData=num.repeat(num.repeat(data,self.repFact,axis=0),self.repFact,axis=1)/(self.repFact*self.repFact)
        repData = expand2d(data, self.repFact)
        (A, B) = repData.shape

        if ((x < w) and (y < w)):
            cx = num.array([(x - 1) * self.repFact, (y - 1) * self.repFact])
        elif (x < w):
            cx = num.array([(x - 1) * self.repFact,
                            (y - int(y) + w) * self.repFact])
        elif (y < w):
            cx = num.array([(x - int(x) + w) * self.repFact,
                            (y - 1) * self.repFact])
        else:
            cx = num.array([(x - int(x) + w) * self.repFact,
                            (y - int(y) + w) * self.repFact])
        h = self.repFact * (radius**2 + (length / 2.)**2)**0.5
        beta = num.arctan2(num.array(radius), num.array(length / 2.))

        x0 = cx + num.array([num.cos(beta + ang), num.sin(beta + ang)]) * h
        x1 = cx + num.array(
            [num.cos(ang - beta + num.pi),
             num.sin(ang - beta + num.pi)]) * h
        x2 = cx + num.array(
            [num.cos(ang + beta + num.pi),
             num.sin(beta + ang + num.pi)]) * h
        x3 = cx + num.array([num.cos(ang - beta), num.sin(ang - beta)]) * h

        map = num.zeros((A, B)).astype('float')
        #draw the box
        l0 = line(x0, x1)
        l1 = line(x1, x2)
        l2 = line(x2, x3)
        l3 = line(x3, x0)
        self.l0 = l0
        self.l1 = l1
        self.l2 = l2
        self.l3 = l3
        if abs(ang) % num.pi in [0, num.pi / 2, num.pi, -num.pi / 2, -num.pi]:
            corners = num.concatenate([[x0], [x1], [x2], [x3]])
            map[num.min(corners[:, 1]).astype('int'):num.max(corners[:, 1]
                                                             ).astype('int'),
                num.min(corners[:, 0]).astype('int'):num.max(corners[:, 0]).
                astype('int')] = 1.
        else:
            perimeter = []
            for ii in num.arange(l0.xlim[0], l0.xlim[1]):
                perimeter.append([ii + 0.5, l0(ii)])
            for ii in num.arange(l1.xlim[0], l1.xlim[1]):
                perimeter.append([ii + 0.5, l1(ii)])
            for ii in num.arange(l2.xlim[0], l2.xlim[1]):
                perimeter.append([ii + 0.5, l2(ii)])
            for ii in num.arange(l3.xlim[0], l3.xlim[1]):
                perimeter.append([ii + 0.5, l3(ii)])

            perimeter = num.array(perimeter).astype('int')
            ux = num.unique(perimeter[:, 0])
            for ii in range(len(ux)):
                if (ux[ii] >= len(map[0, :])) or (ux[ii] < 0): continue
                ww = num.where(perimeter[:, 0] == ux[ii])
                y = perimeter[ww][:, 1]
                map[num.max([0, num.min(y)]):num.max([0, num.max(y) + 1]),
                    ux[ii]] = 1.
                #ADDIN double check for pixels beyond 1 of the lines.

        p0 = (cx + self.repFact * (length / 2.) *
              num.array([num.cos(ang), num.sin(ang)]))
        p1 = (cx + self.repFact * (length / 2.) *
              num.array([num.cos(ang + num.pi),
                         num.sin(ang + num.pi)]))

        xeval = num.linspace(max(0., p0[0] - radius * self.repFact),
                             min(p0[0] + radius * self.repFact, B - 1),
                             radius * 100 * 2)
        for ii in range(len(xeval)):
            val = (radius * self.repFact)**2 - (xeval[ii] - p0[0])**2
            if val < 0: continue
            y = val**0.5
            y0 = -y + p0[1] + 0.5
            y1 = y + p0[1] + 0.5
            if (y0 < 0) and (y1 < 0): continue
            if (y0 < 0): y0 = 0
            if int(y0) == int(y1): y1 += 1
            map[int(y0):int(y1), int(xeval[ii] + 0.5)] = 1.

        xeval = num.linspace(max(0., p1[0] - radius * self.repFact),
                             min(p1[0] + radius * self.repFact, B - 1),
                             radius * 100 * 2)
        for ii in range(len(xeval)):
            val = (radius * self.repFact)**2 - (xeval[ii] - p1[0])**2
            if val < 0: continue
            y = val**0.5
            y0 = -y + p1[1] + 0.5
            y1 = y + p1[1] + 0.5
            if (y0 < 0) and (y1 < 0): continue
            if (y0 < 0): y0 = 0
            if int(y0) == int(y1): y1 += 1
            map[int(y0):int(y1), int(xeval[ii] + 0.5)] = 1.

        #print p0,p1,radius*self.repFact
        #print x0,x1,x2,x3

        self.mask = map * 1.
        if retObj:
            return map * repData
        omap = num.equal(map, 0.0)
        self.bgmask = omap * 1.
        #pyl.imshow(omap*repData)
        #pyl.show()
        return omap * repData

        pyl.scatter(x0[0], x0[1], marker='^')
        pyl.scatter(x1[0], x1[1], marker='s')
        pyl.scatter(x2[0], x2[1])
        pyl.scatter(x3[0], x3[1])
        pyl.scatter(cx[0], cx[1])
        pyl.show()
        sys.exit()