def reconstruct(Iorig, I, Y, out_size, threshold=.9):

    net_stride = 2**4
    side = ((208. + 40.) / 2.) / net_stride  # 7.75

    Probs = Y[..., 0]
    Affines = Y[..., 2:]
    rx, ry = Y.shape[:2]
    ywh = Y.shape[1::-1]
    iwh = np.array(I.shape[1::-1], dtype=float).reshape((2, 1))

    xx, yy = np.where(Probs > threshold)

    WH = getWH(I.shape)
    MN = WH / net_stride

    vxx = vyy = 0.5  #alpha

    base = lambda vx, vy: np.matrix([[-vx, -vy, 1.], [vx, -vy, 1.],
                                     [vx, vy, 1.], [-vx, vy, 1.]]).T
    labels = []

    for i in range(len(xx)):
        y, x = xx[i], yy[i]
        affine = Affines[y, x]
        prob = Probs[y, x]

        mn = np.array([float(x) + .5, float(y) + .5])

        A = np.reshape(affine, (2, 3))
        A[0, 0] = max(A[0, 0], 0.)
        A[1, 1] = max(A[1, 1], 0.)

        pts = np.array(A * base(vxx, vyy))  #*alpha
        pts_MN_center_mn = pts * side
        pts_MN = pts_MN_center_mn + mn.reshape((2, 1))

        pts_prop = pts_MN / MN.reshape((2, 1))

        labels.append(DLabel(0, pts_prop, prob))

    final_labels = nms(labels, .1)
    TLps = []

    if len(final_labels):
        final_labels.sort(key=lambda x: x.prob(), reverse=True)
        for i, label in enumerate(final_labels):

            t_ptsh = getRectPts(0, 0, out_size[0], out_size[1])
            ptsh = np.concatenate((label.pts * getWH(Iorig.shape).reshape(
                (2, 1)), np.ones((1, 4))))
            H = find_T_matrix(ptsh, t_ptsh)
            Ilp = cv2.warpPerspective(Iorig, H, out_size, borderValue=.0)

            TLps.append(Ilp)

    return final_labels, TLps
 def reset_view(self):
     self.cx, self.cy = .5, .5
     wh = np.array([self.width, self.height], dtype=float)
     self.zoom_factor = (wh / getWH(self.Iorig.shape)).min()
     self.mouse_center = np.array([.5, .5])
     self.angles = np.array([0., 0., 0.])
     self._setPerspective()
 def __pts2xys(self, pts):
     N = pts.shape[1]
     pts = pts * getWH(self.Iorig.shape).reshape((2, 1))
     pts = np.concatenate((pts, np.ones((1, N))))
     pts = np.squeeze(np.array(np.matmul(self.R, pts)))
     pts = pts[:2] / pts[2]
     return pts
 def __pt2xy(self, pt):
     pt = np.squeeze(
         np.array(
             np.matmul(self.R, np.append(pt * getWH(self.Iorig.shape),
                                         1.))))
     pt = pt[:2] / pt[2]
     return tuple(pt.astype(int).tolist())
def augment_sample(I,pts, dim_w, dim_h, cls):

	maxsum,maxangle = 50,np.array([20.,20.,25.])
	angles = np.random.rand(3)*maxangle
	if angles.sum() > maxsum:
		angles = (angles/angles.sum())*(maxangle/maxangle.sum())

	I = im2single(I)
	iwh = getWH(I.shape)
	# codigo original
	# whratio = random.uniform(2., 4.)
	# wsiz = random.uniform(dim * .2, dim * 1.)

	# carro
	if cls=='0':
		whratio = random.uniform(2.75, 3.25)
		wsiz = random.uniform(dim_w * .2, dim_w * 0.7)
	else:
		whratio = random.uniform(0.7, 1.2)
		wsiz = random.uniform(dim_w * .2, dim_w * 0.7)
	# whratio = 1.
	# wsiz = random.uniform(dim_w*.2, dim_w*1.)
	# moto
	# whratio = random.uniform(0.7, 1.2)
	# wsiz = random.uniform(dim_w * .3, dim_w * 0.7)
	
	hsiz = wsiz/whratio

	dx = random.uniform(0.,dim_w - wsiz)
	dy = random.uniform(0.,dim_h - hsiz)

	pph = getRectPts(dx,dy,dx+wsiz,dy+hsiz)
	pts = pts*iwh.reshape((2,1))
	T = find_T_matrix(pts2ptsh(pts),pph)

	H = perspective_transform((dim_w,dim_h),angles=angles)
	H = np.matmul(H,T)

	Iroi,pts = project(I,H,pts,dim_w, dim_h)
	
	# hsv_mod = np.random.rand(3).astype('float32')
	# hsv_mod = (hsv_mod - .5)*.3
	# hsv_mod[0] *= 360
	# Iroi = hsv_transform(Iroi,hsv_mod)
	Iroi = np.clip(Iroi,0.,1.)

	pts = np.array(pts)

	# if random.random() > .5:
	# 	Iroi,pts = flip_image_and_pts(Iroi,pts)

	tl,br = pts.min(1),pts.max(1)
	llp = Label(0,tl,br)

	return Iroi,llp,pts
示例#6
0
	def updatePerspectiveMatrix(self):
		zf  = self.zoom_factor
		w,h = getWH(self.Iorig.shape)

		self.dx = self.cx*w*zf - self.width/2.
		self.dy = self.cy*h*zf - self.height/2.

		R = np.eye(3)
		R = np.matmul(R,np.matrix([[zf,0,-self.dx],[0,zf,-self.dy],[0,0,1]],dtype=float))
		R = np.matmul(R,perspective_transform((w,h),angles=self.angles))

		self.R = R
		self.Rinv = np.linalg.inv(R)
    def rectifyToPts(self, pts):

        if pts.shape[1] != 4:
            return

        ptsh = pts * getWH(self.Iorig.shape).reshape((2, 1))
        ptsh = np.concatenate((ptsh, np.ones((1, 4))))

        to_pts = self.__pts2xys(pts)
        wi, hi = (to_pts.min(1)[:2]).tolist()
        wf, hf = (to_pts.max(1)[:2]).tolist()
        to_pts = np.matrix([[wi, wf, wf, wi], [hi, hi, hf, hf], [1, 1, 1, 1]])

        self.R = find_T_matrix(ptsh, to_pts)
        self.Rinv = np.linalg.inv(self.R)
        self._setPerspective(update=False)
示例#8
0
def augment_sample(I,pts,dim):

	maxsum,maxangle = 120,np.array([80.,80.,45.])
	angles = np.random.rand(3)*maxangle
	if angles.sum() > maxsum:
		angles = (angles/angles.sum())*(maxangle/maxangle.sum())

	I = im2single(I)  # /255.0 归一化
	iwh = getWH(I.shape) #得到宽高

	whratio = random.uniform(2.,4.) # 随即取一个2-4的值作为宽高比
	wsiz = random.uniform(dim*.2,dim*1.) # 宽取0.2*208 到 208之间
	
	hsiz = wsiz/whratio

	dx = random.uniform(0.,dim - wsiz)
	dy = random.uniform(0.,dim - hsiz)

    #下面涉及到整个变换
    # In the first 3 lines, the original corner points are transformed into a rectangular bounding box with aspect ratio 
    # varying between 2:1 and 4:1. In other words, T matrix rectifies the LP with a random aspect ratio. Then, 
    
	pph = getRectPts(dx,dy,dx+wsiz,dy+hsiz)
	pts = pts*iwh.reshape((2,1))  #将点恢复到真实坐标值
	T = find_T_matrix(pts2ptsh(pts),pph)
    #in the next two lines, a perspective transformation with random rotation (H) is combined with T to 
    #generate the final transformation.
	H = perspective_transform((dim,dim),angles=angles)
	H = np.matmul(H,T)

	Iroi,pts = project(I,H,pts,dim)
	
	hsv_mod = np.random.rand(3).astype('float32')
	hsv_mod = (hsv_mod - .5)*.3
	hsv_mod[0] *= 360
	Iroi = hsv_transform(Iroi,hsv_mod)
	Iroi = np.clip(Iroi,0.,1.)

	pts = np.array(pts)

	if random.random() > .5:
		Iroi,pts = flip_image_and_pts(Iroi,pts)

	tl,br = pts.min(1),pts.max(1)
	llp = Label(0,tl,br)

	return Iroi,llp,pts
示例#9
0
def augment_sample(I,pts,dim):

	maxsum,maxangle = 120,np.array([80.,80.,45.])
	angles = np.random.rand(3)*maxangle
	if angles.sum() > maxsum:
		angles = (angles/angles.sum())*(maxangle/maxangle.sum())

	# chuyển qua ma trận float32, chia cho 255
	I = im2single(I)
	# Lấy w, h dạng ma trận
	iwh = getWH(I.shape)

	whratio = random.uniform(2.,4.)
	wsiz = random.uniform(dim*.2,dim*1.)
	
	hsiz = wsiz/whratio

	dx = random.uniform(0.,dim - wsiz)
	dy = random.uniform(0.,dim - hsiz)

	pph = getRectPts(dx,dy,dx+wsiz,dy+hsiz)
	# Lấy tọa độ thật
	pts = pts*iwh.reshape((2,1))
	T = find_T_matrix(pts2ptsh(pts),pph)

	H = perspective_transform((dim,dim),angles=angles)
	H = np.matmul(H,T)

	Iroi,pts = project(I,H,pts,dim)
	
	hsv_mod = np.random.rand(3).astype('float32')
	hsv_mod = (hsv_mod - .5)*.3
	hsv_mod[0] *= 360
	Iroi = hsv_transform(Iroi,hsv_mod)
	Iroi = np.clip(Iroi,0.,1.)

	pts = np.array(pts)

	if random.random() > .5:
		Iroi,pts = flip_image_and_pts(Iroi,pts)

	# lấy giá trị tọa độ top-left, bot-right
	tl,br = pts.min(1),pts.max(1)
	llp = Label(0,tl,br)

	return Iroi,llp,pts
 def mouse_callback(self, event, x, y, flags, param):
     mc = np.array([x, y], dtype=float)
     mc = np.matmul(self.Rinv, np.append(mc, 1.))
     mc = np.squeeze(np.array(mc))
     self.mouse_center = (mc[:2] / mc[2]) / getWH(self.Iorig.shape)