示例#1
0
def fit_ellipse_findContours(image, pred):
    #all 69.51ms

    i_sz = pred.shape
    pts = []
    #pdb.set_trace()
    _, p, hierarchy = cv2.findContours(pred, cv2.RETR_TREE,
                                       cv2.CHAIN_APPROX_SIMPLE)

    for i in range(len(p)):
        for j in range(len(p[i])):
            pts.append(p[i][j])

    pts_ = np.array(pts)
    if pts_.shape[0] > 5:
        #3.43ms/54.23ms
        ellipse_info_ = cv2.fitEllipse(pts_)
        if ellipse_info_[2] > 90:
            angle_ = 180
        else:
            angle_ = 0
        ellipse_info = (ellipse_info_[0], ellipse_info_[1], angle_)
        cv2.ellipse(image, ellipse_info, (0, 255, 0), 1)
        #print(ellipse_info)

        #11.8ms/57.67ms
        #***********************
        part_label = ellipse_my(ellipse_info)
        c_x = ellipse_info[0][0]
        c_y = ellipse_info[0][1]

        for i in range(len(part_label)):
            x = int(part_label[i][1])
            y = int(part_label[i][0])
            if y < len(image) and x < len(image[0]) and y >= 0 and x >= 0:
                image[y][x][0] = 0
                image[y][x][1] = 255
                image[y][x][2] = 0
                p_i_x, p_i_y, p_o_x, p_o_y = get_nine_half(c_x, c_y, x, y)
                #p_i_y,p_i_x,p_o_y,p_o_x=get_nine_half(c_x,c_y,x,y)
                cv2.line(image, (p_i_x, p_i_y), (p_o_x, p_o_y), (0, 255, 0), 1)
        #**********************
    else:
        pass
    '''
        for i in range(pts_.shape[0]):
            x=int(pts_[i][1])
            y=int(pts_[i][0])
            if y<len(image) and x<len(image[0]) and y >= 0 and x>=0: 
                image[y][x][0]=0
                image[y][x][1]=255
                image[y][x][2]=0'''

    size = (224, 224)
    image = cv2.resize(image, size, interpolation=cv2.INTER_AREA)

    return image
示例#2
0
def fit_ellipse(image, pred):
    #all 69.51ms

    i_sz = pred.shape
    pts = []

    #13.4ms/40.75ms
    for i in range(i_sz[0]):
        for j in range(i_sz[1]):
            if pred[i][j] != 0:
                pts.append([j, i])

    p = np.array(pts)
    if p.shape[0] > 5:

        #print('p',p)
        #3.43ms/54.23ms
        ellipse_info_ = cv2.fitEllipse(p)
        if ellipse_info_[2] > 90:
            angle_ = 180
        else:
            angle_ = 0
        ellipse_info = (ellipse_info_[0], ellipse_info_[1], angle_)
        cv2.ellipse(image, ellipse_info, (0, 255, 0), 1)
        #print(ellipse_info)

        #11.8ms/57.67ms
        #***********************
        part_label = ellipse_my(ellipse_info)
        c_x = ellipse_info[0][0]
        c_y = ellipse_info[0][1]

        for i in range(len(part_label)):
            x = int(part_label[i][1])
            y = int(part_label[i][0])
            if y < len(image) and x < len(image[0]):
                image[y][x][0] = 0
                image[y][x][1] = 255
                image[y][x][2] = 0
                p_i_x, p_i_y, p_o_x, p_o_y = get_nine_half(c_x, c_y, x, y)
                #p_i_y,p_i_x,p_o_y,p_o_x=get_nine_half(c_x,c_y,x,y)
                cv2.line(image, (p_i_x, p_i_y), (p_o_x, p_o_y), (0, 255, 0), 1)
        #**********************
        size = (224, 224)
        image = cv2.resize(image, size, interpolation=cv2.INTER_AREA)

    return image
示例#3
0
        lx, ly, rx, ry, w, h = _rect(gt[i])
        #print('lx:',lx,'ly:',ly,'rx',rx,'ry',ry,'w',w,'h',h)
        cx = lx + w / 2
        cy = ly + h / 2

        img_name_split = gt[i][0].split('/')
        str_l = len(img_name_split)
        img_name = os.path.join(
            img_folder1, img_name_split[str_l - 3] + img_name_split[str_l - 1])

        crops = cv2.resize(im, size, interpolation=cv2.INTER_AREA)
        cv2.imwrite(img_name, crops)

        box = [cx, cy, h / 2, w / 2]
        #print('box:',box)
        pts = ellipse_my(box, 0)
        for j in range(len(pts)):
            x = int(pts[j][0])
            y = int(pts[j][1])
            s_x, e_x, s_y, e_y = get_point(x, y)
            gt_im[s_y:e_y, s_x:e_x, :] = 1

        gt_img_name = os.path.join(
            gt_folder1, img_name_split[str_l - 3] + img_name_split[str_l - 1])
        gt_crops = cv2.resize(gt_im, size, interpolation=cv2.INTER_AREA)
        #print('gt_crops',gt_crops.shape)
        cv2.imwrite(gt_img_name, gt_crops)
        if i % 1000 == 0:
            print('Now is %d...' % i)
        '''line=img_name+' '+gt_img_name+' '+str(c_x)+' '+str(c_y)+' '+str(n_w)+' '+str(n_h)+' '+str(0)+'\n'
        #print(line)
 
 count = 1
 h_ = h
 w_ = w
 h_s = h
 w_s = w
 rate_pixel = 254
 for t in range(expand_num):
    h_ += count
    w_ += count
    h_s -= count
    w_s -= count
    tmp = rate_pixel - 1
    
    box_ = [cx,cy,h_/2,w_/2]
    pts_ = ellipse_my(box_,0)
    for j in range(len(pts_)):
        x=int(pts_[j][0])
        y=int(pts_[j][1])
        if y >=0 and x>=0 and y < frame_sz[0] and x<frame_sz[1]:
            gt_im[y][x][0]=tmp
    if h_s>0 and w_s>0:
        box_s = [cx,cy,h_s/2,w_s/2]
        pts_s = ellipse_my(box_s, 0)
        for j in range(len(pts_s)):
            x=int(pts_s[j][0])
            y=int(pts_s[j][1])
            if y >=0 and x>=0 and y < frame_sz[0] and x<frame_sz[1]:
                gt_im[y][x][0]=tmp
                 
    rate_pixel -= 1
示例#5
0
def fit_ellipse_findContours_ori(image, pred):
    #all 69.51ms
    i_sz = pred.shape
    im_sz = image.shape
    rate1 = im_sz[0] / i_sz[0]
    rate0 = im_sz[1] / i_sz[1]
    rate = [rate0, rate1]
    pts = []
    _, p, hierarchy = cv2.findContours(pred, cv2.RETR_TREE,
                                       cv2.CHAIN_APPROX_SIMPLE)

    for i in range(len(p)):
        for j in range(len(p[i])):
            pts.append(p[i][j])

    pts_ = np.array(pts)

    if pts_.shape[0] > 5:
        #3.43ms/54.23ms
        ellipse_info_ = cv2.fitEllipse(pts_)
        if ellipse_info_[2] > 90:
            angle_ = 180
        else:
            angle_ = 0
        ellipse_info_resize = (ellipse_info_[0], ellipse_info_[1], angle_)
        part_label = ellipse_my(ellipse_info_resize)

        ellipse_info_0 = tuple(np.array(ellipse_info_[0]) * rate)
        ellipse_info_1 = tuple(np.array(ellipse_info_[1]) * rate)
        ellipse_info = (ellipse_info_0, ellipse_info_1, angle_)
        cv2.ellipse(image, ellipse_info, (0, 255, 0), 1)
        #print(ellipse_info)

        #11.8ms/57.67ms
        #***********************
        #part_label=ellipse_my(ellipse_info)
        c_x = ellipse_info[0][0]
        c_y = ellipse_info[0][1]

        for i in range(len(part_label)):
            x = int(part_label[i][1] * rate0)
            y = int(part_label[i][0] * rate1)

            if y < len(image) and x < len(image[0]) and y >= 0 and x >= 0:
                image[y][x][0] = 0
                image[y][x][1] = 255
                image[y][x][2] = 0
                p_i_x, p_i_y, p_o_x, p_o_y = get_nine_half(c_x, c_y, x, y)
                #p_i_y,p_i_x,p_o_y,p_o_x=get_nine_half(c_x,c_y,x,y)
                cv2.line(image, (p_i_x, p_i_y), (p_o_x, p_o_y), (0, 255, 0), 1)
        #**********************
    else:
        pts_ = np.squeeze(pts_, axis=1)
        for i in range(pts_.shape[0]):
            x = int(pts_[i][1] * rate0)
            y = int(pts_[i][0] * rate1)
            if y < len(image) and x < len(image[0]) and y >= 0 and x >= 0:
                image[y][x][0] = 0
                image[y][x][1] = 255
                image[y][x][2] = 0

    return image
示例#6
0
    def divide_shelter_once(self,
                            im,
                            filename,
                            pred,
                            pred_pro,
                            gt_ellip,
                            if_valid=False,
                            is_save=True):
        '''
        divide the shelter and not shelter
        '''

        pts = []
        '''
        _, p, hierarchy = cv2.findContours(pred, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        #tmp draw contours
        sz = im.shape
        c_im_show = np.zeros((sz[0], sz[1], 3))
        cv2.drawContours(c_im_show, p, -1, (0, 255, 0), 1)
        for i in range(len(p)):
            for j in range(len(p[i])):
                pts.append(p[i][j])

        '''
        sz = im.shape
        im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
        #tmp
        fn = filename.strip().decode('utf-8')
        #fn_full = '%s%s.bmp' % ('img', fn.split('img')[1])
        #im_full = cv2.imread(os.path.join(cfgs.full_im_path, fn_full))
        #im = cv2.resize(im_full, (sz[1], sz[0]), interpolation=cv2.INTER_CUBIC)

        fn_full = '%s%s%s.bmp' % ('s8', 'img', fn.split('img')[1])
        fn_full_path = os.path.join(cfgs.full_im_path, fn_full)
        im_full = cv2.imread(fn_full_path)
        im = cv2.resize(im_full, (sz[1], sz[0]), interpolation=cv2.INTER_CUBIC)

        #pred = remove_small_area(pred)
        for ii in range(sz[0]):
            for jj in range(sz[1]):
                #if pred[ii][jj] > 0:
                if pred[ii][jj] == 2:
                    pts.append([jj, ii])
                    #im[ii][jj] = 255

        pts_ = np.array(pts)
        if pts_.shape[0] > 5:
            ellipse_info = cv2.fitEllipse(pts_)
            ellipse_info_ = ellipse_info
            #pred_ellip = np.array([ellipse_info[0][0], ellipse_info[0][1], ellipse_info[1][0], ellipse_info[1][1]])
            if ellipse_info[2] > 150 or ellipse_info[2] < 30:
                angle = 180
                pred_ellip = np.array([
                    ellipse_info[0][0], ellipse_info[0][1], ellipse_info[1][0],
                    ellipse_info[1][1]
                ])
            else:
                angle = 90
                pred_ellip = np.array([
                    ellipse_info[0][0], ellipse_info[0][1], ellipse_info[1][1],
                    ellipse_info[1][0]
                ])

            ellipse_info = (tuple(
                np.array([
                    ellipse_info[0][0], ellipse_info[0][1]
                ])), tuple(np.array([ellipse_info[1][0],
                                     ellipse_info[1][1]])), angle)
            #loss = self.haus_loss(pred_ellip, gt_ellip)
        else:
            pred_ellip = np.array([0, 0, 0, 0])
            ellipse_info = (tuple(np.array([0, 0])), tuple(np.array([0,
                                                                     0])), 0)
            #loss = self.ellip_loss(pred_ellip, gt_ellip)
        loss = 0
        if is_save:

            #save worse result
            error_path = cfgs.error_path
            #im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
            cv2.ellipse(im, ellipse_info, (0, 255, 0), 1)
            #gt_ellip_info = (tuple(np.array([gt_ellip[0], gt_ellip[1]])), tuple(np.array([gt_ellip[2], gt_ellip[3]])), 0)
            #cv2.ellipse(im,gt_ellip_info,(0,0,255),1)
            part_label = ellipse_my(ellipse_info)
            c_x = ellipse_info[0][0]
            c_y = ellipse_info[0][1]

            for i in range(len(part_label)):
                x = int(part_label[i][1])
                y = int(part_label[i][0])
                if y < len(im) and x < len(im[0]):
                    im[y][x][0] = 0
                    im[y][x][1] = 255
                    im[y][x][2] = 0
                    p_i_x, p_i_y, p_o_x, p_o_y = get_nine_half(c_x, c_y, x, y)
                    #p_i_y,p_i_x,p_o_y,p_o_x=get_nine_half(c_x,c_y,x,y)
                    cv2.line(im, (p_i_x, p_i_y), (p_o_x, p_o_y), (0, 255, 0),
                             1)

            if loss > cfgs.loss_thresh:
                if loss > 500:
                    loss = 500
                path_ = os.path.join(
                    error_path,
                    filename.strip().decode('utf-8') + '_' + str(int(loss)) +
                    '.bmp')
                cv2.imwrite(path_, im)
            else:
                path_ = os.path.join(
                    error_path + '_better',
                    filename.strip().decode('utf-8') + '_' + str(int(loss)) +
                    '.bmp')
                cv2.imwrite(path_, im)

        return loss