Python cv2.line() Examples

The following are 30 code examples of cv2.line(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cv2 , or try the search function .
Example #1
Source File: common.py    From OpenCV-Python-Tutorial with MIT License 7 votes vote down vote up
def on_mouse(self, event, x, y, flags, param):
        pt = (x, y)
        if event == cv2.EVENT_LBUTTONDOWN:
            self.prev_pt = pt
        elif event == cv2.EVENT_LBUTTONUP:
            self.prev_pt = None

        if self.prev_pt and flags & cv2.EVENT_FLAG_LBUTTON:
            for dst, color in zip(self.dests, self.colors_func()):
                cv2.line(dst, self.prev_pt, pt, color, 5)
            self.dirty = True
            self.prev_pt = pt
            self.show()


# palette data from matplotlib/_cm.py 
Example #2
Source File: mlt17_to_voc.py    From tf_ctpn with MIT License 6 votes vote down vote up
def test():
    # p1 = Point(5, 1)
    # p2 = Point(1, 4)
    # l1 = Line(p2, p1)
    # print(l1.k)
    #
    # p1 = Point(5, 4)
    # p2 = Point(1, 1)
    # l1 = Line(p1, p2)
    # print(l1.k)

    pnts = [(40, 40), (140, 85), (140, 160), (50, 100)]
    img = np.zeros((200, 200, 3))
    a = cv2.minAreaRect(np.asarray(pnts))

    img = cv2.line(img, pnts[0], pnts[1], (0, 255, 0), thickness=1)
    img = cv2.line(img, pnts[1], pnts[2], (0, 255, 0), thickness=1)
    img = cv2.line(img, pnts[2], pnts[3], (0, 255, 0), thickness=1)
    img = cv2.line(img, pnts[3], pnts[0], (0, 255, 0), thickness=1)

    box = cv2.boxPoints(a)

    def tt(p):
        return (p[0], p[1])

    img = cv2.line(img, tt(box[0]), tt(box[1]), (255, 255, 0), thickness=1)
    img = cv2.line(img, tt(box[1]), tt(box[2]), (255, 255, 0), thickness=1)
    img = cv2.line(img, tt(box[2]), tt(box[3]), (255, 255, 0), thickness=1)
    img = cv2.line(img, tt(box[3]), tt(box[0]), (255, 255, 0), thickness=1)

    cv2.imshow('test', img.astype(np.uint8))
    cv2.waitKey(0) 
Example #3
Source File: sunrgbd_utils.py    From H3DNet with MIT License 6 votes vote down vote up
def draw_projected_box3d(image, qs, color=(255,255,255), thickness=2):
    ''' Draw 3d bounding box in image
        qs: (8,2) array of vertices for the 3d box in following order:
            1 -------- 0
           /|         /|
          2 -------- 3 .
          | |        | |
          . 5 -------- 4
          |/         |/
          6 -------- 7
    '''
    qs = qs.astype(np.int32)
    for k in range(0,4):
       #http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html
       i,j=k,(k+1)%4
       cv2.line(image, (qs[i,0],qs[i,1]), (qs[j,0],qs[j,1]), color, thickness, cv2.CV_AA) # use LINE_AA for opencv3

       i,j=k+4,(k+1)%4 + 4
       cv2.line(image, (qs[i,0],qs[i,1]), (qs[j,0],qs[j,1]), color, thickness, cv2.CV_AA)

       i,j=k,k+4
       cv2.line(image, (qs[i,0],qs[i,1]), (qs[j,0],qs[j,1]), color, thickness, cv2.CV_AA)
    return image 
Example #4
Source File: filters.py    From exposure with MIT License 6 votes vote down vote up
def visualize_filter(self, debug_info, canvas):
    curve = debug_info['filter_parameters']
    height, width = canvas.shape[:2]
    for i in range(self.channels):
      values = np.array([0] + list(curve[0][0][i]))
      values /= sum(values) + 1e-30
      scale = 1
      values *= scale
      for j in range(0, self.cfg.curve_steps):
        values[j + 1] += values[j]
      for j in range(self.cfg.curve_steps):
        p1 = tuple(
            map(int, (width / self.cfg.curve_steps * j, height - 1 -
                      values[j] * height)))
        p2 = tuple(
            map(int, (width / self.cfg.curve_steps * (j + 1), height - 1 -
                      values[j + 1] * height)))
        color = []
        for t in range(self.channels):
          color.append(1 if t == i else 0)
        cv2.line(canvas, p1, p2, tuple(color), thickness=1) 
Example #5
Source File: esr_visualizer.py    From Udacity-SDC-Radar-Driver-Micro-Challenge with MIT License 6 votes vote down vote up
def update(self, radarData):
        self.img = np.zeros((self.height, self.width, self.channels), np.uint8)
        cv2.line(self.img, (10, 0), (self.width/2 - 5, self.height), (100, 255, 255))
        cv2.line(self.img, (self.width - 10, 0), (self.width/2 + 5, self.height), (100, 255, 255))

        for track_number in range(1, 65):
            if str(track_number)+'_track_range' in radarData:
                track_range = radarData[str(track_number)+'_track_range']
                track_angle = (float(radarData[str(track_number)+'_track_angle'])+90.0)*math.pi/180

                x_pos = math.cos(track_angle)*track_range*4
                y_pos = math.sin(track_angle)*track_range*4

                cv2.circle(self.img, (self.width/2 + int(x_pos), self.height - int(y_pos) - 10), 5, (255, 255, 255))
                #cv2.putText(self.img, str(track_number), 
                #    (self.width/2 + int(x_pos)-2, self.height - int(y_pos) - 10), self.font, 1, (255,255,255), 2)

        cv2.imshow("Radar", self.img)
        cv2.waitKey(2) 
Example #6
Source File: esr_visualizer.py    From Udacity-SDC-Radar-Driver-Micro-Challenge with MIT License 6 votes vote down vote up
def update(self, radarData):
        self.img = np.zeros((self.height, self.width, self.channels), np.uint8)
        cv2.line(self.img, (10, 0), (self.width/2 - 5, self.height), (100, 255, 255))
        cv2.line(self.img, (self.width - 10, 0), (self.width/2 + 5, self.height), (100, 255, 255))

        for track_number in range(1, 65):
            if str(track_number)+'_track_range' in radarData:
                track_range = radarData[str(track_number)+'_track_range']
                track_angle = (float(radarData[str(track_number)+'_track_angle'])+90.0)*math.pi/180

                x_pos = math.cos(track_angle)*track_range*4
                y_pos = math.sin(track_angle)*track_range*4

                cv2.circle(self.img, (self.width/2 + int(x_pos), self.height - int(y_pos) - 10), 5, (255, 255, 255))
                #cv2.putText(self.img, str(track_number), 
                #    (self.width/2 + int(x_pos)-2, self.height - int(y_pos) - 10), self.font, 1, (255,255,255), 2)

        cv2.imshow("Radar", self.img)
        cv2.waitKey(2) 
Example #7
Source File: filters.py    From exposure with MIT License 6 votes vote down vote up
def get_curve(self):
        L = 200
        canvas = np.ones((L, L, 3)) * 0.851
        curve_step = 16
        xs = np.linspace(0.0, 1.0, curve_step + 1)[:, None, None]
        xs = np.concatenate([xs] * 3, axis=2)
        ys = self.apply(xs.copy())
        ys = np.clip(ys, 0.01, 1.0)
        for i in range(curve_step):
            for j in range(3):
                x, y = xs[i, 0, j], ys[i, 0, j]
                xx, yy = xs[i + 1, 0, j], ys[i + 1, 0, j]
                color = [0, 0, 0]
                color[j] = 0.7
                color = tuple(color)
                cv2.line(canvas, (int(L * x), int(L - L * y)), (int(L * xx), int(L - L * yy)), color, 1)
        return canvas 
Example #8
Source File: my.py    From 3D-HourGlass-Network with MIT License 6 votes vote down vote up
def func1(k=None):
    if not k:
        k=randint(0, 20)
    print('image is',k)
    for i, (img, heatmap,vecmap,depthmap,kpt_3d) in enumerate(train_loader):
        if i==k:
#            test_heatmaps(heatmap,img,i)
#            test_vecmaps(vecmap,img,i)
#            edges = [[0, 1], [1, 2], [2, 6], [6, 3], [3, 4], [4, 5], [10, 11], [11, 12], [12, 8], [8, 13], [13, 14], [14, 15], [6, 8], [8, 9]]
#            ppl=kpt_3d.shape[0]
#            for i in range(ppl):
#                for edge in edges:
#                    cv2.line(img,(int(kpt_3d[i][edge[0]][0]),int(kpt_3d[i][edge[0]][1])),(int(kpt_3d[i][edge[1]][0]),int(kpt_3d[i][edge[1]][1])),(0,255,0))
#            cv2.imwrite('outside3dfinal.png',img)
       
            return img,heatmap,vecmap,depthmap,kpt_3d 
Example #9
Source File: my.py    From 3D-HourGlass-Network with MIT License 6 votes vote down vote up
def func1(k=None):
    if not k:
        k=randint(0, 20)
    print('image is',k)
    for i, (img, heatmap,vecmap,depthmap,kpt_3d) in enumerate(train_loader):
        if i==k:
#            test_heatmaps(heatmap,img,i)
#            test_vecmaps(vecmap,img,i)
#            edges = [[0, 1], [1, 2], [2, 6], [6, 3], [3, 4], [4, 5], [10, 11], [11, 12], [12, 8], [8, 13], [13, 14], [14, 15], [6, 8], [8, 9]]
#            ppl=kpt_3d.shape[0]
#            for i in range(ppl):
#                for edge in edges:
#                    cv2.line(img,(int(kpt_3d[i][edge[0]][0]),int(kpt_3d[i][edge[0]][1])),(int(kpt_3d[i][edge[1]][0]),int(kpt_3d[i][edge[1]][1])),(0,255,0))
#            cv2.imwrite('outside3dfinal.png',img)
       
            return img,heatmap,vecmap,depthmap,kpt_3d 
Example #10
Source File: estimator.py    From tf-pose with Apache License 2.0 6 votes vote down vote up
def draw_humans(npimg, humans, imgcopy=False):
        if imgcopy:
            npimg = np.copy(npimg)
        image_h, image_w = npimg.shape[:2]
        centers = {}
        for human in humans:
            # draw point
            for i in range(common.CocoPart.Background.value):
                if i not in human.body_parts.keys():
                    continue

                body_part = human.body_parts[i]
                center = (int(body_part.x * image_w + 0.5), int(body_part.y * image_h + 0.5))
                centers[i] = center
                cv2.circle(npimg, center, 3, common.CocoColors[i], thickness=3, lineType=8, shift=0)

            # draw line
            for pair_order, pair in enumerate(common.CocoPairsRender):
                if pair[0] not in human.body_parts.keys() or pair[1] not in human.body_parts.keys():
                    continue

                npimg = cv2.line(npimg, centers[pair[0]], centers[pair[1]], common.CocoColors[pair_order], 3)

        return npimg 
Example #11
Source File: draw.py    From tf-pose with Apache License 2.0 6 votes vote down vote up
def draw_limbs(image, pose_2d, visible):
    """Draw the 2D pose without the occluded/not visible joints."""

    _COLORS = [
        [0, 0, 255], [0, 170, 255], [0, 255, 170], [0, 255, 0],
        [170, 255, 0], [255, 170, 0], [255, 0, 0], [255, 0, 170],
        [170, 0, 255]
    ]
    _LIMBS = np.array([0, 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9,
                       9, 10, 11, 12, 12, 13]).reshape((-1, 2))

    _NORMALISATION_FACTOR = int(math.floor(math.sqrt(image.shape[0] * image.shape[1] / NORMALISATION_COEFFICIENT)))

    for oid in range(pose_2d.shape[0]):
        for lid, (p0, p1) in enumerate(_LIMBS):
            if not (visible[oid][p0] and visible[oid][p1]):
                continue
            y0, x0 = pose_2d[oid][p0]
            y1, x1 = pose_2d[oid][p1]
            cv2.circle(image, (x0, y0), JOINT_DRAW_SIZE *_NORMALISATION_FACTOR , _COLORS[lid], -1)
            cv2.circle(image, (x1, y1), JOINT_DRAW_SIZE*_NORMALISATION_FACTOR , _COLORS[lid], -1)
            cv2.line(image, (x0, y0), (x1, y1),
                     _COLORS[lid], LIMB_DRAW_SIZE*_NORMALISATION_FACTOR , 16) 
Example #12
Source File: rotation2xyz.py    From Auto_Conditioned_RNN_motion with MIT License 6 votes vote down vote up
def visualize_joints(bone_list, focus):
    m = np.zeros((424, 600, 3))
    m.astype(np.uint8)
    for bone in bone_list:
        p1x = bone[0][0]
        p1y = bone[0][1]
        p1z = bone[0][2] + 400
        p2x = bone[1][0]
        p2y = bone[1][1]
        p2z = bone[1][2] + 400
        p1 = (
         int(p1x * focus / p1z + 300.0), int(-p1y * focus / p1z + 204.0))
        p2 = (int(p2x * focus / p2z + 300.0), int(-p2y * focus / p2z + 204.0))
        if inside_image(p1[0], p1[1]) and inside_image(p2[0], p2[1]):
            cv.line(m, p1, p2, (255, 0, 0), 2)
            cv.circle(m, p1, 2, (0, 255, 255), -1)
            cv.circle(m, p2, 2, (0, 255, 255), -1)

    return m 
Example #13
Source File: sunrgbd_utils.py    From H3DNet with MIT License 6 votes vote down vote up
def __init__(self, line):
        data = line.split(' ')
        data[1:] = [float(x) for x in data[1:]]
        self.classname = data[0]
        self.xmin = data[1] 
        self.ymin = data[2]
        self.xmax = data[1]+data[3]
        self.ymax = data[2]+data[4]
        self.box2d = np.array([self.xmin,self.ymin,self.xmax,self.ymax])
        self.centroid = np.array([data[5],data[6],data[7]])
        self.unused_dimension = np.array([data[8],data[9],data[10]])
        self.w = data[8]
        self.l = data[9]
        self.h = data[10]
        self.orientation = np.zeros((3,))
        self.orientation[0] = data[11]
        self.orientation[1] = data[12]
        self.heading_angle = -1 * np.arctan2(self.orientation[1], self.orientation[0]) 
Example #14
Source File: mlt17_to_voc.py    From tf_ctpn with MIT License 6 votes vote down vote up
def split_text_line(line, step):
    """
    按照 Bounding box 对文字进行划分
    :param line: (xmin, ymin, xmax, ymax)
    :return: [(anchor_xmin, anchor_ymin, anchor_xmax, anchor_ymax)]
    """
    xmin, ymin, xmax, ymax = line
    width = xmax - xmin

    anchor_count = int(math.ceil(width / step))

    splited_lines = []

    for i in range(anchor_count):
        anchor_xmin = i * step + xmin
        anchor_ymin = ymin
        anchor_xmax = anchor_xmin + step - 1
        anchor_ymax = ymax

        splited_lines.append((anchor_xmin, anchor_ymin, anchor_xmax, anchor_ymax))

    return splited_lines 
Example #15
Source File: mlt17_to_voc.py    From tf_ctpn with MIT License 6 votes vote down vote up
def clip_line(line, size):
    """
    :param line: (xmin, ymin, xmax, ymax)
    :param size: (height, width)
    :return: (xmin, ymin, xmax, ymax)
    """
    xmin, ymin, xmax, ymax = line

    if xmin < 0:
        xmin = 0
    if xmax > size[1] - 1:
        xmax = size[1] - 1
    if ymin < 0:
        ymin = 0
    if ymax > size[0] - 1:
        ymax = size[0] - 1

    return xmin, ymin, xmax, ymax 
Example #16
Source File: demo.py    From tf_ctpn with MIT License 6 votes vote down vote up
def save_result(img, img_name, text_lines, result_dir):
    dst = img
    color = (0, 150, 0)
    for bbox in text_lines:
        bbox = [int(x) for x in bbox]
        p1 = (bbox[0], bbox[1])
        p2 = (bbox[2], bbox[3])
        p3 = (bbox[6], bbox[7])
        p4 = (bbox[4], bbox[5])
        dst = cv2.line(dst, p1, p2, color, 2)
        dst = cv2.line(dst, p2, p3, color, 2)
        dst = cv2.line(dst, p3, p4, color, 2)
        dst = cv2.line(dst, p4, p1, color, 2)

    img_path = os.path.join(result_dir, img_name[0:-4] + '.jpg')
    cv2.imwrite(img_path, dst) 
Example #17
Source File: data_inspect_utils.py    From centerpose with MIT License 6 votes vote down vote up
def apply_keypoint(image, keypoint, num_joints=17):
    image = image.astype(np.uint8)
    
    edges = [[0, 1], [0, 2], [1, 3], [2, 4], 
                    [3, 5], [4, 6], [5, 6], 
                    [5, 7], [7, 9], [6, 8], [8, 10], 
                    [5, 11], [6, 12], [11, 12], 
                    [11, 13], [13, 15], [12, 14], [14, 16]]
    
    for j in range(num_joints):
        if keypoint[j][2]>0.:
            cv2.circle(image,
                      (keypoint[j, 0], keypoint[j, 1]), 3, (255,255,255), 2)
                     
    stickwidth = 2
           
    for j, e in enumerate(edges):
        if keypoint[e[0],2] > 0. and keypoint[e[1],2] > 0.:
            centerA = keypoint[e[0],:2]
            centerB = keypoint[e[1],:2]
            cv2.line(image,(centerA[0], centerA[1]),(centerB[0], centerB[1]),(255, 255,255),2)
    return image 
Example #18
Source File: cv_plot.py    From centerpose with MIT License 6 votes vote down vote up
def plot_kpt(image, kpt):
    ''' Draw 68 key points
    Args: 
        image: the input image
        kpt: (68, 3).
    '''
    image = image.copy()
    kpt = np.round(kpt).astype(np.int32)
    for i in range(kpt.shape[0]):
        st = kpt[i, :2]
        image = cv2.circle(image, (st[0], st[1]), 1, (0, 0, 255), 2)
        if i in end_list:
            continue
        ed = kpt[i + 1, :2]
        image = cv2.line(image, (st[0], st[1]), (ed[0], ed[1]), (255, 255, 255), 1)
    return image 
Example #19
Source File: cv_plot.py    From LipReading with MIT License 6 votes vote down vote up
def plot_kpt(image, kpt):
    ''' Draw 68 key points
    Args: 
        image: the input image
        kpt: (68, 3).
    '''
    image = image.copy()
    kpt = np.round(kpt).astype(np.int32)
    for i in range(kpt.shape[0]):
        st = kpt[i, :2]
        image = cv2.circle(image,(st[0], st[1]), 1, (0,0,255), 2)  
        if i in end_list:
            continue
        ed = kpt[i + 1, :2]
        image = cv2.line(image, (st[0], st[1]), (ed[0], ed[1]), (255, 255, 255), 1)
    return image 
Example #20
Source File: rotation2xyz.py    From Auto_Conditioned_RNN_motion with MIT License 6 votes vote down vote up
def visualize_joints2(bone_list, focus):
    m = np.zeros((424, 600, 3))
    m.astype(np.uint8)
    for bone in bone_list:
        p1x = bone[0][0]
        p1y = bone[0][1]
        p1z = bone[0][2] + 400
        p2x = bone[1][0]
        p2y = bone[1][1]
        p2z = bone[1][2] + 400
        p1 = (
         int(p1x * focus / p1z + 300.0), int(-p1y * focus / p1z + 204.0))
        p2 = (int(p2x * focus / p2z + 300.0), int(-p2y * focus / p2z + 204.0))
        if inside_image(p1[0], p1[1]) and inside_image(p2[0], p2[1]):
            cv.line(m, p1, p2, (255, 0, 0), 2)
            cv.circle(m, p1, 2, (0, 255, 255), -1)
            cv.circle(m, p2, 2, (0, 255, 255), -1)

    return m 
Example #21
Source File: rputil.py    From RelativePose with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def drawMatch(img0,img1,src,tgt,color='b'):
    if len(img0.shape)==2:
      img0=np.expand_dims(img0,2)
    if len(img1.shape)==2:
      img1=np.expand_dims(img1,2)
    h,w = img0.shape[0],img0.shape[1]
    img = np.zeros([2*h,w,3])
    img[:h,:,:] = img0
    img[h:,:,:] = img1
    n = len(src)
    if color == 'b':
        color=(255,0,0)
    else:
        color=(0,255,0)
    for i in range(n):
      cv2.circle(img, (int(src[i,0]), int(src[i,1])), 3,color,-1)
      cv2.circle(img, (int(tgt[i,0]), int(tgt[i,1])+h), 3,color,-1)
      cv2.line(img, (int(src[i,0]),int(src[i,1])),(int(tgt[i,0]),int(tgt[i,1])+h),color,1)
    return img 
Example #22
Source File: hist.py    From OpenCV-Python-Tutorial with MIT License 5 votes vote down vote up
def hist_lines(im):
    h = np.zeros((300,256,3))
    if len(im.shape)!=2:
        print("hist_lines applicable only for grayscale images")
        #print("so converting image to grayscale for representation"
        im = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    hist_item = cv2.calcHist([im],[0],None,[256],[0,256])
    cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX)
    hist=np.int32(np.around(hist_item))
    for x,y in enumerate(hist):
        cv2.line(h,(x,0),(x,y),(255,255,255))
    y = np.flipud(h)
    return y 
Example #23
Source File: code.py    From OpenCV-Python-Tutorial with MIT License 5 votes vote down vote up
def drawlines(img1, img2, lines, pts1, pts2):
    ''' img1 - image on which we draw the epilines for the points in img2
        lines - corresponding epilines '''
    r, c = img1.shape
    img1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
    img2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2BGR)
    for r, pt1, pt2 in zip(lines, pts1, pts2):
        color = tuple(np.random.randint(0, 255, 3).tolist())
        x0, y0 = map(int, [0, -r[2] / r[1]])
        x1, y1 = map(int, [c, -(r[2] + r[0] * c) / r[1]])
        img1 = cv2.line(img1, (x0, y0), (x1, y1), color, 1)
        img1 = cv2.circle(img1, tuple(pt1), 5, color, -1)
        img2 = cv2.circle(img2, tuple(pt2), 5, color, -1)
    return img1, img2 
Example #24
Source File: 检测线条和形状-几何形状.py    From OpenCV-Python-Tutorial with MIT License 5 votes vote down vote up
def lines(self):
        lines = cv2.HoughLinesP(self.image, 1, np.pi / 2, 6, None, 50, 10)
        for line in lines[0]:
            pt1 = (line[0], line[1])
            pt2 = (line[2], line[3])
            cv2.line(self.image, pt1, pt2, (0, 0, 255), 2) 
Example #25
Source File: calib3d.py    From OpenCV-Python-Tutorial with MIT License 5 votes vote down vote up
def draw(img, corners, imgpts):
    corner = tuple(corners[0].ravel())
    img = cv2.line(img, corner, tuple(imgpts[0].ravel()), (255, 0, 0), 5)
    img = cv2.line(img, corner, tuple(imgpts[1].ravel()), (0, 255, 0), 5)
    img = cv2.line(img, corner, tuple(imgpts[2].ravel()), (0, 0, 255), 5)
    return img


# 渲染一个立方体 
Example #26
Source File: hist.py    From OpenCV-Python-Tutorial with MIT License 5 votes vote down vote up
def hist_lines(im):
    h = np.zeros((300, 256, 3))
    if len(im.shape) != 2:
        print("hist_lines applicable only for grayscale images")
        # print "so converting image to grayscale for representation"
        im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    hist_item = cv2.calcHist([im], [0], None, [256], [0, 256])
    cv2.normalize(hist_item, hist_item, 0, 255, cv2.NORM_MINMAX)
    hist = np.int32(np.around(hist_item))
    for x, y in enumerate(hist):
        cv2.line(h, (x, 0), (x, y), (255, 255, 255))
    y = np.flipud(h)
    return y 
Example #27
Source File: mosse.py    From OpenCV-Python-Tutorial with MIT License 5 votes vote down vote up
def draw_state(self, vis):
        (x, y), (w, h) = self.pos, self.size
        x1, y1, x2, y2 = int(x-0.5*w), int(y-0.5*h), int(x+0.5*w), int(y+0.5*h)
        cv2.rectangle(vis, (x1, y1), (x2, y2), (0, 0, 255))
        if self.good:
            cv2.circle(vis, (int(x), int(y)), 2, (0, 0, 255), -1)
        else:
            cv2.line(vis, (x1, y1), (x2, y2), (0, 0, 255))
            cv2.line(vis, (x2, y1), (x1, y2), (0, 0, 255))
        draw_str(vis, (x1, y2+16), 'PSR: %.2f' % self.psr) 
Example #28
Source File: kalman.py    From OpenCV-Python-Tutorial with MIT License 5 votes vote down vote up
def draw_cross(center, color, d):
                cv2.line(img,
                         (center[0] - d, center[1] - d), (center[0] + d, center[1] + d),
                         color, 1, cv2.LINE_AA, 0)
                cv2.line(img,
                         (center[0] + d, center[1] - d), (center[0] - d, center[1] + d),
                         color, 1, cv2.LINE_AA, 0) 
Example #29
Source File: contours.py    From OpenCV-Python-Tutorial with MIT License 5 votes vote down vote up
def make_image():
    img = np.zeros((500, 500), np.uint8)
    black, white = 0, 255
    for i in xrange(6):
        dx = int((i%2)*250 - 30)
        dy = int((i/2.)*150)

        if i == 0:
            for j in xrange(11):
                angle = (j+5)*np.pi/21
                c, s = np.cos(angle), np.sin(angle)
                x1, y1 = np.int32([dx+100+j*10-80*c, dy+100-90*s])
                x2, y2 = np.int32([dx+100+j*10-30*c, dy+100-30*s])
                cv2.line(img, (x1, y1), (x2, y2), white)

        cv2.ellipse( img, (dx+150, dy+100), (100,70), 0, 0, 360, white, -1 )
        cv2.ellipse( img, (dx+115, dy+70), (30,20), 0, 0, 360, black, -1 )
        cv2.ellipse( img, (dx+185, dy+70), (30,20), 0, 0, 360, black, -1 )
        cv2.ellipse( img, (dx+115, dy+70), (15,15), 0, 0, 360, white, -1 )
        cv2.ellipse( img, (dx+185, dy+70), (15,15), 0, 0, 360, white, -1 )
        cv2.ellipse( img, (dx+115, dy+70), (5,5), 0, 0, 360, black, -1 )
        cv2.ellipse( img, (dx+185, dy+70), (5,5), 0, 0, 360, black, -1 )
        cv2.ellipse( img, (dx+150, dy+100), (10,5), 0, 0, 360, black, -1 )
        cv2.ellipse( img, (dx+150, dy+150), (40,10), 0, 0, 360, black, -1 )
        cv2.ellipse( img, (dx+27, dy+100), (20,35), 0, 0, 360, white, -1 )
        cv2.ellipse( img, (dx+273, dy+100), (20,35), 0, 0, 360, white, -1 )
    return img 
Example #30
Source File: plane_ar.py    From OpenCV-Python-Tutorial with MIT License 5 votes vote down vote up
def draw_overlay(self, vis, tracked):
        x0, y0, x1, y1 = tracked.target.rect
        quad_3d = np.float32([[x0, y0, 0], [x1, y0, 0], [x1, y1, 0], [x0, y1, 0]])
        fx = 0.5 + cv2.getTrackbarPos('focal', 'plane') / 50.0
        h, w = vis.shape[:2]
        K = np.float64([[fx*w, 0, 0.5*(w-1)],
                        [0, fx*w, 0.5*(h-1)],
                        [0.0,0.0,      1.0]])
        dist_coef = np.zeros(4)
        ret, rvec, tvec = cv2.solvePnP(quad_3d, tracked.quad, K, dist_coef)
        verts = ar_verts * [(x1-x0), (y1-y0), -(x1-x0)*0.3] + (x0, y0, 0)
        verts = cv2.projectPoints(verts, rvec, tvec, K, dist_coef)[0].reshape(-1, 2)
        for i, j in ar_edges:
            (x0, y0), (x1, y1) = verts[i], verts[j]
            cv2.line(vis, (int(x0), int(y0)), (int(x1), int(y1)), (255, 255, 0), 2)