Android OpenCV图像识别和图像追踪

首先介绍一下OpenCV中图像识别和跟踪机制:
       图像跟踪机制是确定矩目标在3D环境中的姿态,并根据此类信息环绕目标对象绘制轮廓线。在最终的2D图像中,考虑到目标可能相对于相机倾斜,因而轮廓线将呈现为四边形(不一定是矩形)。
       上述跟踪机制主要包含以下四个步骤:
       (1)获取目标特征。这里特征是指从不同的距离或角度进行观察室,维持特征的外观的一点。例如,各角通常具备此类特征。
       (2)针对各特征集获取描述符,此处,描述符表示为与特征相关的数据向量。需要注意的是,某些特征并不适合生成描述符,因此与特征相比,图像包含较少的描述符。
       (3)获取两个描述符集合间的匹配项。若假设将描述符视为多维空间的数据点,则匹配结果将根据点间的距离加以定义。其中,彼此接近的描述符视为匹配。
       (4)获取参考图像和空间匹配图像间的单应性(homography)。这里,单应性是指一类3D转换,并可排列两幅投影后的2D图像(或足够接近以对二者进行排列)。该过程根据两幅图像的匹配特征点进行计算。当对矩形采用但应性时,即可获得跟踪图像的轮廓线。
        关于本文中相机缓冲和Filter的使用请参考:
 Android OpenCV获取相机并拍(Android Studio)
 Android OpenCV添加图像效果

    言归正传,下面介绍图像检测的ImageDetectionFilter,先上构造方法:
 public ImageDetectionFilter(Context context, int referenceImageResourceID) throws IOException {

// Load the reference image from the app's resources.
    // It is loaded in BGR (blue, green, red) format.
mReferenceImage = Utils.loadResource(context, referenceImageResourceID, Imgcodecs.CV_LOAD_IMAGE_COLOR);

// Create grayscale and RGBA versions of the reference image.
Mat referenceImageGray = new Mat();
Imgproc.cvtColor(mReferenceImage, referenceImageGray, Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(mReferenceImage, mReferenceImage, Imgproc.COLOR_BGR2RGBA);

// Store the reference image's corner coordinates, in pixels.
mReferenceCorners.put(0, 0, new double[]{0.0, 0.0});
mReferenceCorners.put(1, 0, new double[]{referenceImageGray.cols(), 0.0});
mReferenceCorners.put(2, 0, new double[]{referenceImageGray.cols(), referenceImageGray.rows()});
mReferenceCorners.put(3, 0, new double[]{0.0, referenceImageGray.rows()});

// Detect the reference features and compute their
    // descriptors.
mFeatureDetector.detect(referenceImageGray, mReferenceKeypoints);
mDescriptorExtractor.compute(referenceImageGray, mReferenceKeypoints, mReferenceDescriptors);
}
       代码中首先根据传入的参考图像资源生成Mat对象,然后生成灰度图和RGBA存储在成员变量中,另外,将图像的角点,特征以及描述符也一并存储。

        初始化完成以后,接下来就是apply方法,该方法在获取每一帧时都会被调用,进行图像检测,代码如下:
 @Override
public void apply(Mat src, Mat dst) {

// Convert the scene to grayscale.
Imgproc.cvtColor(src, mGraySrc, Imgproc.COLOR_RGBA2GRAY);

// Detect the scene features, compute their descriptors,
    // and match the scene descriptors to reference descriptors.
mFeatureDetector.detect(mGraySrc, mSceneKeypoints);
mDescriptorExtractor.compute(mGraySrc, mSceneKeypoints, mSceneDescriptors);
mDescriptorMatcher.match(mSceneDescriptors, mReferenceDescriptors, mMatches);

// Attempt to find the target image's corners in the scene.
findSceneCorners();

// If the corners have been found, draw an outline around the
    // target image.
    // Else, draw a thumbnail of the target image.
draw(src, dst);
}
        在该方法中,首先向获取到的图像取灰度图,接着讲灰度图进行特征检测,描述符检测以及描述符匹配。随后,调用findSceneCorners方法获取跟踪目标的四个角(若存在的话),并绘制四边形轮廓。最后调用draw()方法,在左上角绘制目标图像的缩略图。

项目Github地址:https://github.com/BruceT2010/OpenCV4AndroidSecondSight
        
--------------------- 
版权声明:本文为CSDN博主「BruceT2010」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u012500046/article/details/53642182

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值