Пример #1
0
def main(frame_path, dataset_name):
    print(frame_path)
    filename, f_type = getBaseName(frame_path)
    createFolder('img')
    createFolder('img/out-simple')
    createFolder('img/out-simple/' + dataset_name)
    save_path = 'img/out/' + dataset_name + '/simple/'
    createFolder(save_path)

    img = cv2.imread(frame_path, 1)
    h, w, c = img.shape
    [b, g, r] = cv2.split(img)
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    img_H = img_hsv[:, :, 0]
    img_S = img_hsv[:, :, 1]
    img_V = img_hsv[:, :, 2]
    img_color_intensity = color_intensity(img)

    ### Light Detection
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) / 255.0
    ret, img_gray = cv2.threshold(img_gray, 0.4, 1, cv2.THRESH_TOZERO)
    #ret , img_gray_th = cv2.threshold(img_gray, 245/255, 1, cv2.THRESH_TOZERO)
    #ret, img_gray_th = cv2.threshold(img_gray, 245/255, 1, cv2.THRESH_TOZERO)
    #ret, img_color_intensity_th = cv2.threshold(img_color_intensity, 0.2, 1, cv2.THRESH_TOZERO)

    img_nakagami = Nakagami_image_enhancement(img_gray, 5) / 255.0
    img_nakagami_c = Nakagami_image_enhancement(img_color_intensity, 5) / 255.0

    ret, img_nakagami_norm_th = cv2.threshold(img_nakagami, 240 / 255, 1,
                                              cv2.THRESH_TOZERO)
    ret, img_nakagami_c_th = cv2.threshold(img_nakagami_c, 240 / 255, 1,
                                           cv2.THRESH_TOZERO)

    pipline = [
        'img', 'img_H', 'img_S', 'img_V', 'img_gray', 'img_color_intensity',
        'img_nakagami', 'img_nakagami_c', 'img_nakagami_norm_th',
        'img_nakagami_c_th'
    ]
    imgs = [
        img, img_H, img_S, img_V, img_gray, img_color_intensity, img_nakagami,
        img_nakagami_c, img_nakagami_norm_th, img_nakagami_c_th
    ]

    fig = plt.figure(figsize=(32, 64))

    for i in range(len(imgs)):
        ax = fig.add_subplot(len(imgs) // 2 + 1, 2, i + 1)
        img_tmp = BGR_to_RGB(imgs[i])
        ax.set_title(pipline[i])
        if pipline[i] in ['img_gray_th_', 'img_nakagami_']:
            plt.imshow(img_tmp)
            plt.colorbar()
        elif len(img_tmp.shape) == 2:
            plt.imshow(img_tmp, cmap='gray')
        else:
            plt.imshow(img_tmp)

    plt.savefig(save_path + dataset_name + '_' + filename + '.png', dpi=200)

    return -1, -1
Пример #2
0
def main(frame_path):

    filename, f_type = getBaseName(frame_path)
    createFolder('img')
    createFolder('img/out')
    createFolder('img/out/' + filename)
    save_path = 'img/out/' + filename + '/'

    img = cv2.imread(frame_path, 1)
    h, w, c = img.shape
    [b, g, r] = cv2.split(img)
    img_red_filted, idx_red = Euclidean_filter(img, 1, 150, 0, [255, 0, 0],
                                               [b, g, r], save_path)
    img_white_filted, idx_white = Euclidean_filter(img, 1, 255, 7,
                                                   [255, 255, 255], [b, g, r],
                                                   save_path)

    img_symbolic_idx, img_symbolic_show = symbolic_image([idx_red, idx_white],
                                                         (h, w))

    img_light_b = np.where(img_symbolic_idx > 0, 255, 0).astype('uint8')

    img_ccl_origin, img_ccl_show, label_num = ccl(img_light_b)
    img_box, boxes = find_BoundingBox(img_ccl_origin, img)
    """
    cv2.imshow("img_ccl", img_ccl)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    """

    #print(frame_path,label_num, img_ccl_origin.shape)

    if save:
        cv2.imwrite('img/out/' + filename + '/' + filename + '_origin.png',
                    img)
        cv2.imwrite(
            'img/out/' + filename + '/' + filename + '_img_red_filted.png',
            img_red_filted)
        cv2.imwrite(
            'img/out/' + filename + '/' + filename + '_img_white_filted.png',
            img_white_filted)

        cv2.imwrite(
            'img/out/' + filename + '/' + filename + '_img_symbolic.png',
            img_symbolic_show)
        cv2.imwrite(
            'img/out/' + filename + '/' + filename + '_img_light_b.png',
            img_light_b)
        cv2.imwrite('img/out/' + filename + '/' + filename + '_img_ccl.png',
                    img_ccl_show)
        cv2.imwrite('img/out/' + filename + '/' + filename + '_img_box.png',
                    img_box)
Пример #3
0
def make_training_data(base, frame_path, dataset_name):

    print(frame_path)
    filename, f_type = getBaseName(frame_path)

    img = cv2.imread(base + dataset + "/" + filename + '.bmp',
                     1).astype('uint8')
    img_ground = cv2.imread(base + "ground_truth/" + filename + '.bmp',
                            1).astype('uint8')
    img_yolo_b = cv2.imread(base + "yolo_binary/" + filename + '.png',
                            0).astype('uint8')

    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    img_hsv[:, :, 0] = img_hsv[:, :, 0] / img_hsv[:, :, 0].max() * 255.0
    img_hsv[:, :, 0] = img_hsv[:, :, 1] / img_hsv[:, :, 1].max() * 255.0
    img_hsv[:, :, 0] = img_hsv[:, :, 2] / img_hsv[:, :, 2].max() * 255.0

    img_H = img_hsv[:, :, 0]
    img_S = img_hsv[:, :, 1]
    img_V = img_hsv[:, :, 2]

    img_ground_mask = binary_color_filter(img_ground).astype('uint8')

    img_ccl_origin, img_ccl_show, label_num = ccl(img_ground_mask)
    img_box, boxes = find_BoundingBox(img_ccl_origin, img)

    features = []
    answers = []
    for b in boxes:
        x = b[0]
        y = b[1]
        w = b[2] - b[0]
        h = b[3] - b[1]

        ### make feature

        yolo_and_edgebox = img_yolo_b[y:y + h, x:x + w]
        area_yolo_and_edgebox = (yolo_and_edgebox // 255).sum()

        ROI_combine = area_yolo_and_edgebox / (w * h)
        center_line = img_S[y + h // 2, x:x + w]
        ROI_center_std = np.std(center_line)
        ROI_center_min = np.amin(center_line)
        ROI_center_area = w * h
        ROI_height = y

        feature = [
            filename, ROI_combine, ROI_center_min, ROI_center_std, ROI_height,
            ROI_center_area, [x, y, w, h]
        ]

        #print(feature)
        ### make answer

        yolo_and_ground = np.bitwise_and(img_ground_mask[y:y + h, x:x + w],
                                         img_yolo_b[y:y + h, x:x + w])
        area_yolo_and_ground = (yolo_and_ground // 255).sum()

        img_ROI = img_ground[y:y + h, x:x + w]
        t, ct = np.unique(img_ROI.reshape(-1, img_ROI.shape[2]),
                          axis=0,
                          return_counts=True)
        #print(t,ct)

        answer = ""
        #if len(t)==1:
        t = t.tolist()
        idx, = np.where(ct == ct.max())
        if t[int(idx)] in [[0, 0, 255], [0, 0, 0]]:
            answer = 0
        else:
            answer = 1

        if answer not in [0, 1]:
            if t in [[0, 0, 255], [0, 0, 0]]:
                answer = 0
            else:
                answer = 1

            cv2.imshow("img_ROI", img_ROI)
            cv2.waitKey(0)
            cv2.destroyAllWindows()
        """
        print("iou:", ROI_combine, "answer:", answer)
        #cv2.imshow("img_ground", img_ground)
        cv2.imshow("img_ROI", img_ROI)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        """

        features.append(feature)
        answers.append(answer)

    #print(boxes)
    """
    cv2.imshow("img_ground", img_box)
    cv2.imshow("img_ground_mask", img_ground_mask)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    """

    return features, answers
Пример #4
0
def main(frame_path, dataset_name):
    
    print(frame_path)
    filename, f_type = getBaseName(frame_path)
    
    
    #createFolder('img/out/'+dataset_name+'/'+filename)
    #save_path = 'img/out/'+dataset_name+'/'+filename+'/'

    img = cv2.imread(frame_path, 1)
    h, w, c = img.shape
    [b,g,r] = cv2.split(img)
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    img_hsv[:,:,0] = img_hsv[:,:,0] / img_hsv[:,:,0].max() * 255.0
    img_hsv[:,:,0] = img_hsv[:,:,1] / img_hsv[:,:,1].max() * 255.0
    img_hsv[:,:,0] = img_hsv[:,:,2] / img_hsv[:,:,2].max() * 255.0

    img_H = img_hsv[:,:,0]
    img_S = img_hsv[:,:,1]
    img_V = img_hsv[:,:,2]
    
    
    ### Light Detection
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) / 255.0
    ret , img_gray_th = cv2.threshold(img_gray, 0.4, 1, cv2.THRESH_TOZERO)
    

    if nakagami:
        img_nakagami = Nakagami_image_enhancement(img_gray_th, 3)
    else:
        img_nakagami = img_gray_th
    img_nakagami_norm = img_nakagami/img_nakagami.max() # resize to [0,1]
    
    ret, img_nakagami_norm_th = cv2.threshold(img_nakagami_norm, 0.88, 1, cv2.THRESH_TOZERO)

    ### Area filter
    thresh = (img_nakagami_norm_th * 255.0).astype('uint8')
    
    img_ccl_origin, img_ccl_show, label_nums = ccl(thresh)    ### need imwrite
    
    
    """
    cv2.imshow("thresh", thresh)
    cv2.imshow("opening", img_ccl_show)
    #cv2.imshow("sure_bg", sure_bg)
    
    
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    """
    """
    for (i, label) in enumerate(np.unique(img_ccl_origin)):
        if label == 0: #background
            continue
        else:
            labelMask = np.zeros(img_nakagami_norm_th_tmp.shape, dtype="uint8")
            labelMask[labels == label] = 255
            numPixels = cv2.countNonZero(labelMask)
    """

    

    ### clip center
    img_nakagami_norm_th_clip = clip_center(img_nakagami_norm_th, int(h/3), int(h))
    #print(img_nakagami_norm_th_center.max())
    #print(img_nakagami_norm_th.max())
    ### Color filter
    #img_white_filted, idx_white, img_white_d = Euclidean_filter(img=img, threshold=15, color=[255,255,255], img_BGR_spilt=[b,g,r], save_path=save_path)
    ret , img_white_filted = cv2.threshold(img_gray, 245/255.0, 1, cv2.THRESH_TOZERO)
    img_white_filted = img_white_filted*255.0
    #img_red_filted, idx_red, img_red_d = Euclidean_filter(img=img, threshold=150, color=[255,50,150], img_BGR_spilt=[b,g,r], save_path=save_path)
    


    lower_red = np.array([0,100,0])
    upper_red = np.array([5,255,255])
    mask0 = cv2.inRange(img_hsv, lower_red, upper_red)
    
    lower_red = np.array([170,100,0])
    upper_red = np.array([179,255,255])
    mask1 = cv2.inRange(img_hsv, lower_red, upper_red)
    mask = mask0 + mask1
    img_red_filted = img.copy()
    img_red_filted[np.where(mask==0)] = 0
    
    """
    cv2.imshow("mask0", mask0)
    cv2.imshow("mask1", mask1)
    cv2.imshow("mask", mask)
    #cv2.imshow("sure_bg", sure_bg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    """

    ###### 形態學操作
    #img_white_filted_gray = cv2.cvtColor(img_white_filted, cv2.COLOR_BGR2GRAY)
    img_white_filted_gray = img_white_filted
    k=np.ones((3,3), np.uint8)
    img_white_mor = cv2.morphologyEx(img_white_filted_gray,cv2.MORPH_CLOSE, k,iterations=3) / 255.0
    

    
    img_red_filted_gray = cv2.cvtColor(img_red_filted, cv2.COLOR_BGR2GRAY)
    ret , img_red_filted_gray_th = cv2.threshold(img_red_filted_gray, 150, 255, cv2.THRESH_TOZERO)
    k=np.ones((13,13), np.uint8)
    #k=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(13,13))
    img_red_mor = cv2.morphologyEx(img_red_filted_gray,cv2.MORPH_CLOSE, k,iterations=5)
    ret, img_red_mor_th = cv2.threshold(img_red_mor, 80, 255, cv2.THRESH_TOZERO)
    

    ### Multiply color and light
    img_white_multiply = img_nakagami_norm_th_clip * img_white_mor
    img_red_multiply = img_nakagami_norm_th_clip * img_red_mor_th
    
    ret, img_white_b = cv2.threshold(img_white_multiply, 0.5, 255, cv2.THRESH_BINARY)
    ret, img_red_b = cv2.threshold(img_red_multiply, 0.5, 255, cv2.THRESH_BINARY)

    img_white_light = img_white_b - img_red_b
    img_red_light = img_red_b

    img_white_light = np.clip(img_white_light,0,255)
    img_red_light = np.clip(img_red_light,0,255)
    
    

    ### Contour
    img_red_contour = contour_Detection(img_red_light)
    img_white_contour = contour_Detection(img_white_light)

    ### EdgeBox
    img_roi_combine = np.copy(img)
    img_red_edgeboxes,img_roi_combine, boxes_red = Edgeboxes(img_gray=img_red_contour, img_origin=img, color=[0,255,0], img_roi_combine=img_roi_combine, state=state, filename=filename, base=base) #contour, img_origin, box color(BGR)
    img_white_edgeboxes,img_roi_combine, boxes_white = Edgeboxes(img_gray=img_white_contour, img_origin=img, color=[255,0,0], img_roi_combine=img_red_edgeboxes, state=state, filename=filename, base=base) #contour, img_origin, box color(BGR)
    
    ### Fusion
    ### fusion ###
    img_ground = cv2.imread(base+"ground_truth/"+filename+'.bmp',1).astype('uint8')
    img_ground_mask = binary_color_filter(img_ground).astype('uint8')
    
    img_yolo_b = cv2.imread(base+"yolo_binary/"+filename+'.png',0).astype('uint8')
        
    features_red, answers_red = make_feature(boxes_red, img_ground=img_ground, img_ground_mask=img_ground_mask, state=state, img_H=img_H, img_yolo_b=img_yolo_b, filename=filename)
    features_white, answers_white = make_feature(boxes_white, img_ground=img_ground, img_ground_mask=img_ground_mask, state=state, img_H=img_H, img_yolo_b=img_yolo_b, filename=filename)
        
    features = features_red + features_white
    answers = answers_red + answers_white

    imgs =  [
            img, img_hsv, img_H, img_S, img_V, img_gray*255.0, 
            img_gray_th*255.0, img_nakagami_norm*255.0, img_nakagami_norm_th*255.0, img_nakagami_norm_th_clip*255.0,
            img_white_filted, img_white_filted_gray, img_white_mor*255.0, img_white_multiply*255.0, 
            img_red_filted, img_red_filted_gray, img_red_filted_gray_th, img_red_mor, 
            img_red_mor_th, img_red_multiply, img_white_b, img_red_b, 
            img_white_light, img_red_light, img_red_contour, img_white_contour, 
            img_red_edgeboxes, img_white_edgeboxes, img_roi_combine
    ]
    
    for i in range(len(names)):
        
        save_path = 'img/out/'+dataset_name+'/'+names[i]+'/'+filename
        try:
            cv2.imwrite(save_path+'.png', imgs[i])
        except:
            print('error on ', names[i])
   
    #img_white_edgeboxes,img_roi_combine,
    
    #cv2.imwrite(save_path+'_img_nakagami_thB.png', img_nakagami_thB*255.0)

    #img_white_filted, idx_white, img_white_d = Euclidean_filter(img=img, threshold=20, color=[255,255,255], img_BGR_spilt=[b,g,r], save_path=save_path)
    
    """    
    cv2.imshow("img_nakagami", img_gray)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    """
    """
    ### 形態學操作
    #r1=cv2.morphologyEx(img_red,cv2.MORPH_CLOSE, k,iterations=3)
    #k=np.ones((3,3), np.uint8)
    k=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
    img_mor = cv2.morphologyEx(img_red, cv2.MORPH_CLOSE, k,iterations=3)

    img_ccl_origin, img_ccl_show, label_num = ccl(img_mor)
    img_red_box, red_boxes = find_BoundingBox(img_ccl_origin, img)
    """
    """
    cv2.imshow("img_red_filled", img_box)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    """
    

   
    

    """
    cv2.imshow("img_red", img_mor)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    """
    

    
    

   
    
    
    

    
    
    
    """
    h, w, c = img.shape
    
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    img_H = img_hsv[:,:,0]
    img_S = img_hsv[:,:,1]
    img_V = img_hsv[:,:,2]
    
    [b,g,r] = cv2.split(img)
    
    
    ### image enhacement
    img_th = color_intensity(img)
    img_nakagami = Nakagami_image_enhancement(img_th, 3)
    ret, img_nakagami_thB = cv2.threshold(img_nakagami, 100, 1, cv2.THRESH_BINARY)

    img_red_filted, idx_red = Euclidean_filter(img, 1,150,0,[255,0,0], [b,g,r], save_path)  #img, percent,up_threshold,lower_threshold color(RGB), save_path
    img_white_filted,idx_white = Euclidean_filter(img, 1,255,7, [255,255,255], [b,g,r], save_path)  #img, percent, color(RGB), save_path
    
    
    img_red_filted_gray = cv2.cvtColor(img_red_filted, cv2.COLOR_BGR2GRAY)
    img_white_filted_gray = cv2.cvtColor(img_white_filted, cv2.COLOR_BGR2GRAY)
    
    img_red_filted_gray_max = img_red_filted_gray.max()
    img_white_filted_gray_max = img_white_filted_gray.max()
    

    img_red_filted_gray_norm = img_red_filted_gray/img_red_filted_gray_max
    img_white_filted_gray_norm = img_white_filted_gray/img_white_filted_gray_max

    
    img_red_multiply = img_nakagami_thB * img_red_filted_gray_norm
    img_white_multiply = img_nakagami_thB * img_white_filted_gray_norm

    img_red_multiply = img_red_multiply*255.0
    img_white_multiply = img_white_multiply*255.0
    
    #img_red_nakagami = Nakagami_image_enhancement(img_red_filted_gray_norm, 3) #gray_img, kernel_size
    #img_white_nakagami = Nakagami_image_enhancement(img_white_filted_gray_norm, 3) #gray_img, kernel_size
    
    
    img_red_nakagami_cliped = clip_center(img_red_multiply, int(h/3), int(h)), # img, y_up, y_down
    img_white_nakagami_cliped = clip_center(img_white_multiply, int(h/3), int(h)), # img, y_up, y_down
    img_red_nakagami_cliped = img_red_nakagami_cliped[0]
    img_white_nakagami_cliped = img_white_nakagami_cliped[0]
   
    
    

    img_red_contour = contour_Detection(img_red_nakagami_cliped)
    img_white_contour = contour_Detection(img_white_nakagami_cliped)

    
    
    
    #print(type(img_red_contour_cliped[0]))
    """
    """
    cv2.imshow("img_red_contour_cliped", img_red_contour_cliped)
    #cv2.imshow("img_white_filted_gray", img_white_filted_gray)
    #cv2.imshow("img_red_nakagami", img_red_nakagami)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    """
    """
    img_roi_combine = np.copy(img)
    img_red_edgeboxes,img_roi_combine, boxes_red = Edgeboxes(img_red_contour, img, [0,255,0], img_roi_combine, state=state, filename=filename, base=base) #contour, img_origin, box color(BGR)
    img_white_edgeboxes,img_roi_combine, boxes_white = Edgeboxes(img_white_contour, img, [255,0,0], img_red_edgeboxes, state=state, filename=filename, base=base) #contour, img_origin, box color(BGR)
    
    
    ### fusion ###
    
    

    img_ground = cv2.imread(base+"ground_truth/"+filename+'.bmp',1).astype('uint8')
    img_ground_mask = binary_color_filter(img_ground).astype('uint8')
    
    img_yolo_b = cv2.imread(base+"yolo_binary/"+filename+'.png',0).astype('uint8')
        
    features_red, answers_red = make_feature(boxes_red, img_ground=img_ground, img_ground_mask=img_ground_mask, state=state, img_H=img_H, img_yolo_b=img_yolo_b, filename=filename)
    features_white, answers_white = make_feature(boxes_white, img_ground=img_ground, img_ground_mask=img_ground_mask, state=state, img_H=img_H, img_yolo_b=img_yolo_b, filename=filename)
        
    features = features_red + features_white
    answers = answers_red + answers_white
        
       
    
        
    ### report ###
    
        

    if save:
        cv2.imwrite(save_path+'_origin.png', img)
        cv2.imwrite(save_path+'_color_intensity_with_threshold.png', img_th*255.0)
        cv2.imwrite(save_path+'_img_nakagami.png', img_nakagami)
        cv2.imwrite(save_path+'_img_nakagami_thB.png', img_nakagami_thB*255.0)
        
        
        
        cv2.imwrite(save_path+'_img_H.png', img_H)
        cv2.imwrite(save_path+'_img_S.png', img_S)
        cv2.imwrite(save_path+'_img_V.png', img_V)
        cv2.imwrite(save_path+'_img_red_filted.png', img_red_filted)
        cv2.imwrite(save_path+'_img_white_filted.png', img_white_filted)
        cv2.imwrite(save_path+'_img_red_filted_gray.png', img_red_filted_gray)
        
        #cv2.imwrite(save_path+'_img_red_nakagami.png', img_red_nakagami)
        #cv2.imwrite(save_path+'_img_white_nakagami.png', img_white_nakagami)
        
        cv2.imwrite(save_path+'_img_red_nakagami_cliped.png', img_red_nakagami_cliped)
        cv2.imwrite(save_path+'_img_white_nakagami_cliped.png', img_white_nakagami_cliped)

        cv2.imwrite(save_path+'_img_red_multiply.png', img_red_multiply)
        cv2.imwrite(save_path+'_img_white_multiply.png', img_white_multiply)
        
        
        cv2.imwrite(save_path+'_img_red_contour.png', img_red_contour)
        cv2.imwrite(save_path+'_img_white_contour.png', img_white_contour)

        
        

        cv2.imwrite(save_path+'_img_red_edgeboxes.png', img_red_edgeboxes)
        cv2.imwrite(save_path+'_img_white_edgeboxes.png', img_white_edgeboxes)
        cv2.imwrite(save_path+'_img_roi_combine.png', img_roi_combine)

    

        #cv2.imwrite(save_path+'_img_red.png', r)
    return features, answers
    """
    return features, answers
Пример #5
0
 'img', 'img_result_light_mask/combine', 'img_result_light_mask/combine_rect', 'img_result_light_mask/light', 'img_result_light_mask/light_rect', 
 'img_result_light_mask/yolo', 'img_result_light_mask/yolo_rect']

#folder_names = ['img_result_light_mask/yolo']




for folder in folder_names:
    image_path = '../img/demo-out/3/'+folder+'/'
    files = glob.glob(image_path+'*.png')

    i=0
    filenames = []
    for f in files:
        filename = getBaseName(f)[0]
        filenames.append(int(filename))
        i+=1
    print(folder, " has %d files"%i)

    # batch process
    batch = i//3000 + 1
    batch_filenames = []
    files = []
    ct=0
    for f in sorted(filenames):
        files.append(int(f))
        ct+=1

    img = cv2.imread(image_path+'0.png')
    h,w,c = img.shape
Пример #6
0
def main(frame_path, dataset_name, boxI=None):

    print(frame_path)
    filename, f_type = getBaseName(frame_path)

    #createFolder('img/out/'+dataset_name+'/'+filename)
    #save_path = 'img/out/'+dataset_name+'/'+filename+'/'

    img = cv2.imread(frame_path, 1)
    h, w, c = img.shape
    [b, g, r] = cv2.split(img)
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    img_hsv[:, :, 0] = img_hsv[:, :, 0] / img_hsv[:, :, 0].max() * 255.0
    img_hsv[:, :, 0] = img_hsv[:, :, 1] / img_hsv[:, :, 1].max() * 255.0
    img_hsv[:, :, 0] = img_hsv[:, :, 2] / img_hsv[:, :, 2].max() * 255.0

    img_H = img_hsv[:, :, 0]
    img_S = img_hsv[:, :, 1]
    img_V = img_hsv[:, :, 2]

    ### Light Detection
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) / 255.0
    ret, img_gray_th = cv2.threshold(img_gray, 0.4, 1, cv2.THRESH_TOZERO)

    img_sigmoid = img_gray * 255.0
    img_sigmoid = 1 / (1 + np.exp(-(img_gray - 128) / 20))
    print(img_sigmoid.min(), img_sigmoid.max())
    d = img_sigmoid.max() - img_sigmoid.min()

    img_sigmoid = 255 * (img_sigmoid - img_sigmoid.min()) / d
    print(img_sigmoid.min(), img_sigmoid.max())
    ret, img_sigmoid_th = cv2.threshold(img_sigmoid, 245, 255,
                                        cv2.THRESH_TOZERO)

    createFolder('img/out/' + dataset_name + '/' + "img_test_sigmoid")
    createFolder('img/out/' + dataset_name + '/' + "img_test_sigmoid_th")
    cv2.imwrite(
        'img/out/' + dataset_name + '/' + "img_test_sigmoid" + '/' + filename +
        '.png', img_sigmoid)
    cv2.imwrite(
        'img/out/' + dataset_name + '/' + "img_test_sigmoid_th" + '/' +
        filename + '.png', img_sigmoid_th)

    if nakagami:
        img_nakagami = Nakagami_image_enhancement(img_gray_th, 3)
    else:
        img_nakagami = img_gray_th
    img_nakagami_norm = img_nakagami / img_nakagami.max()  # resize to [0,1]

    ret, img_nakagami_norm_th = cv2.threshold(img_nakagami_norm, 200 / 255, 1,
                                              cv2.THRESH_TOZERO)

    ### clip center
    img_nakagami_norm_th_clip = clip_center(img_nakagami_norm_th, int(h / 3),
                                            int(h))

    img_white_filted = img_nakagami_norm_th_clip * 255.0
    #ret , img_white_filted = cv2.threshold(img_nakagami_norm_th_clip, 245/255.0, 1, cv2.THRESH_TOZERO)
    #img_white_filted = img_white_filted*255.0

    k = np.ones((3, 3), np.uint8)
    img_white_mor = cv2.morphologyEx(
        img_white_filted, cv2.MORPH_CLOSE, k, iterations=3) / 255.0

    ret, img_white_b = cv2.threshold(img_white_mor, 0.9, 255,
                                     cv2.THRESH_BINARY)

    ### Contour

    img_white_contour = contour_Detection(img_white_b)

    ### EdgeBox
    img_roi_combine = np.copy(img)
    #img_red_edgeboxes,img_roi_combine, boxes_red = Edgeboxes(img_gray=img_red_contour, img_origin=img, color=[0,255,0], img_roi_combine=img_roi_combine, state=state, filename=filename, base=base) #contour, img_origin, box color(BGR)
    img_white_edgeboxes, img_roi_combine, boxes_white = Edgeboxes(
        img_gray=img_white_contour,
        img_origin=img,
        color=[255, 0, 0],
        img_roi_combine=img_roi_combine,
        state=state,
        filename=filename,
        base=base)  #contour, img_origin, box color(BGR)

    ### Fusion
    ### fusion ###
    img_ground = cv2.imread(base + "ground_truth/" + filename + '.bmp',
                            1).astype('uint8')
    img_ground_mask = binary_color_filter(img_ground).astype('uint8')

    img_yolo_b = cv2.imread(base + "yolo_binary/" + filename + '.png',
                            0).astype('uint8')

    features_white, answers_white, bI = make_feature(
        boxes=boxes_white,
        version='v7-hand',
        img_ground=img_ground,
        img_ground_mask=img_ground_mask,
        state=state,
        img_S=img_S,
        img_yolo_b=img_yolo_b,
        filename=filename,
        img=img,
        boxI=boxI)

    features = features_white
    answers = answers_white

    imgs = [
        img, img_hsv, img_H, img_S, img_V, img_gray * 255.0,
        img_gray_th * 255.0, img_nakagami_norm * 255.0,
        img_nakagami_norm_th * 255.0, img_nakagami_norm_th_clip * 255.0,
        img_white_filted, img_white_mor * 255.0, img_white_contour,
        img_white_edgeboxes, img_roi_combine
    ]

    for i in range(len(names)):

        save_path = 'img/out/' + dataset_name + '/' + names[i] + '/' + filename
        try:
            cv2.imwrite(save_path + '.png', imgs[i])
        except:
            print('error on ', names[i])

    #img_white_edgeboxes,img_roi_combine,

    #cv2.imwrite(save_path+'_img_nakagami_thB.png', img_nakagami_thB*255.0)

    #img_white_filted, idx_white, img_white_d = Euclidean_filter(img=img, threshold=20, color=[255,255,255], img_BGR_spilt=[b,g,r], save_path=save_path)
    """    
    cv2.imshow("img_nakagami", img_gray)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    """

    return features, answers, bI
Пример #7
0
def main(frame_path, dataset_name):
    print(frame_path)
    filename, f_type = getBaseName(frame_path)
    createFolder('img')
    createFolder('img/out')
    createFolder('img/out/'+dataset_name)
    createFolder('img/out/'+dataset_name+'/'+filename)
    save_path = 'img/out/'+dataset_name+'/'+filename+'/'

    img = cv2.imread(frame_path, 1)
    h, w, c = img.shape
    
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    img_H = img_hsv[:,:,0]
    img_S = img_hsv[:,:,1]
    img_V = img_hsv[:,:,2]
    
    [b,g,r] = cv2.split(img)
    
    
    ### image enhacement
    img_th = color_intensity(img)
    img_nakagami = Nakagami_image_enhancement(img_th, 3)
    ret, img_nakagami_thB = cv2.threshold(img_nakagami, 100, 1, cv2.THRESH_BINARY)

    img_red_filted, idx_red = Euclidean_filter(img, 1,150,0,[255,0,0], [b,g,r], save_path)  #img, percent,up_threshold,lower_threshold color(RGB), save_path
    img_white_filted,idx_white = Euclidean_filter(img, 1,255,7, [255,255,255], [b,g,r], save_path)  #img, percent, color(RGB), save_path
    
    
    img_red_filted_gray = cv2.cvtColor(img_red_filted, cv2.COLOR_BGR2GRAY)
    img_white_filted_gray = cv2.cvtColor(img_white_filted, cv2.COLOR_BGR2GRAY)
    
    img_red_filted_gray_max = img_red_filted_gray.max()
    img_white_filted_gray_max = img_white_filted_gray.max()
    

    img_red_filted_gray_norm = img_red_filted_gray/img_red_filted_gray_max
    img_white_filted_gray_norm = img_white_filted_gray/img_white_filted_gray_max

    
    img_red_multiply = img_nakagami_thB * img_red_filted_gray_norm
    img_white_multiply = img_nakagami_thB * img_white_filted_gray_norm

    img_red_multiply = img_red_multiply*255.0
    img_white_multiply = img_white_multiply*255.0
    
    #img_red_nakagami = Nakagami_image_enhancement(img_red_filted_gray_norm, 3) #gray_img, kernel_size
    #img_white_nakagami = Nakagami_image_enhancement(img_white_filted_gray_norm, 3) #gray_img, kernel_size
    
    
    img_red_nakagami_cliped = clip_center(img_red_multiply, int(h/3), int(h)), # img, y_up, y_down
    img_white_nakagami_cliped = clip_center(img_white_multiply, int(h/3), int(h)), # img, y_up, y_down
    img_red_nakagami_cliped = img_red_nakagami_cliped[0]
    img_white_nakagami_cliped = img_white_nakagami_cliped[0]
   
    
    

    img_red_contour = contour_Detection(img_red_nakagami_cliped)
    img_white_contour = contour_Detection(img_white_nakagami_cliped)

    
    
    
    #print(type(img_red_contour_cliped[0]))
    
    """
    cv2.imshow("img_red_contour_cliped", img_red_contour_cliped)
    #cv2.imshow("img_white_filted_gray", img_white_filted_gray)
    #cv2.imshow("img_red_nakagami", img_red_nakagami)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    """

    img_roi_combine = np.copy(img)
    img_red_edgeboxes,img_roi_combine, boxes_red = Edgeboxes(img_red_contour, img, [0,255,0], img_roi_combine, state=state, filename=filename, base=base) #contour, img_origin, box color(BGR)
    img_white_edgeboxes,img_roi_combine, boxes_white = Edgeboxes(img_white_contour, img, [255,0,0], img_red_edgeboxes, state=state, filename=filename, base=base) #contour, img_origin, box color(BGR)
    
    
    ### fusion ###
    
    

    img_ground = cv2.imread(base+"ground_truth/"+filename+'.bmp',1).astype('uint8')
    img_ground_mask = binary_color_filter(img_ground).astype('uint8')
    
    img_yolo_b = cv2.imread(base+"yolo_binary/"+filename+'.png',0).astype('uint8')
        
    features_red, answers_red = make_feature(boxes_red, img_ground=img_ground, img_ground_mask=img_ground_mask, state=state, img_H=img_H, img_yolo_b=img_yolo_b, filename=filename)
    features_white, answers_white = make_feature(boxes_white, img_ground=img_ground, img_ground_mask=img_ground_mask, state=state, img_H=img_H, img_yolo_b=img_yolo_b, filename=filename)
        
    features = features_red + features_white
    answers = answers_red + answers_white
        
       
    
        
    ### report ###
    
        

    if save:
        cv2.imwrite(save_path+'_origin.png', img)
        cv2.imwrite(save_path+'_color_intensity_with_threshold.png', img_th*255.0)
        cv2.imwrite(save_path+'_img_nakagami.png', img_nakagami)
        cv2.imwrite(save_path+'_img_nakagami_thB.png', img_nakagami_thB*255.0)
        
        
        
        cv2.imwrite(save_path+'_img_H.png', img_H)
        cv2.imwrite(save_path+'_img_S.png', img_S)
        cv2.imwrite(save_path+'_img_V.png', img_V)
        cv2.imwrite(save_path+'_img_red_filted.png', img_red_filted)
        cv2.imwrite(save_path+'_img_white_filted.png', img_white_filted)
        cv2.imwrite(save_path+'_img_red_filted_gray.png', img_red_filted_gray)
        
        #cv2.imwrite(save_path+'_img_red_nakagami.png', img_red_nakagami)
        #cv2.imwrite(save_path+'_img_white_nakagami.png', img_white_nakagami)
        
        cv2.imwrite(save_path+'_img_red_nakagami_cliped.png', img_red_nakagami_cliped)
        cv2.imwrite(save_path+'_img_white_nakagami_cliped.png', img_white_nakagami_cliped)

        cv2.imwrite(save_path+'_img_red_multiply.png', img_red_multiply)
        cv2.imwrite(save_path+'_img_white_multiply.png', img_white_multiply)
        
        
        cv2.imwrite(save_path+'_img_red_contour.png', img_red_contour)
        cv2.imwrite(save_path+'_img_white_contour.png', img_white_contour)

        
        

        cv2.imwrite(save_path+'_img_red_edgeboxes.png', img_red_edgeboxes)
        cv2.imwrite(save_path+'_img_white_edgeboxes.png', img_white_edgeboxes)
        cv2.imwrite(save_path+'_img_roi_combine.png', img_roi_combine)



        #cv2.imwrite(save_path+'_img_red.png', r)
    return features, answers
Пример #8
0
def main(frame_name):
    features = ""
    print(frame_name)
    filename, f_type = getBaseName(frame_name)

    img = cv2.imread(frame_name, 1)
    h, w, c = img.shape
    [b, g, r] = cv2.split(img)
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    img_hsv[:, :, 0] = img_hsv[:, :, 0] / img_hsv[:, :, 0].max() * 255.0
    img_hsv[:, :, 0] = img_hsv[:, :, 1] / img_hsv[:, :, 1].max() * 255.0
    img_hsv[:, :, 0] = img_hsv[:, :, 2] / img_hsv[:, :, 2].max() * 255.0

    img_H = img_hsv[:, :, 0]
    img_S = img_hsv[:, :, 1]
    img_V = img_hsv[:, :, 2]

    ### Light Detection
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) / 255.0
    ret, img_gray_th = cv2.threshold(img_gray, 0.4, 1, cv2.THRESH_TOZERO)

    if nakagami:
        img_nakagami = Nakagami_image_enhancement(img_gray_th, 3)
    else:
        img_nakagami = img_gray_th
    img_nakagami_norm = img_nakagami / img_nakagami.max()  # resize to [0,1]

    ret, img_nakagami_norm_th = cv2.threshold(img_nakagami_norm, 200 / 255, 1,
                                              cv2.THRESH_TOZERO)

    ### clip center
    img_nakagami_norm_th_clip = clip_center(img_nakagami_norm_th, int(h / 3),
                                            int(h))

    img_white_filted = img_nakagami_norm_th_clip * 255.0
    #ret, img_white_filted = cv2.threshold(img_white_filted, 200, 255, cv2.THRESH_TOZERO)
    #ret , img_white_filted = cv2.threshold(img_nakagami_norm_th_clip, 245/255.0, 1, cv2.THRESH_TOZERO)
    #img_white_filted = img_white_filted*255.0

    k = np.ones((3, 3), np.uint8)
    img_white_mor = cv2.morphologyEx(
        img_white_filted, cv2.MORPH_CLOSE, k, iterations=3) / 255.0

    ret, img_white_b = cv2.threshold(img_white_mor, 0.9, 255,
                                     cv2.THRESH_BINARY)

    ### Contour
    img_white_contour = contour_Detection(img_white_b)

    ### EdgeBox
    img_roi_combine = np.copy(img)
    #img_red_edgeboxes,img_roi_combine, boxes_red = Edgeboxes(img_gray=img_red_contour, img_origin=img, color=[0,255,0], img_roi_combine=img_roi_combine, state=state, filename=filename, base=base) #contour, img_origin, box color(BGR)
    img_white_edgeboxes, img_roi_combine, boxes_white = Edgeboxes(
        img_gray=img_white_contour,
        img_origin=img,
        color=[255, 0, 0],
        img_roi_combine=img_roi_combine,
        state=state,
        filename=filename,
        base=None)  #contour, img_origin, box color(BGR)

    img_yolo_b = cv2.imread(yolo_b_path + filename + '.png', 0).astype('uint8')
    features_white = make_feature(boxes=boxes_white,
                                  version='v6',
                                  img_ground=None,
                                  img_ground_mask=None,
                                  state='test',
                                  img_S=img_S,
                                  img_yolo_b=img_yolo_b,
                                  filename=filename)

    features = features_white
    ### save files ###
    imgs = [
        img, img_hsv, img_H, img_S, img_V, img_gray * 255.0,
        img_gray_th * 255.0, img_nakagami_norm * 255.0,
        img_nakagami_norm_th * 255.0, img_nakagami_norm_th_clip * 255.0,
        img_white_filted, img_white_mor * 255.0, img_white_b,
        img_white_contour, img_white_edgeboxes, img_roi_combine
    ]

    dataset_name = frame_name.split('/')[2]
    for i in range(len(names)):

        save_path = 'img/demo-out/' + dataset_name + '/' + names[
            i] + '/' + filename
        try:
            cv2.imwrite(save_path + '.png', imgs[i])
        except:
            print('error on ', names[i])

    return features
Пример #9
0
sys.path.insert(0, parentdir)

from utils.files import getBaseName, createFolder

try:
    shutil.rmtree(
        '/home/alanhc-school/Downloads/research/research-beta/architecture0714/img/demo-out'
    )
except:
    print('folder clean.')

files = glob.glob('../../../dataset/highway_video/*')
video_path = files[1]
print(files)

filename = getBaseName(video_path)[0]
createFolder('../img')
createFolder('../img/demo-out')
createFolder('../img/demo-out/' + filename)
createFolder('../img/demo-out/' + filename + '/origin')

save_path = '../img/demo-out/' + filename + '/'
cyclegan_path = '/home/alanhc-school/Desktop/CycleGAN-Tensorflow-2/'

vidcap = cv2.VideoCapture(video_path)
success, image = vidcap.read()
count = 0
while success:
    try:
        # save frame as JPEG file
        success, image = vidcap.read()