基于二值图的角点检测

网友投稿 896 2022-11-23

基于二值图的角点检测

基于二值图的角点检测

简述

cv2.goodFeaturesToTrack()函数是用来跟踪检测图像中的角点

参数

image: 输入图像,是八位的或者32位浮点型,单通道图像,所以有时候用灰度图 maxCorners:返回最大的角点数,是最有可能的角点数,如果这个参数不大于0,那么表示没有角点数的限制。qualityLevel:图像角点的最小可接受参数,质量测量值乘以这个参数就是最小特征值,小于这个数的会被抛弃。minDistance:返回的角点之间最小的欧式距离。mask: 检测区域。如果图像不是空的(它需要具有CV_8UC1类型和与图像相同的大小),它指定检测角的区域。blockSize:用于计算每个像素邻域上的导数协变矩阵的平均块的大小。useHarrisDetector:选择是否采用Harris角点检测,默认是false. k: Harris检测的自由参数。

代码

import cv2import numpy as npimport scipy.ndimageimport skimage.morphologyimport osimport gdalToolsdef 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 imgdef 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) * 255def 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()

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:plt.ion()画动态图
下一篇:基于遥感影像的道路完整性与连接性评价指标
相关文章

 发表评论

暂时没有评论,来抢沙发吧~