Пример #1
0
    def _draw_rectangle(self):

        # xc, yc, w, h, theta, R, G, B, A
        x0, y0, w, h, theta = self.stroke_params[0:5]
        R0, G0, B0, ALPHA = self.stroke_params[5:]
        x0 = _normalize(x0, self.CANVAS_WIDTH)
        y0 = _normalize(y0, self.CANVAS_WIDTH)
        w = (int)(1 + w * self.CANVAS_WIDTH // 4)
        h = (int)(1 + h * self.CANVAS_WIDTH // 4)
        theta = np.pi * theta
        stroke_alpha_value = self.stroke_params[-1]

        self.foreground = np.zeros_like(
            self.canvas, dtype=np.uint8)  # uint8 for antialiasing
        self.stroke_alpha_map = np.zeros_like(
            self.canvas, dtype=np.uint8)  # uint8 for antialiasing

        color = (R0 * 255, G0 * 255, B0 * 255)
        alpha = (stroke_alpha_value * 255, stroke_alpha_value * 255,
                 stroke_alpha_value * 255)
        ptc = (x0, y0)
        pt0 = utils.rotate_pt(pt=(x0 - w, y0 - h),
                              rotate_center=ptc,
                              theta=theta)
        pt1 = utils.rotate_pt(pt=(x0 + w, y0 - h),
                              rotate_center=ptc,
                              theta=theta)
        pt2 = utils.rotate_pt(pt=(x0 + w, y0 + h),
                              rotate_center=ptc,
                              theta=theta)
        pt3 = utils.rotate_pt(pt=(x0 - w, y0 + h),
                              rotate_center=ptc,
                              theta=theta)

        ppt = np.array([pt0, pt1, pt2, pt3], np.int32)
        ppt = ppt.reshape((-1, 1, 2))
        cv2.fillPoly(self.foreground, [ppt], color, lineType=cv2.LINE_AA)
        cv2.fillPoly(self.stroke_alpha_map, [ppt], alpha, lineType=cv2.LINE_AA)

        if not self.train:
            self.foreground = cv2.dilate(self.foreground, np.ones([2, 2]))
            self.stroke_alpha_map = cv2.erode(self.stroke_alpha_map,
                                              np.ones([2, 2]))

        self.foreground = np.array(self.foreground, dtype=np.float32) / 255.
        self.stroke_alpha_map = np.array(self.stroke_alpha_map,
                                         dtype=np.float32) / 255.
        self.canvas = self._update_canvas()
Пример #2
0
    def _draw_markerpen(self):

        # x0, y0, x1, y1, x2, y2, radius0, radius2, R, G, B, A
        x0, y0, x1, y1, x2, y2, radius, _ = self.stroke_params[0:8]
        R0, G0, B0, ALPHA = self.stroke_params[8:]
        x1 = x0 + (x2 - x0) * x1
        y1 = y0 + (y2 - y0) * y1
        x0 = _normalize(x0, self.CANVAS_WIDTH)
        x1 = _normalize(x1, self.CANVAS_WIDTH)
        x2 = _normalize(x2, self.CANVAS_WIDTH)
        y0 = _normalize(y0, self.CANVAS_WIDTH)
        y1 = _normalize(y1, self.CANVAS_WIDTH)
        y2 = _normalize(y2, self.CANVAS_WIDTH)
        radius = (int)(1 + radius * self.CANVAS_WIDTH // 4)

        stroke_alpha_value = self.stroke_params[-1]

        self.foreground = np.zeros_like(
            self.canvas, dtype=np.uint8)  # uint8 for antialiasing
        self.stroke_alpha_map = np.zeros_like(
            self.canvas, dtype=np.uint8)  # uint8 for antialiasing

        if abs(x0 - x2) + abs(y0 - y2) < 4:  # too small, dont draw
            self.foreground = np.array(self.foreground,
                                       dtype=np.float32) / 255.
            self.stroke_alpha_map = np.array(self.stroke_alpha_map,
                                             dtype=np.float32) / 255.
            self.canvas = self._update_canvas()
            return

        color = (R0 * 255, G0 * 255, B0 * 255)
        alpha = (stroke_alpha_value * 255, stroke_alpha_value * 255,
                 stroke_alpha_value * 255)
        tmp = 1. / 100
        for i in range(100):
            t = i * tmp
            x = (1 - t) * (1 - t) * x0 + 2 * t * (1 - t) * x1 + t * t * x2
            y = (1 - t) * (1 - t) * y0 + 2 * t * (1 - t) * y1 + t * t * y2

            ptc = (x, y)
            dx = 2 * (t - 1) * x0 + 2 * (1 - 2 * t) * x1 + 2 * t * x2
            dy = 2 * (t - 1) * y0 + 2 * (1 - 2 * t) * y1 + 2 * t * y2

            theta = np.arctan2(dx, dy) - np.pi / 2
            pt0 = utils.rotate_pt(pt=(x - radius, y - radius),
                                  rotate_center=ptc,
                                  theta=theta)
            pt1 = utils.rotate_pt(pt=(x + radius, y - radius),
                                  rotate_center=ptc,
                                  theta=theta)
            pt2 = utils.rotate_pt(pt=(x + radius, y + radius),
                                  rotate_center=ptc,
                                  theta=theta)
            pt3 = utils.rotate_pt(pt=(x - radius, y + radius),
                                  rotate_center=ptc,
                                  theta=theta)
            ppt = np.array([pt0, pt1, pt2, pt3], np.int32)
            ppt = ppt.reshape((-1, 1, 2))
            cv2.fillPoly(self.foreground, [ppt], color, lineType=cv2.LINE_AA)
            cv2.fillPoly(self.stroke_alpha_map, [ppt],
                         alpha,
                         lineType=cv2.LINE_AA)

        if not self.train:
            self.foreground = cv2.dilate(self.foreground, np.ones([2, 2]))
            self.stroke_alpha_map = cv2.erode(self.stroke_alpha_map,
                                              np.ones([2, 2]))

        self.foreground = np.array(self.foreground, dtype=np.float32) / 255.
        self.stroke_alpha_map = np.array(self.stroke_alpha_map,
                                         dtype=np.float32) / 255.
        self.canvas = self._update_canvas()