예제 #1
0
def proc1(str1, sw):
    sfx = ("fixed_") if (sw == 2) else ("")

    img_src = cv2.imread("img/aerial_only_" + str1 + ".png", cv2.IMREAD_COLOR)
    img_mask = cv2.imread("img/mask_invert_" + str1 + ".png",
                          cv2.IMREAD_GRAYSCALE)
    # npy_label = np.load( "data/label.npy" )
    npy_label = labeling_from_mask(img_mask)
    # npy_data  = np.load( "data/edge_length_average.npy" )
    src_gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
    npy_data = getAverageEdgeLength(src_gray, img_mask, npy_label, sw)
    # np.save("data/edge_length_average.npy", edge_len)

    npy_data /= npy_data.max()
    # npy_data = sr.extractInRange(npy_data, 0, 40)
    npy_data = sr.percentileModification(npy_data, 10)
    np.savetxt("data/csv_edgelength_" + sfx + str1 + ".csv",
               npy_data,
               delimiter=",",
               fmt="%4.8f")

    result_img = sr.createResultImg(img_src, npy_data, npy_label, True)
    result_img = cv2.add(img_src, result_img)

    cv2.imwrite("img/result_edgelength_" + sfx + str1 + ".png", result_img)
예제 #2
0
def saveEdgeFeatureAsImage(src_img,
                           mask_img,
                           npy_features,
                           feat_name,
                           SHOW_LOG=False,
                           SHOW_IMG=False,
                           doDataMod=True):
    assert (src_img.shape[0] == mask_img.shape[0] and src_img.shape[1] == mask_img.shape[1]), \
      " 'src_img' and 'mask_img' must be the same size."
    assert (src_img.shape[2] == 3), \
      " 'src_img' must be 3-ch RGB image."
    assert (len( mask_img.shape ) == 2), \
      " 'src_img' must be 1-ch Grayscale image"

    # NPY Data Load
    npy_label = labeling_from_mask(mask_img)

    if SHOW_LOG:
        print("  feat: %s" % feat_name, flush=True)

    # Extract specifical feature
    npy_data = ep.extractFeature(npy_features, feat_name)

    # Data Modification
    if doDataMod:
        npy_data = percentileModification(npy_data, 1)
    else:
        npy_data = npy_data / npy_data.max()

    # for debug
    for i in range(1, len(npy_data)):
        print(npy_data[i])

    # Create Image
    dst_img = createResultImg(src_img, npy_data, npy_label, raibowColor=True)
    dst_img = createOverwrappedImage(src_img, dst_img, doGSConv=True)

    if SHOW_IMG:
        cv2.imshow("Test", dst_img)
        cv2.waitKey(0)
    return dst_img
예제 #3
0
def proc2(str1):
    img_src = cv2.imread("img/non_blured/aerial_only_" + str1 + ".png",
                         cv2.IMREAD_COLOR)
    img_mask = cv2.imread("img/mask_invert_" + str1 + ".png",
                          cv2.IMREAD_GRAYSCALE)
    # npy_label = np.load( "data/label.npy" )
    npy_label = labeling_from_mask(img_mask)
    # npy_data  = np.load( "data/edge_variance.npy" )
    npy_data = getEdgeAngleVariance(getEdgeHSVArray(img_src), npy_label)
    # np.save("data/edge_variance.npy", edge_len)

    npy_data /= npy_data.max()
    # npy_data = sr.extractInRange(npy_data, 0, 40)
    npy_data = sr.percentileModification(npy_data, 10)
    # np.savetxt("data/csv_edge_variance_"+sfx+str1+".csv", npy_data, delimiter=",", fmt="%4.8f")

    result_img = sr.createResultImg(img_src, npy_data, npy_label, True)
    result_img = cv2.add(img_src, result_img)

    cv2.imshow("Test", result_img)
    cv2.waitKey(0)
    cv2.imwrite("img/result_edge_variance_" + str1 + ".png", result_img)
예제 #4
0
def makeAnswerImg(mask_img, answer_img, thresh_low=0.20, thresh_high=0.80):
    assert (len(answer_img.shape) == 3), "answer_img must be 3-ch color image."
    assert (thresh_low <
            thresh_high), "thresh_low must be lower than thresh_high"

    npy_label = labeling_from_mask(mask_img)
    R = np.array([0, 0, 255], dtype=np.uint8)

    npy_result = []

    for (labelNum, labels) in enumerate(npy_label):
        n_of_dmg = 0
        for p in labels:
            if (answer_img[p[0], p[1]] == R).all():
                n_of_dmg += 1

        # for Debug
        print("Label %d: score = %lf" % (labelNum, n_of_dmg / len(labels)))

        # 0.30: Collapsed, 0.60:Half-Collapsed, 0.90:Non-Collapsed
        value = n_of_dmg / len(labels)
        if (value < thresh_low):
            # Non-Collapsed
            npy_result.append(0.90)
        elif (thresh_low <= value <= thresh_high):
            # Half-Collapsed
            npy_result.append(0.60)
        else:
            # Collapsed
            npy_result.append(0.30)

    npy_result = np.array(npy_result, dtype=np.float32)
    dst_img = createResultImg(mask_img,
                              npy_result,
                              npy_label,
                              raibowColor=True)
    return dst_img, npy_result
예제 #5
0
def clasify(src_img, mask_img, npy_features, npy_thresholds, SHOW_LOG,
            SHOW_IMG):
    assert (src_img.shape[0] == mask_img.shape[0] and src_img.shape[1] == mask_img.shape[1]), \
      " 'src_img' and 'mask_img' must be the same size."
    assert (src_img.shape[2] == 3), \
      " 'src_img' must be 3-ch RGB image."
    assert (len( mask_img.shape ) == 2), \
      " 'src_img' must be 1-ch Grayscale image"

    features = ['length', 'endpoints', 'branches', 'passings']

    # NPY Data Load
    npy_label = labeling_from_mask(mask_img)

    roi_category = {}

    for feature in features:
        # Extract specify feature
        npy_data = ep.extractFeature(npy_features, feature)
        npy_data = percentileModification(npy_data, 1)

        # Extract specify thresholds
        thresh = npy_thresholds[feature]['th']

        # Add list to dict
        roi_category.update(
            {feature: np.ndarray((len(npy_data)), dtype=np.uint8)})

        # Clasify by thresholds
        # 0 - Collapsed completely
        # 1 - Collapsed roughly
        # 2 - Not Collapsed
        ### Thresholds
        #  npy_thresholds = { 'feat_name', {'type: 0 or 1, th:[]} }:
        # if type == 0
        ##     0.0 < p < th[0]: Not Collapsed (2)
        ##   th[1] < p < th[1]: Collapsed roughly (1)
        ##   th[1] < p <   1.0: Collapsed completely (0)
        # elif type == 1
        ##     0.0 < p < th[0]: Collapsed completely (0)
        ##   th[1] < p < th[1]: Collapsed roughly (1)
        ##   th[1] < p <   1.0: Not Collapsed (2)

        for i in range(1, len(npy_data)):
            # 0 - Collapsed completely
            # 1 - Collapsed roughly
            # 2 - Not Collapsed
            if npy_thresholds[feature]['type'] == 0:
                if (0.0 <= npy_data[i] <= thresh[0]):
                    roi_category[feature][i] = 2
                elif (thresh[0] < npy_data[i] < thresh[1]):
                    roi_category[feature][i] = 1
                elif (thresh[1] <= npy_data[i] <= 1.0):
                    roi_category[feature][i] = 0
            # 0 - Collapsed completely
            # 1 - Collapsed roughly
            # 2 - Not Collapsed
            elif npy_thresholds[feature]['type'] == 1:
                if (0.0 <= npy_data[i] <= thresh[0]):
                    roi_category[feature][i] = 0
                elif (thresh[0] < npy_data[i] < thresh[1]):
                    roi_category[feature][i] = 1
                elif (thresh[1] <= npy_data[i] <= 1.0):
                    roi_category[feature][i] = 2

    npy_result = np.ndarray((len(npy_label)), dtype=np.uint8)
    for i in range(1, len(npy_data)):
        vote = np.zeros((3), dtype=np.uint8)
        for feature in features:
            vote[roi_category[feature][i]] += 1
        npy_result[i] = vote.argmax()

    # for debug
    # print(npy_result)

    npy_result = npy_result / 2.5
    #
    # # # Create Image
    dst_img = createResultImg(src_img, npy_result, npy_label, raibowColor=True)
    dst_img = createOverwrappedImage(src_img, dst_img, doGSConv=True)

    if SHOW_IMG:
        cv2.imshow("Test", dst_img)
        cv2.waitKey(0)
    return dst_img
예제 #6
0
    path_img_src = f"img/resource/{target_name}_aerial.png"
    path_img_mask = f"img/resource/{target_name}_mask.png"
    divided_dir = "img/divided/" + target_name

    print("    img: ", path_img_src)
    print("   mask: ", path_img_mask)
    print("divided: ", divided_dir)

    ########################
    ##### RESOURCE LOAD ####
    ########################
    img_src = cv2.imread(path_img_src, cv2.IMREAD_COLOR)
    img_mask = cv2.imread(path_img_mask, cv2.IMREAD_GRAYSCALE)

    # Get Mask Data
    npy_label = labeling_from_mask(img_mask)

    ########################
    # IMAGE PRE-PROCESSING #
    ########################
    img = denoise_by_median_and_meanshift(img_src)

    ########################
    #### IMAGE DIVISION ####
    ########################
    n_of_imgs = divide_by_mask(img, npy_label, target_name)

    #########################
    ## CALC EDGE FEATURES  ##
    #########################
    edge_features = EdgeProfiler.saveFeatureAsNPY(target_name,