示例#1
0
def userinit(x,img):
    
    global polygon
    

    w,h = 1360,768 
    img = rg.resize(img,w,h)
    imgw = img.shape[1]
    imgh = img.shape[0]
    canvasimg = np.array(img)
    minx = abs(min(x[:len(x)/2]))
    miny = abs(min(x[len(x)/2:]))
    
    # transform model points to image coord
    pts = zip(x[:len(x)/2],x[len(x)/2:])
    pts = [((point[0]+minx)/3.2,(point[1]+miny)/3.2) for point in pts]
    polygon = pts
    pinimg = [(int(y[0]*imgw),int(y[1]*imgh)) for y in pts]
    pinimg = np.array(np.array(pinimg).tolist(), np.int32)
    pinimg = pinimg.reshape((-1,1,2))
    cv2.polylines(img,[pinimg],True,(0,0,255))
    
    #show gui
    cv2.imshow('choose',img)
    cv.SetMouseCallback( 'choose', mouse, canvasimg)
    cv2.waitKey(0)
    
    return [el for tple in zip(*polygon) for el in tple]
示例#2
0
def LOOFIT(k,m,inciset,enhancedgraphs, colors):
 
    for i in range(14):
        print "INCISOR: "+str(inciset[0]) + " LEFTOUT: "+str(i)
        trainingset = range(14)
        trainingset.remove(i)
        mask = segment(k,m,inciset,trainingset,enhancedgraphs, colors, i, 1)
        # save mask
        cv2.imwrite("fits/fit-incis"+str(inciset[0])+"-leftout"+str(i)+".png",rg.resize(mask,3023,1597))
示例#3
0
def plotinit(est, img, colors, imgidx):
    colors = [(0,0,255),(50,50,255),(100,100,255),(150,150,255),(255,0,0),(255,50,50),(255,100,100),(255,150,150)]
    dw,dh = 1360,768 
    image = rg.resize(cv2.cvtColor(img,cv.CV_GRAY2RGB),dw,dh)
    sx = (image.shape[1])
    sy = (image.shape[0])
    
    for i in range(len(est)):
        p = [[int(q[0]*(sx)),int(q[1]*(sy))] for q in zip(est[i][:len(est[i])/2],est[i][len(est[i])/2:])]
        cv2.polylines(image,np.int32(np.array([p])),True,colors[i])
        
    cv2.imwrite("init/"+str(imgidx)+".png",image)
示例#4
0
def init(landmarks, img):
    """Allows the user to provide an initial fit for the given model in the
    given image by dragging the mean shape to the right position on a dental
    radiograph.

    Args:
        landmarks (Landmark): A model.
        img (nparray): An image to fit the model on.

    Returns:
        The centroid of the manual fit.
        The landmark points, adapted to the position chosen by the user and the
        scale of the image.

    """
    global tooth

    oimgh = img.shape[0]
    img, scale = rg.resize(img, 1200, 800)
    imgh = img.shape[0]
    canvasimg = np.array(img)

    # transform model points to image coord
    points = landmarks.as_matrix()
    min_x = abs(points[:, 0].min())
    min_y = abs(points[:, 1].min())
    points = [((point[0] + min_x) * scale, (point[1] + min_y) * scale)
              for point in points]
    tooth = points
    pimg = np.array([(int(p[0] * imgh), int(p[1] * imgh)) for p in points])
    cv2.polylines(img, [pimg], True, (0, 255, 0))

    # show gui
    cv2.imshow('choose', img)
    cv.SetMouseCallback('choose', __mouse, canvasimg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    centroid = np.mean(tooth, axis=0)

    return centroid, Landmarks(
        np.array([[point[0] * oimgh, point[1] * oimgh] for point in tooth]))
def init(landmarks, img):
    """Allows the user to provide an initial fit for the given model in the
    given image by dragging the mean shape to the right position on a dental
    radiograph.

    Args:
        landmarks (Landmark): A model.
        img (nparray): An image to fit the model on.

    Returns:
        The centroid of the manual fit.
        The landmark points, adapted to the position chosen by the user and the
        scale of the image.

    """
    global tooth

    oimgh = img.shape[0]
    img, scale = rg.resize(img, 1200, 800)
    imgh = img.shape[0]
    canvasimg = np.array(img)

    # transform model points to image coord
    points = landmarks.as_matrix()
    min_x = abs(points[:, 0].min())
    min_y = abs(points[:, 1].min())
    points = [((point[0]+min_x)*scale, (point[1]+min_y)*scale) for point in points]
    tooth = points
    pimg = np.array([(int(p[0]*imgh), int(p[1]*imgh)) for p in points])
    cv2.polylines(img, [pimg], True, (0, 255, 0))

    # show gui
    cv2.imshow('choose', img)
    cv.SetMouseCallback('choose', __mouse, canvasimg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    centroid = np.mean(tooth, axis=0)

    return centroid, Landmarks(np.array([[point[0]*oimgh, point[1]*oimgh] for point in tooth]))
def create_database(radiographs):
    """Interactively select rectangle ROIs for the four upper/lower incisors
    and store list of bboxes.

    Args:
        radiographs: A list with dental radiographs.

    .. _Based on:
        http://stackoverflow.com/a/36642507
    """
    def draw_rect_roi(event, x, y, flags, param):
        """Mouse callback function"""
        # grab references to the global variables
        global rect_bbox, rect_endpoint_tmp, drawing, bbox

        # if the left mouse button was clicked, record the starting
        # (x, y) coordinates and indicate that drawing is being
        # performed. set rect_endpoint_tmp empty list.
        if event == cv2.EVENT_LBUTTONDOWN:
            rect_endpoint_tmp = []
            rect_bbox = [(x, y)]
            drawing = True

        # check to see if the left mouse button was released
        elif event == cv2.EVENT_LBUTTONUP:
            # record the ending (x, y) coordinates and indicate that
            # drawing operation is finished
            rect_bbox.append((x, y))
            drawing = False

            # for bbox find upper left and bottom right points
            p_1, p_2 = rect_bbox
            p_1x, p_1y = p_1
            p_2x, p_2y = p_2

            lx = min(p_1x, p_2x)
            ty = min(p_1y, p_2y)
            rx = max(p_1x, p_2x)
            by = max(p_1y, p_2y)

            # add bbox to list if both points are different
            if (lx, ty) != (rx, by):
                bbox = [(lx, ty), (rx, by)]

        # if mouse is drawing set tmp rectangle endpoint to (x,y)
        elif event == cv2.EVENT_MOUSEMOVE and drawing:
            rect_endpoint_tmp = [(x, y)]

    # First do the upper incisors, next the upper incisors
    for is_lower in range(0, 2):
        if is_lower:
            print 'Select the region of the four lower incisors for each radiograph\n', \
                    'and press the c key when done or the d key to ignore the example.'
        else:
            print 'Select the region of the four upper incisors for each radiograph\n', \
                    'and press the c key when done or the d key to ignore the example.'

        bbox_list = []
        for ind, img in enumerate(radiographs):
            if is_lower:
                windowtitle = "Lower incisors [%d/%d]" % (
                    ind + 1,
                    len(radiographs),
                )
            else:
                windowtitle = "Upper incisors [%d/%d]" % (
                    ind + 1,
                    len(radiographs),
                )
            # clone image and setup the mouse callback function
            canvasimg = img.copy()
            # scale image to fit on screen
            canvasimg, scale = rg.resize(canvasimg, 1200, 800)
            cv2.namedWindow(windowtitle)
            cv2.setMouseCallback(windowtitle, draw_rect_roi)

            # keep looping until the 'c' key is pressed
            while True:
                # display the image and wait for a keypress
                rect_cpy = canvasimg.copy()
                if not drawing:
                    if bbox:
                        start_point = bbox[0]
                        end_point_tmp = bbox[1]
                        cv2.rectangle(rect_cpy, start_point, end_point_tmp,
                                      (0, 255, 0), 1)
                        cv2.imshow(windowtitle, rect_cpy)
                    else:
                        cv2.imshow(windowtitle, canvasimg)
                elif drawing and rect_endpoint_tmp:
                    start_point = rect_bbox[0]
                    end_point_tmp = rect_endpoint_tmp[0]
                    cv2.rectangle(rect_cpy, start_point, end_point_tmp,
                                  (0, 255, 0), 1)
                    cv2.imshow(windowtitle, rect_cpy)

                key = cv2.waitKey(1) & 0xFF
                # if the 'c' key is pressed, break from the loop, and store the example
                if key == ord('c'):
                    bbox_list.append(bbox)
                    break
                # if the 'd' key is pressed, break from the loop and ignore the example
                if key == ord('d'):
                    break
            # close all open windows
            cv2.destroyAllWindows()

        # rescale bounding boxes
        bbox_list = np.array([[(int(p[0] / scale), int(p[1] / scale))
                               for p in bb] for bb in bbox_list])

        # print results summary
        bbs = [bb[1] - bb[0] for bb in bbox_list]
        avg_width, avg_height = np.mean(bbs, axis=0)
        print 'Avg. height: ' + str(avg_height)
        print 'Avg. width: ' + str(avg_width)

        # save the results
        if is_lower:
            np.save('Models/lower_incisor_model', bbox_list)
        else:
            np.save('Models/upper_incisor_model', bbox_list)
def create_database(radiographs):
    """Interactively select rectangle ROIs for the four upper/lower incisors
    and store list of bboxes.

    Args:
        radiographs: A list with dental radiographs.

    .. _Based on:
        http://stackoverflow.com/a/36642507
    """
    def draw_rect_roi(event, x, y, flags, param):
        """Mouse callback function"""
        # grab references to the global variables
        global rect_bbox, rect_endpoint_tmp, drawing, bbox

        # if the left mouse button was clicked, record the starting
        # (x, y) coordinates and indicate that drawing is being
        # performed. set rect_endpoint_tmp empty list.
        if event == cv2.EVENT_LBUTTONDOWN:
            rect_endpoint_tmp = []
            rect_bbox = [(x, y)]
            drawing = True

        # check to see if the left mouse button was released
        elif event == cv2.EVENT_LBUTTONUP:
            # record the ending (x, y) coordinates and indicate that
            # drawing operation is finished
            rect_bbox.append((x, y))
            drawing = False

            # for bbox find upper left and bottom right points
            p_1, p_2 = rect_bbox
            p_1x, p_1y = p_1
            p_2x, p_2y = p_2

            lx = min(p_1x, p_2x)
            ty = min(p_1y, p_2y)
            rx = max(p_1x, p_2x)
            by = max(p_1y, p_2y)

            # add bbox to list if both points are different
            if (lx, ty) != (rx, by):
                bbox = [(lx, ty), (rx, by)]

        # if mouse is drawing set tmp rectangle endpoint to (x,y)
        elif event == cv2.EVENT_MOUSEMOVE and drawing:
            rect_endpoint_tmp = [(x, y)]

    # First do the upper incisors, next the upper incisors
    for is_lower in range(0, 2):
        if is_lower:
            print 'Select the region of the four lower incisors for each radiograph\n', \
                    'and press the c key when done or the d key to ignore the example.'
        else:
            print 'Select the region of the four upper incisors for each radiograph\n', \
                    'and press the c key when done or the d key to ignore the example.'

        bbox_list = []
        for ind, img in enumerate(radiographs):
            if is_lower:
                windowtitle = "Lower incisors [%d/%d]" % (ind+1, len(radiographs),)
            else:
                windowtitle = "Upper incisors [%d/%d]" % (ind+1, len(radiographs),)
            # clone image and setup the mouse callback function
            canvasimg = img.copy()
            # scale image to fit on screen
            canvasimg, scale = rg.resize(canvasimg, 1200, 800)
            cv2.namedWindow(windowtitle)
            cv2.setMouseCallback(windowtitle, draw_rect_roi)

            # keep looping until the 'c' key is pressed
            while True:
                # display the image and wait for a keypress
                rect_cpy = canvasimg.copy()
                if not drawing:
                    if bbox:
                        start_point = bbox[0]
                        end_point_tmp = bbox[1]
                        cv2.rectangle(rect_cpy, start_point, end_point_tmp, (0, 255, 0), 1)
                        cv2.imshow(windowtitle, rect_cpy)
                    else:
                        cv2.imshow(windowtitle, canvasimg)
                elif drawing and rect_endpoint_tmp:
                    start_point = rect_bbox[0]
                    end_point_tmp = rect_endpoint_tmp[0]
                    cv2.rectangle(rect_cpy, start_point, end_point_tmp, (0, 255, 0), 1)
                    cv2.imshow(windowtitle, rect_cpy)

                key = cv2.waitKey(1) & 0xFF
                # if the 'c' key is pressed, break from the loop, and store the example
                if key == ord('c'):
                    bbox_list.append(bbox)
                    break
                # if the 'd' key is pressed, break from the loop and ignore the example
                if key == ord('d'):
                    break
            # close all open windows
            cv2.destroyAllWindows()

        # rescale bounding boxes
        bbox_list = np.array([[(int(p[0]/scale), int(p[1]/scale))
                               for p in bb]
                              for bb in bbox_list])

        # print results summary
        bbs = [bb[1] - bb[0] for bb in bbox_list]
        avg_width, avg_height = np.mean(bbs, axis=0)
        print 'Avg. height: ' + str(avg_height)
        print 'Avg. width: ' + str(avg_width)

        # save the results
        if is_lower:
            np.save('Models/lower_incisor_model', bbox_list)
        else:
            np.save('Models/upper_incisor_model', bbox_list)