Beispiel #1
0
def draw_ackerman_model(image, center, object, mtp_ratio, pursuit_point=None):

    corners = object.get_axel_corners()
    corners = center + np.array(corners) * mtp_ratio
    corners = corners.reshape((-1, 1, 2))

    # Drawing axel frame
    cv2.polylines(image, [corners.astype(np.int32)], True, (255, 50, 255), 1)

    front_left_tyre = center + np.array(object.front_left_tyre) * mtp_ratio
    front_right_tyre = center + np.array(object.front_right_tyre) * mtp_ratio
    rear_left_tyre = center + np.array(object.rear_left_tyre) * mtp_ratio
    rear_right_tyre = center + np.array(object.rear_right_tyre) * mtp_ratio

    cv2.line(image, tuple(front_left_tyre[0].astype(np.int32)),
             tuple(front_left_tyre[1].astype(np.int32)), (0, 0, 255), 3,
             cv2.LINE_AA)
    cv2.line(image, tuple(front_right_tyre[0].astype(np.int32)),
             tuple(front_right_tyre[1].astype(np.int32)), (0, 0, 255), 3,
             cv2.LINE_AA)
    cv2.line(image, tuple(rear_left_tyre[0].astype(np.int32)),
             tuple(rear_left_tyre[1].astype(np.int32)), (255, 0, 0), 3,
             cv2.LINE_AA)
    cv2.line(image, tuple(rear_right_tyre[0].astype(np.int32)),
             tuple(rear_right_tyre[1].astype(np.int32)), (255, 0, 0), 3,
             cv2.LINE_AA)

    if not pursuit_point is None:
        cv2.circle(
            image,
            tuple(center + (pursuit_point * mtp_ratio).astype(np.int32)), 3,
            (0, 100, 255), -1)
    def callback(self, ros_data):
        '''Callback function of subscribed topic. 
        Here images get converted and features detected'''
        if VERBOSE:
            print('received image of type: "%s"' % ros_data.format)
        np_arr = np.fromstring(ros_data.data, np.uint8)
        # Direct conversion to CV2
        frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)  # OpenCV >= 3.0:
        # Get frame dimensions
        frame_height = frame.shape[0]
        frame_width = frame.shape[1]
        frame_channels = frame.shape[2]
        # Image processing
        res = self.blue_filtering(frame)
        # Detect centrode
        cX, cY = self.detect_centrode(res)
        # Write the point (cX,xY) on "res" image
        cv2.circle(res, (int(cX), int(cY)), 5, (255, 0, 0), -1)
        # Normalizing w.r.t the center
        cX = int(cX - frame_width / 2)
        cY = int(cY - frame_height / 2)
        self.greenX_pub.publish(cX)

        # Print the center of mass coordinates w.r.t the center of image and diplay it
        if VERBOSE:
            print(cX, cY)
            cmd = self.extraction(cX)
            print(cmd)
        # Display the result
        if DISPLAY:
            cv2.imshow('frame', frame)
            cv2.imshow("res_center", res)
            cv2.waitKey(2)
Beispiel #3
0
def locate(target, want, show=0, msg=0):
    loc_pos = []
    want, treshold, c_name = want[0], want[1], want[2]
    result = cv2.matchTemplate(target, want, cv2.TM_CCOEFF_NORMED)
    location = numpy.where(result >= treshold)

    if msg:  # 显示正式寻找目标名称,调试时开启
        print(c_name, 'searching... ')

    h, w = want.shape[:-1]  # want.shape[:-1]

    n, ex, ey = 1, 0, 0
    for pt in zip(*location[::-1]):  # 其实这里经常是空的
        x, y = pt[0]+int(w/2), pt[1]+int(h/2)
        if (x-ex)+(y-ey) < 15:  # 去掉邻近重复的点
            continue
        ex, ey = x, y

        cv2.circle(target, (x, y), 10, (0, 0, 255), 3)

        if msg:
            print(c_name, 'we find it !!! ,at', x, y)

        x, y = int(x), int(y)
        loc_pos.append([x, y])

    if show:  # 在图上显示寻找的结果,调试时开启
        cv2.imshow('we get', target)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    return loc_pos
def facial_features_extraction():
    photo = 'sample.jpg'
    faceslist = []
    facial_landmarks = []
    image = Image.open(open(photo, 'rb'))
    iwidth, iheight = image.size
    res = detect_faces_from_localfile(photo)
    faces = res["FaceDetails"]
    for face in faces:
        faceslist.append(face)
        facial_landmarks.append(face["Landmarks"])
    while True:
        image = cv2.imread(photo)
        for face in faceslist:
            for lan in face["Landmarks"]:
                cv2.circle(
                    image,
                    (round(lan["X"] * iwidth), round(lan["Y"] * iheight)), 5,
                    (0, 0, 255), -1)
            left = iwidth * face["BoundingBox"]['Left']
            top = iheight * face["BoundingBox"]['Top']
            width = iwidth * face["BoundingBox"]['Width']
            height = iheight * face["BoundingBox"]['Height']
            cv2.rectangle(image, (round(left), round(top)),
                          (round(left + width), round(top + height)),
                          (0, 255, 0), 2)
        image = resizeimage(image)
        cv2.imshow("image", image)
        if cv2.waitKey(1) == 27:
            break
    cv2.destroyAllWindows()
Beispiel #5
0
def show_correspondence_circles(imgA, imgB, X1, Y1, X2, Y2):
    """
    Visualizes corresponding points between two images by plotting circles at
    each correspondence location. Corresponding points will have the same random color.

    Args:
    - imgA: A numpy array of shape (M,N,3)
    - imgB: A numpy array of shape (D,E,3)
    - x1: A numpy array of shape (k,) containing x-locations of keypoints in imgA
    - y1: A numpy array of shape (k,) containing y-locations of keypoints in imgA
    - x2: A numpy array of shape (j,) containing x-locations of keypoints in imgB
    - y2: A numpy array of shape (j,) containing y-locations of keypoints in imgB

    Returns:
    - newImg: A numpy array of shape (max(M,D), N+E, 3)
    """
    newImg = hstack_images(imgA, imgB)
    shiftX = imgA.shape[1]
    X1 = X1.astype(np.int)
    Y1 = Y1.astype(np.int)
    X2 = X2.astype(np.int)
    Y2 = Y2.astype(np.int)

    for x1, y1, x2, y2 in zip(X1, Y1, X2, Y2):
        cur_color = np.random.rand(3)
        green = (0, 1, 0)
        newImg = cv2.circle(newImg, (x1, y1), 10, cur_color, -1, cv2.LINE_AA)
        newImg = cv2.circle(newImg, (x1, y1), 10, green, 2, cv2.LINE_AA)
        newImg = cv2.circle(newImg, (x2 + shiftX, y2), 10, cur_color, -1,
                            cv2.LINE_AA)
        newImg = cv2.circle(newImg, (x2 + shiftX, y2), 10, green, 2,
                            cv2.LINE_AA)

    return newImg
Beispiel #6
0
def keyDetection(orig_img,
                 num_labels,
                 labels,
                 stats,
                 centroids,
                 min_key_area=100,
                 display_result=False):
    final_labels = []

    output_img = orig_img.copy()

    # Loop through the detected connected components
    for i in range(1, num_labels):
        [x, y, w, h, area] = getConnectedComponentRectangle(stats[i, :])
        (cX, cY) = centroids[i]

        # Consider only connected that have an area greater than min_key_area pixels
        if (min_key_area < area < np.inf):
            final_labels.append([i, cX])

            # Display on original image
            if (display_result):
                cv2.rectangle(output_img, (x, y), (x + w, y + h), (255, 0, 0),
                              1)
                cv2.circle(output_img, (int(cX), int(cY)), 4, (255, 255, 0),
                           -1)
                #componentMask = (labels == i).astype("uint8") * 255
                cv2.imshow("Output", output_img)
                cv2.waitKey(0)

    key_width = statistics.median(stats[:, cv2.CC_STAT_WIDTH])
    cv2.destroyAllWindows()

    #Return the centroid of the processed connected components and the median width
    return final_labels, key_width
Beispiel #7
0
def draw_circle(event, x, y, flags, param):
    global point1, img, G_RECT
    img2 = img.copy()  # 这里必须要拷贝一个新的
    if event == cv.EVENT_LBUTTONDOWN:  # 获取左上角的坐标
        point1 = x, y
        print("point1: ", point1)
        # 画图的时候是先在img2数据上进行处理,之后再将其显示出来
        cv.circle(img2, point1, 10, LINE_COLOR, LINE_WIDTH)
        cv.imshow("ori_image", img2)
    elif event == cv.EVENT_MOUSEMOVE and (flags & cv.EVENT_FLAG_LBUTTON):
        cv.rectangle(img2, point1, (x, y), LINE_COLOR, LINE_WIDTH)
        cv.imshow("ori_image", img2)

    elif event == cv.EVENT_LBUTTONUP:  # 鼠标左键按钮松开的时候画图
        point2 = x, y
        print("point2", point2)
        if point1 != point2:
            min_x = min(point1[0], point2[0])
            min_y = min(point1[1], point2[1])
            width = abs(point1[0] - point2[0])
            height = abs(point1[1] - point2[1])
            G_RECT = [min_x, min_y, width, height]
            print("g_rect: ", G_RECT)
            cv.rectangle(img2, point1, point2, LINE_COLOR, LINE_WIDTH)

        cv.imshow('ori_image', img2)
    def showData(self, data: Data):
        """ To show data if this module start standalone """

        if data.source_name == self.ME_NAME + '/Skeleton':
            person = data.data  #Points of skeleton
            aux = data.strToJSon(data.aux)
            h = int(Misc.hasKey(aux, 'H', self.ME_CONFIG['FRAME_HEIGHT']))
            w = int(Misc.hasKey(aux, 'W', self.ME_CONFIG['FRAME_WIDTH']))
            rgbImage = np.zeros((h, w, 3), np.uint8)
            for idj, join in enumerate(person):
                if join[0] != None:
                    cv2.circle(rgbImage, (join[0], join[1]),
                               5,
                               self.colors[idj],
                               thickness=-1)
        elif data.source_name == self.ME_NAME + '/Person':
            rgbImage = np.zeros((data.data.shape[0], data.data.shape[1], 3),
                                dtype=np.uint8)
            rgbImage[:, :, 0] = np.where(data.data[:, :] == 1, 255, 0)
            rgbImage[:, :, 1] = np.where(data.data[:, :] == 1, 255, 0)
            rgbImage[:, :, 2] = np.where(data.data[:, :] == 1, 255, 0)
        else:
            rgbImage = data.data  # For capture the image in RGB color space

        # Display the resulting frame
        cv2.imshow(data.source_name + '-' + data.source_item,
                   cv2.cvtColor(rgbImage, cv2.COLOR_BGR2RGB))

        if cv2.waitKey(1) & 0xFF == ord('q'):
            self.stop()
            cv2.destroyAllWindows()
def click_event(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.circle(img, (x, y), 3, (0, 0, 255), -1)
        points.append((x, y))
        if len(points) >= 2:
            cv2.line(img, points[-1], points[-2], (255, 0, 0), 2)
        cv2.imshow('image', img)
Beispiel #10
0
def draw_boxes_on_img(image):
    """ 在传入的图片上画出边角
    返回:画图后的图片,坐标,是否能画图
    """
    # 边缘检测
    img = image.copy()
    try:
        binary_img = getCanny(img)
        # 找边缘
        max_contour, max_area = findMaxContour(binary_img)
        # 拟合矩形
        boxes = getBoxPoint(max_contour)
        # 画矩形
        cv2.drawContours(img, max_contour, -1, (0, 255, 0), 9)
        # print(boxes)
        # [ 369  355]
        #  [2152  465]
        #  [2364 2776]
        #  [ 400 2985]
        # 画圆圈
        for box in boxes:
            cv2.circle(img, tuple(box), 30, (0, 255, 255), 10)
        return img, boxes, True
    except:
        print('无法检测')
        boxes = None
        return image, boxes, False
Beispiel #11
0
def extra_processing(showFrame):
    global pipeline

    m_table = NetworkTablesInstance.getDefault().getTable("JETSONNANO")

    # Get the biggest contor and process.
    if pipeline.filter_contours_output:
        m_MAXCnt = sorted(pipeline.filter_contours_output,
                          key=cv2.contourArea)[0]
        # Find the bounding boxes of the contour to get x, y, width, and height.
        x, y, w, h = cv2.boundingRect(m_MAXCnt)

        # Dilver data to roborio by NetworkTable.
        m_table.putBoolean("IsHaveTarget", True)
        m_table.putNumber("SpinError", (x + w / 2) - showFrame.shape[1]/2)

        # Show the image when you need it.
        cv2.drawContours(showFrame, m_MAXCnt, -1, 255, 3)
        cv2.circle(
            showFrame, (int(x + w / 2), int(y + h / 2)), 3, (0, 0, 255), -1)
        print("Target Get!!! ", "X:", int(x + w / 2), "Y:",
              int(y + h / 2), "Error:", (x + w / 2) - showFrame.shape[1]/2)

    else:
        m_table.putBoolean("IsHaveTarget", False)
        print("Target Lose QvQ")
def main():
    image_address = str(input('Enter the image name: '))
    image_address = "pool_Images/" + image_address
    print(image_address) 
    ball_coord, cue_coord, stick_coord = detection.detect_coordinates2(image_address)
    #print(cue_coord, ball_coord, stick_coord)
    if len(cue_coord) == 0 or len(stick_coord) == 0:
        print("No point detected")
        return

    cue_point = Point(cue_coord[0], cue_coord[1])
    stick_point = Point(stick_coord[0], stick_coord[1])
    path_of_white_ball(cue_point, stick_coord, cue_coord[2])
    #path_of_white_ball(p1, p2, RADIUS)
    future_point, collision_ball_info  = during_collision(cue_point, cue_coord[2],stick_point,ball_coord)
    if future_point == cue_point:
        print("No collision")
        return 
    else:
        frame = cv.imread(image_address, 1)
        cv.circle(frame, (int(collision_ball_info[0]), int(collision_ball_info[1])), 2*int(collision_ball_info[2]), (0, 255, 255), 2)
        cv.circle(frame, (int(future_point.x), int(future_point.y)), int(cue_coord[2]), (255, 255, 255), -1)
        temp_point = Point(collision_ball_info[0],collision_ball_info[1])
        colored_future_point = intersection(Ray(future_point,temp_point),Circle(temp_point,collision_ball_info[2]*5)) 
        cv.line(frame, (cue_point.x, cue_point.y), (future_point.x, future_point.y), (255, 255, 255), thickness=2)
        cv.line(frame, ((int)(collision_ball_info[0]), (int)(collision_ball_info[1])), ((int)(colored_future_point[0].x), (int)(colored_future_point[0].y)), (0, 255, 0), thickness=2)
        while 1:
            cv.namedWindow('Image',cv.WND_PROP_FULLSCREEN)
            cv.imshow('Image', frame)
            if cv.waitKey(1) & 0xFF == 27:
                break
        cv.destroyAllWindows()
def on_mouse(event, x, y, flags, img):
    global point1, point2
    img2 = img.copy()
    if event == cv2.EVENT_LBUTTONDOWN:  # 左键点击
        point1 = (x, y)
        cv2.circle(img2, point1, 10, (0, 255, 0), 5)
        cv2.imshow('image', img2)
    # 按住左键拖曳
    elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON):
        cv2.rectangle(img2, point1, (x, y), (255, 0, 0), 5)
        cv2.imshow('image', img2)
    elif event == cv2.EVENT_LBUTTONUP:  # 左键释放
        point2 = (x, y)
        cv2.rectangle(img2, point1, point2, (0, 0, 255), 5)
        cv2.imshow('image', img2)
        min_x = min(point1[0], point2[0])
        min_y = min(point1[1], point2[1])
        width = abs(point1[0] - point2[0])
        height = abs(point1[1] - point2[1])
        cut_img = img[min_y:min_y+height, min_x:min_x+width]
        # cv2.imwrite('500.jpg', cut_img)
        # 显示RGB三个通道的图像

        # 方法一:显示在一张图上
        # 按R、G、B三个通道分别计算颜色直方图
        b_hist = cv2.calcHist([cut_img], [0], None, [256], [0, 256])
        g_hist = cv2.calcHist([cut_img], [1], None, [256], [0, 256])
        r_hist = cv2.calcHist([cut_img], [2], None, [256], [0, 256])
        # 显示3个通道的颜色直方图
        plt.plot(b_hist, label='B', color='blue')
        plt.plot(g_hist, label='G', color='green')
        plt.plot(r_hist, label='R', color='red')
        plt.legend(loc='best')
        plt.xlim([0, 256])
        plt.show()
Beispiel #14
0
def findend():
    global endpoint

    hsv_image = cv2.cvtColor(img_maze, cv2.COLOR_BGR2HSV)
  
   # define range of red color in HSV
    lower_red = np.array([0,100,100])
    upper_red = np.array([135,255,255])
   
    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange(hsv_image, lower_red, upper_red)
    

    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(hsv_image,hsv_image, mask= mask)
    cimg=res
    res=cv2.cvtColor(res, cv2.COLOR_RGB2GRAY)

    circles = cv2.HoughCircles(res,cv2.HOUGH_GRADIENT,1,30,param1=50,param2=10,minRadius=0,maxRadius=0)
    #circles = np.uint16(np.around(circles))

    for i in circles[0,:]:
    # draw the outer circle
        cv2.circle(cimg,(i[0],i[1]),i[2],(255,0,0),2)
     # draw the center of the circle
        cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
        endpoint = (i[0],i[1])
Beispiel #15
0
def calibrationFunction(event, x, y, flags, params):

    global unitDistance

    pointsList = params[0]
    if (event == cv2.EVENT_LBUTTONDOWN
            and len(pointsList) < 2):  #Left Click and <2points exist

        #Draw Point
        cv2.circle(img, (x, y), 2, (0, 0, 255), cv2.FILLED)

        #Add to points list
        pointsList.append((x, y))
        print("Point drawn at:" + str(x) + "," + str(y))
        print(pointsList)

        if len(pointsList) == 2:  #If 2 points drawn
            #Draw Line between two points
            cv2.line(img, pointsList[0], pointsList[1], (0, 0, 255), 2)
            userDistance = input(
                "Calibration Line drawn, please enter distance: ")

            pixelDistance = math.sqrt(
                ((pointsList[0][0] - pointsList[1][0])**2 +
                 (pointsList[0][1] - pointsList[1][1])**2))
            unitDistance = float(userDistance) / pixelDistance

            print(
                "Calibration Complete, please press E to exit calibration mode"
            )
Beispiel #16
0
def measure_object(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
    print("threshold value:%s" % ret)
    cv.imshow("binary image", binary)
    dst = cv.cvtColor(binary, cv.COLOR_GRAY2BGR)
    contours, hireachy = cv.findContours(binary, cv.RETR_EXTERNAL,
                                         cv.CHAIN_APPROX_SIMPLE)
    for i, contour in enumerate(contours):
        area = cv.contourArea(contour)
        x, y, w, h = cv.boundingRect(contour)
        mm = cv.moments(contour)
        if mm['m00']:
            cx = mm['m10'] / mm['m00']
            cy = mm['m01'] / mm['m00']
        else:
            continue
        cv.circle(dst, (np.int(cx), np.int(cy)), 3, (0, 0, 255), -1)
        cv.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)  #绘制矩形边界
        """
        print("area",area)
        approxCurve = cv.approxPolyDP(contour,4,True)#多边形逼近
        if approxCurve.shape[0] > 6:#边数大于6条的
            cv.drawContours(dst,contours,i,(0,0,255),2)
        if approxCurve.shape[0] == 4:#矩形
            cv.drawContours(dst,contours,i,(0,255,0),2)
        if approxCurve.shape[0] == 3:#三角形
            cv.drawContours(dst,contours,i,(255,0,0),2)
        """
    cv.imshow("image", dst)
    cv.imshow("souce", image)
Beispiel #17
0
def timeNotes(x,
              y,
              w,
              h,
              ith_centroids,
              y_offset,
              note_height,
              keyboard_line,
              keyboard_array,
              keys_timed_update,
              elapsed,
              output_img,
              font=cv2.FONT_HERSHEY_SIMPLEX,
              font_scale=0.5,
              font_color=(0, 0, 255)):
    # x,y,w,h - respective x-coord, y-coord, width and height of a connected component
    # ith_centroids - the centroid of a given note (i.e. an x,y pair)
    # y_offset - if the keyboard has been offset in the y direction (this allows the detection to stay out of the top/bottom edges)
    # note_height - from connectedcomponents
    # keyboard_line - the y-coordinate that serves as the threshold/line to calculate press down/up
    # keyboard_array - the matrix of keyboard notes/octaves and x-coordinates
    # key_timed_update - our array that holds on/off button for each note in our keyboard
    # elapsed - time in seconds
    # output_img - the screen we want to display to

    lag = 2  # number of pixels about the keyboard_line for detection

    # Get centroid of detected note
    (cX, cY) = ith_centroids
    cY = cY + y_offset  # We cropped out the first 20 pixels

    # Y-coordinate of top and bottom edges of a note (this is truncated by the "size" of the detection area)
    dist_to_edge = note_height / 2  #getting the distance from centroid to bottom edge for better detection later on
    top_dot = cY - dist_to_edge
    bottom_dot = cY + dist_to_edge

    #     note = Note(cX, cY+dist_to_edge) #creating note object and adding to list

    if ((int(bottom_dot) >= int(keyboard_line - lag))
            and (int(bottom_dot) <= int(keyboard_line))):
        note_played, index = key_pressed(keyboard_array, cX)
        keys_timed_update[index].append([elapsed])
#         print(note_played)
#         print('='*50)

    if ((int(top_dot) >= int(keyboard_line - lag))
            and (int(top_dot) <= int(keyboard_line))):
        note_played, index = key_pressed(keyboard_array, cX)
        keys_timed_update[index].append([elapsed])

    note_played, _ = key_pressed(keyboard_array, cX)
    cv2.line(output_img, (0, int(keyboard_line)), (600, int(keyboard_line)),
             (0, 0, 255), 2)
    cv2.rectangle(output_img, (x, y), (x + w, y + h), font_color, 1)
    cv2.circle(output_img, (int(cX), int(bottom_dot)), 1, (0, 122, 255), 3)
    cv2.circle(output_img, (int(cX), int(top_dot)), 1, (0, 122, 255), 3)
    cv2.putText(output_img, note_played, (int(cX), int(cY + dist_to_edge)),
                font, font_scale, font_color, 1)

    return keys_timed_update
Beispiel #18
0
def func(ImgNo, defThr=127):
    img = cv2.imread(impDef.select_img(ImgNo))
    img1 = img.copy()
    imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    ret, thr = cv2.threshold(imgray, defThr, 255, 0)
    contours, _ = cv2.findContours(thr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    cnt = contours[0]

    # Convex Hull만을 찾기 위해 contours 1개만을 인자로 입력
    hull = cv2.convexHull(cnt)
    cv2.drawContours(img, [hull], 0, (0, 0, 255), 2)

    # Convexity Defects를 찾기위해 2번째 인자로 returnPoints = False를 지정해야 함
    # (cnt, returnPoints = False) 인자로 주어진 cnt를 분석하여 Convex Hull을 이루는 모든 좌표를 리턴하는 것이 아니라,
    # 원래 Contour와 Convex Hull이 만나는 부부의 Contour 인덱스를 리턴함.
    # 즉 별의 꼭지점에 해당하는 5군데를 리턴함.
    hull = cv2.convexHull(cnt, returnPoints=False)
    defects = cv2.convexityDefects(cnt, hull)

    for i in range(defects.shape[0]):
        sp, ep, fp, dist = defects[i, 0]
        start = tuple(cnt[sp][0])
        end = tuple(cnt[ep][0])
        farthest = tuple(cnt[fp][0])

        cv2.circle(img, farthest, 5, (0, 255, 0), -1)

    cv2.imshow('defects', img)
    impDef.close_window()
Beispiel #19
0
def linie_erkennen(dieser_frame, schwarze_maske, offset):
    yoffset = PIXEL_HOEHE - SLICE_HOEHE_FUER_SCHWARZ - offset - 1
    roi_line = schwarze_maske[yoffset:yoffset + SLICE_HOEHE_FUER_SCHWARZ,
                              0:PIXEL_BREITE - 1]
    _, konturen, _ = cv2.findContours(roi_line, cv2.RETR_TREE,
                                      cv2.CHAIN_APPROX_SIMPLE)
    cx = 0
    cy = yoffset + SLICE_HOEHE_FUER_SCHWARZ / 2
    cnt = 0
    farbe = GRUEN
    linien = []
    for kontur in konturen:
        x, y, w, h = cv2.boundingRect(kontur)
        if w > 50:
            if w >= PIXEL_BREITE - 50:
                farbe = ROT
                ist_kreuzung = True
            else:
                farbe = GELB
                ist_kreuzung = False
            cv2.rectangle(dieser_frame, (x, y + yoffset),
                          (x + w, y + yoffset + h), farbe, 2)
            cx = cx + int(x + w / 2)
            cnt = cnt + 1
            if ist_kreuzung:
                linien.append((True, cx - PIXEL_BREITE / 2))
            else:
                linien.append((False, cx - PIXEL_BREITE / 2))
    if cx is 0:
        cx = (PIXEL_BREITE - 1) / 2
    else:
        cx = cx / cnt
    cv2.circle(dieser_frame, (int(cx), int(cy)), 5, farbe, -1)
    return linien
Beispiel #20
0
def PPT(ImgNo, defThr=127):
    img = cv2.imread(impDef.select_img(ImgNo))
    img2 = img.copy()
    imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    ret, thr = cv2.threshold(imgray, defThr, 255, 0)
    contours, _ = cv2.findContours(thr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    cnt = contours[0]
    cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)  # contour를 녹색으로 그림

    outside = (55, 70)
    inside = (140, 150)

    # (55, 70)으로 설정된 outside와 (140, 150)으로 설정된 inside에 대해 별보양의 Contour까지 최단거리를 구함
    # cv2.pointPolygonTest()함수의 3번째 인자가 True로 설정되면 Contour와 점 사이의 최단거리를 리턴
    # cv2.pointPolygonTest()함수의 3번째 인자가 False로 설정되면 주어진 점이 Contour의 외부에 있으면 -1, Contour위에 있으면 0,
    # Contour내부에 있으면 1을 리턴
    dist1 = cv2.pointPolygonTest(cnt, outside, True)
    dist2 = cv2.pointPolygonTest(cnt, inside, True)

    print('Contour에서 (%d, %d)까지의 거리 : %.3f' % (outside[0], outside[1], dist1))
    print('Contour에서 (%d, %d)까지의 거리 : %.3f' % (inside[0], inside[1], dist2))

    cv2.circle(img, outside, 3, (0, 255, 255), -1)  # 노란색 점
    cv2.circle(img, inside, 3, (255, 0, 255), -1)  # 분홍색 점

    cv2.imshow('defects', img)
    impDef.close_window()
def show_img_with_rect(dst_img,
                       rect=None,
                       frame_id=None,
                       particles=None,
                       save_dir=None):
    tmp_img = np.array(dst_img)
    if len(tmp_img.shape) == 2:
        tmp_img = cv2.cvtColor(tmp_img, cv2.COLOR_GRAY2BGR)

    if rect is not None:
        cv2.rectangle(tmp_img, (rect.ly, rect.lx),
                      (rect.ly + rect.h, rect.lx + rect.w), (255, 0, 0), 1)
    if frame_id is not None:
        text = 'Frame: {:03d}'.format(frame_id)
        cv2.putText(tmp_img, text, (rect.ly, rect.lx), cv2.FONT_HERSHEY_DUPLEX,
                    1, (0, 0, 255), 2)

    if particles is not None:
        if not isinstance(particles, list):  # Single particle
            particles = [particles]
        for particle in particles:
            cv2.circle(tmp_img, (int(particle.cy), int(particle.cx)), 1,
                       (0, 255, 0), -1)
    # plt.imshow(tmp_img)
    # plt.axis('off')
    # plt.show()
    cv2.imshow('img', tmp_img)
    cv2.waitKey(50)

    if save_dir is not None:
        cv2.imwrite(save_dir / '{:03d}.png'.format(frame_id), tmp_img)
Beispiel #22
0
def draw_landmark(img_im, land):

    img = copy.deepcopy(img_im)
    n = numpy.array(land)
    for i in range(len(land)):
        cv2.circle(img, (int(n[i][0]), int(n[i][1])), 2, (0, 0, 255), -1)
    cv2.imshow('aaa', img)
    def getOrientation(self, contour, img):
        sz = len(contour)
        data_pts = np.empty((sz, 2), dtype=np.float64)
        for i in range(data_pts.shape[0]):
            data_pts[i, 0] = contour[i, 0, 0]
            data_pts[i, 1] = contour[i, 0, 1]

        # Perform PCA analysis
        mean = np.empty((0))
        mean, eigenvectors, eigenvalues = cv2.PCACompute2(data_pts, mean)

        # Store the center of the object
        cntr = (int(mean[0, 0]), int(mean[0, 1]))
        ## [pca]

        ## [visualization]
        # Draw the principal components
        cv2.circle(img, cntr, 3, (255, 0, 255), 2)
        p1 = (cntr[0] + 0.02 * eigenvectors[0, 0] * eigenvalues[0, 0],
              cntr[1] + 0.02 * eigenvectors[0, 1] * eigenvalues[0, 0])
        p2 = (cntr[0] - 0.02 * eigenvectors[1, 0] * eigenvalues[1, 0],
              cntr[1] - 0.02 * eigenvectors[1, 1] * eigenvalues[1, 0])
        self.drawAxis(img, cntr, p1, (0, 255, 0), 1)
        self.drawAxis(img, cntr, p2, (255, 255, 0), 5)

        angle = math.atan2(
            eigenvectors[0, 1],
            eigenvectors[0, 0]) * 180 / math.pi  # orientation in degrees
        ## [visualization]

        return angle
Beispiel #24
0
def line_slice(roi_kreuzung, dieser_frame, offset):
    """
    Zeichnet auf auf einem Schnit am gegebenen Y-Offset im Kamerabild die gefundenen
    Linienstückchen als grüne Rechtecke ein.
    Wird die eine Linie zu breit, wird das Rechteck rot eingefärbt.
    Das Zentrum aller Linienstückchen in einem Schnitt wird mit einem Punkt markiert.
    """
    yoffset = PIXEL_HOEHE - (PIXEL_HOEHE - 200) - 20 - offset
    # Horizontalen Streifen an y-Position "offset" herausschneiden
    roi_line = dieser_frame[PIXEL_HOEHE - 20 - offset:PIXEL_HOEHE - 1 - offset,
                            0:PIXEL_BREITE - 1]
    # in HSV-Farbschema umwandeln
    hsv = cv2.cvtColor(roi_line, cv2.COLOR_BGR2HSV)
    # Maske erstellen (Monochromes Bild: Linie weiß, alles andere schwarz)
    black_mask = cv2.inRange(hsv, black_lower_limit, black_upper_limit)
    # Konturen extrahieren
    _, konturen, _ = cv2.findContours(black_mask, cv2.RETR_TREE,
                                      cv2.CHAIN_APPROX_SIMPLE)
    cx = 0
    cy = yoffset + 5
    cnt = 0
    farbe = GRUEN
    # Liste der X-Positionen der Zentren der gefundenen Linienstücke
    ret = []
    is_kreuzung = False
    for kontur in konturen:
        # Rechteck um die Kontur erhalten
        x, y, w, h = cv2.boundingRect(kontur)
        # zu kleine Konturen wegfiltern
        if w > 10:
            # zu große Konturen rot einfärben
            if w > 150:
                farbe = ROT
                is_kreuzung = True
            # sonst grün einfärben
            else:
                farbe = GRUEN
            # Rechteck um die Kontur zeichnen
            cv2.rectangle(roi_kreuzung, (x, y + yoffset),
                          (x + w, y + yoffset + h), farbe, 2)
            # Summe aller x-Positionen der Zentren der gefundenen Rechtecke bilden
            cx = cx + int(x + w / 2)
            # Anzahl der gefundenen Rechecke mitzählen
            cnt = cnt + 1
            # Rote Rechtecke: X-Position ist 0 (Mitte des Kamerabildes)
            if is_kreuzung:
                ret.append(0)
            # Grüne Rechtecke: Abweichung von Bildmitte an Liste anfügen
            else:
                ret.append(cx - PIXEL_BREITE / 2)
    # keine Linienstücke gefunden: Durchnitt aller X-Positionen ist Bildmitte
    if cx is 0:
        cx = (PIXEL_BREITE - 1) / 2
    # Linienstückchen gefunden: Durchschnitt aller X-Positionen errechnen
    else:
        cx = cx / cnt
    # Kreis zeichnen an durchschnittlicher X-Position aller gefundenen Linienstückchen
    cv2.circle(roi_kreuzung, (int(cx), int(cy)), 5, farbe, -1)
    # Ergebnisliste zurückgeben: Liste der Abweichung der Linie von Mitte in Pixel
    return ret
Beispiel #25
0
def show_correspondence_lines(imgA, imgB, X1, Y1, X2, Y2, line_colors=None):
    """
    Visualizes corresponding points between two images by drawing a line segment
    between the two images for each (x1,y1) (x2,y2) pair.

    Args:
    - imgA: A numpy array of shape (M,N,3)
    - imgB: A numpy array of shape (D,E,3)
    - x1: A numpy array of shape (k,) containing x-locations of keypoints in imgA
    - y1: A numpy array of shape (k,) containing y-locations of keypoints in imgA
    - x2: A numpy array of shape (j,) containing x-locations of keypoints in imgB
    - y2: A numpy array of shape (j,) containing y-locations of keypoints in imgB
    - line_colors: A numpy array of shape (N x 3) with colors of correspondence lines (optional)

    Returns:
    - newImg: A numpy array of shape (max(M,D), N+E, 3)
    """
    newImg = hstack_images(imgA, imgB)
    shiftX = imgA.shape[1]
    X1 = X1.astype(np.int)
    Y1 = Y1.astype(np.int)
    X2 = X2.astype(np.int)
    Y2 = Y2.astype(np.int)

    dot_colors = np.random.rand(len(X1), 3)
    if line_colors is None:
        line_colors = dot_colors

    for x1, y1, x2, y2, dot_color, line_color in zip(X1, Y1, X2, Y2,
                                                     dot_colors, line_colors):
        newImg = cv2.circle(newImg, (x1, y1), 5, dot_color, -1)
        newImg = cv2.circle(newImg, (x2 + shiftX, y2), 5, dot_color, -1)
        newImg = cv2.line(newImg, (x1, y1), (x2 + shiftX, y2), line_color, 2,
                          cv2.LINE_AA)
    return newImg
Beispiel #26
0
def NumberOfFingers(defects, l):
    try:
        for i in range(defects.shape[0]):
            s,e,f,d = defects[i,0]
            start = tuple(approx[s][0])
            end = tuple(approx[e][0])
            far = tuple(approx[f][0])
            
            # Find length of all sides of triangle
            a = math.sqrt((end[0] - start[0])**2 + (end[1] - start[1])**2)
            b = math.sqrt((far[0] - start[0])**2 + (far[1] - start[1])**2)
            c = math.sqrt((end[0] - far[0])**2 + (end[1] - far[1])**2)
            s = (a+b+c)/2
            ar = math.sqrt(s*(s-a)*(s-b)*(s-c))
            
            d=(2*ar)/a
            
            # Cosine Rule for angle
            angle = math.acos((b**2 + c**2 - a**2)/(2*b*c))*(180/math.pi)        
        
            # Ignoring angles > 90 and ignore points very close to convex hull
            if angle <= 70 and d > 30:
                l += 1
                cv2.circle(frame, far, 3, [255,0,0], -1)
    except AttributeError:
        pass
    return l    
Beispiel #27
0
    def traffic_lights_masks(self,
                             traffic_lights: List[carla.Actor]) -> Tuple[Mask]:
        red_light_canvas = self.make_empty_mask()
        yellow_light_canvas = self.make_empty_mask()
        green_light_canvas = self.make_empty_mask()
        tls = carla.TrafficLightState
        for tl in traffic_lights:
            world_pos = tl.get_location()
            pos = self.location_to_pixel(world_pos)
            radius = int(self.pixels_per_meter * 1.2)
            if tl.state == tls.Red:
                target_canvas = red_light_canvas
            elif tl.state == tls.Yellow:
                target_canvas = yellow_light_canvas
            elif tl.state == tls.Green:
                target_canvas = green_light_canvas
            else:
                # Unknown or off traffic light
                continue

            cv.circle(
                img=target_canvas,
                center=pos,
                radius=radius,
                color=COLOR_ON,
                thickness=cv.FILLED,
            )
        return red_light_canvas, yellow_light_canvas, green_light_canvas
 def visualizeWaypoints(self, waypoints, start_idx=None, show_wp_num=False):
     pac_dots = self._img.copy()
     pac_dots[:, :] = 0
     pac_dots[waypoints[:, 0], waypoints[:, 1]] = 1
     num_dilations = 1
     kernel = np.ones((2, 2), np.uint8)
     pac_dots = cv2.dilate(pac_dots, kernel, iterations=num_dilations)
     img = pac_dots + self._safety_img
     img_color = img[..., None] * np.array([1, 1, 1])
     if start_idx is not None:
         try:
             for agent in range(len(start_idx)):
                 color = self._path_colors[(((agent - 1) * -1) + 1) %
                                           self._num_colors]
                 cv2.circle(img_color,
                            (waypoints[start_idx[agent],
                                       1], waypoints[start_idx[agent], 0]),
                            5, color)
         except:
             print("INVALID STARTING LOCATIONS")
     plt.imshow(img_color)
     plt.xticks([])
     plt.yticks([])
     if show_wp_num:
         for ii, wp in enumerate(waypoints):
             plt.text(wp[1], wp[0], str(ii), color='red', fontsize=8)
         # cv2.putText(img_color,str(ii),(wp[1],wp[0]),cv2.FONT_HERSHEY_SIMPLEX,0.25,(255,255,255))
     plt.show()
Beispiel #29
0
    def convexityDetectionImg(self):
        try:
            img = cv2.imread(self.filename)
            img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            ret, thresh = cv2.threshold(img_gray, 127, 255, 0)
            contours, hierarchy = cv2.findContours(thresh, 2, 1)
            cnt = contours[0]

            hull = cv2.convexHull(cnt, returnPoints=False)
            defects = cv2.convexityDefects(cnt, hull)

            for i in range(defects.shape[0]):
                s, e, f, d = defects[i, 0]
                start = tuple(cnt[s][0])
                end = tuple(cnt[e][0])
                far = tuple(cnt[f][0])
                cv2.line(img, start, end, [0, 255, 0], 2)
                cv2.circle(img, far, 5, [0, 0, 255], -1)

            cv2.imwrite('img/dist/disbukey.png', img,
                        [cv2.IMWRITE_JPEG_QUALITY, 100])
            cv2.waitKey(0)
            cv2.destroyAllWindows()
        except:
            pass
Beispiel #30
0
def linie_erkennen_links(dieser_frame, schwarze_maske):
    global cy2
    global cx2
    global LS
    roi_line = schwarze_maske[:PIXEL_HOEHE - 1, :SLICE_BREITE_FUER_SCHWARZ]
    _, konturen, _ = cv2.findContours(roi_line, cv2.RETR_TREE,
                                      cv2.CHAIN_APPROX_SIMPLE)
    cy2 = 0
    cx2 = SLICE_BREITE_FUER_SCHWARZ_1 / 2
    cnt = 0
    farbe = GRUEN
    linien = []
    for kontur in konturen:
        x2, y2, w2, h2 = cv2.boundingRect(kontur)
        if h2 > 25:
            if h2 >= PIXEL_HOEHE - 50:
                farbe = ROT
                ist_kreuzung = True
            else:
                farbe = GELB
                ist_kreuzung = False
            cv2.rectangle(dieser_frame, (x2, y2), (x2 + w2, y2 + h2), farbe, 2)
            cy2 = cy2 + int(y2 + h2 / 2)
            cnt = cnt + 1
            if ist_kreuzung:
                linien.append((True, cy2 - PIXEL_HOEHE / 2))
            else:
                linien.append((False, cy2 - PIXEL_HOEHE / 2))
    if cy2 is 0:
        LS = False
    else:
        LS = True
        cy2 = cy2 / cnt
        cv2.circle(dieser_frame, POINT_2, 5, farbe, -1)
    return linien