バイナリグラフに基づくコーナー検出

簡単な説明

ここに画像の説明を挿入

cv2.goodFeaturesToTrack()関数は、検出された画像のコーナーを追跡するために使用されます

パラメータ

  • image:入力画像は8ビットまたは32ビットの浮動小数点シングルチャネル画像であるため、グレースケール画像maxCorners:
    コーナーポイントの最大数を返します。これは、コーナーポイントの可能性が最も高い数です。このパラメータが0以下の場合、コーナーポイントの数に制限がないことを意味します。
  • qualityLevel:画像のコーナーポイントの最小許容パラメータ。このパラメータを掛けた品質測定値が最小特性値であり、この数値よりも小さい値は破棄されます。
  • minDistance:返されたコーナーポイント間の最小ユークリッド距離。
  • マスク:検出領域。画像が空でない場合(CV_8UC1タイプで画像と同じサイズである必要があります)、検出角度の領域を指定します。
  • blockSize:各ピクセルの近傍の微分共分散行列の平均ブロックサイズを計算するために使用されます。
  • useHarrisDetector:ハリスコーナー検出を使用するかどうかを選択します。デフォルトはfalseです。k:ハリス検出の自由パラメーター。

コード

import cv2
import numpy as np
import scipy.ndimage
import skimage.morphology
import os
import gdalTools


def good_feature_to_track(thin_mask, mask, out_name, save_path):
    """
     Apply the detector on the segmentation map to detect the road junctions as starting points for tracing.
    :param thin_mask: one-pixel width segmentation map
    :param mask: road segmentation map
    :param out_name: filename
    :param save_path: the directory of corner detection results
    :return:
    """
    # set a padding to avoid image edge corners
    padding_x = 64+5
    padding_y = 64

    corners = cv2.goodFeaturesToTrack(thin_mask, 100, 0.1, 10)
    corners = np.int0(corners)
    img = np.zeros((mask.shape[0], mask.shape[1], 3))
    img[:, :, 0] = mask
    img[:, :, 1] = mask
    img[:, :, 2] = mask
    corner_num = 0
    with open(save_path+out_name[:-4]+".txt", "w") as f:
        for i in corners:
            x, y = i.ravel()
            if x < padding_x or x > img.shape[0]-padding_x:
                continue
            if y < padding_y or y > img.shape[1]-padding_y:
                continue

            f.write("{},{}\n".format(x,y))
            cv2.circle(img, (x, y), 6, (0, 0, 255), -1)
            corner_num += 1
    print("total corners number:{}".format(corner_num))
    # cv2.imwrite(save_path+out_name[:-4]+'_with_corners.png', img)
    return img


def thin_image(mask_dir, filename):
    """
    Skeletonize the road segmentation map to a one-pixel width
    :param mask_dir: the directory of road segmentation map
    :param filename: the filename of road segmentation map
    :return: one-pixel width segmentation map
    """
    im = scipy.ndimage.imread(mask_dir + filename)
    im = im > 128
    selem = skimage.morphology.disk(2)
    im = skimage.morphology.binary_dilation(im, selem)
    im = skimage.morphology.thin(im)
    return im.astype(np.uint8) * 255


def mkdir(path):
    if not os.path.exists(path):
        os.mkdir(path)


if __name__ == '__main__':
    import matplotlib.pyplot as plt
    txt_dir = 'corners'
    mkdir(txt_dir)
    mask_dir = 'train_labels/'
    mask_filename = '000000011.tif'
    thin_img = thin_image(mask_dir, mask_filename)
    # mask = cv2.imread(mask_dir + mask_filename, 0)
    im_proj, im_geotrans, im_width, im_height, mask = gdalTools.read_img(mask_dir + mask_filename)
    img = good_feature_to_track(thin_img, mask, mask_filename, txt_dir)
    plt.subplot(121)
    plt.imshow(thin_img * 255)
    plt.subplot(122)
    plt.imshow(img)
    plt.show()

おすすめ

転載: blog.csdn.net/weixin_42990464/article/details/114283709