热点新闻
Android基于OpenCV实现相机实时图像检测
2023-07-05 18:09  浏览:950  搜索引擎搜索“手机速企网”
温馨提示:信息一旦丢失不一定找得到,请务必收藏信息以备急用!本站所有信息均是注册会员发布如遇到侵权请联系文章中的联系方式或客服删除!
联系我时,请说明是在手机速企网看到的信息,谢谢。
展会发布 展会网站大全 报名观展合作 软文发布

一.Android摄像头预览

Android OpenCV开发过程中,我们有3种可选方式去实现Android摄像头预览功能:

  • 使用Android系统Camera API
  • 使用CameraX(JetPack组件)
  • 使用OpenCV SDK辅助类(JavaCameraView、JavaCamera2View等)
利用Android OpenCV SDK实现相机预览

1.申明权限

<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" /> <uses-feature android:name="android.hardware.camera.front" android:required="false" /> <uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false" />

2.布局添加JavaCameraView或者JavaCamera2View至布局

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <org.opencv.android.JavaCamera2View android:id="@+id/camera_view" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" android:layout_width="match_parent" android:layout_height="match_parent"> </org.opencv.android.JavaCamera2View> <ImageView android:id="@+id/image_view" android:layout_width="match_parent" android:layout_height="match_parent"> </ImageView> </androidx.constraintlayout.widget.ConstraintLayout>

3.Activity继承CameraActivity,实现CameraBridgeViewbase.CvCameraViewListener或者CameraBridgeViewbase.CvCameraViewListener2接口,两个接口的差异主要集中在onCameraframe回调。

@Override public void onCameraViewStarted(int width, int height) { } @Override public void onCameraViewStopped() { } @Override public Mat onCameraframe(CameraBridgeViewbase.CvCameraViewframe inputframe) { // 获取相机中的图像 Mat rgba = inputframe.rgba(); Core.rotate(rgba, rgba, Core.ROTATE_90_CLOCKWISE); Bitmap bitmap = Bitmap.createBitmap(rgba.cols(), rgba.rows(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(rgba, bitmap); Bitmap location = location(bitmap); if (location==null){ return null; } runonUiThread(() -> image_view.setImageBitmap(location)); return null; }

4.复写方法getCameraViewList

@Override protected List<? extends CameraBridgeViewbase> getCameraViewList() { return Arrays.asList(mCVCamera); }

二.物体检测

1.在onCameraframe回调中拿到图像后做一次滤镜去除干扰再进行边缘检测,这时候边缘可能并不是连通的所以我们再对图像做一次膨胀操作。最后进行轮廓检测将检测到的轮廓绘制到原图上。

private Bitmap location(Bitmap bmp) { Mat originMat=new Mat(); Utils.bitmapToMat(bmp,originMat); Mat resultG = new Mat(); Mat result = new Mat(); Imgproc.GaussianBlur(originMat, resultG, new Size(3.0, 3.0), 0); Imgproc.Canny(resultG, result, 100.0, 220.0, 3); // 膨胀,连接边缘 Imgproc.dilate(result, result, new Mat(), new Point(-1,-1), 4, 1, new Scalar(1)); // Bitmap Bmp = Bitmap.createBitmap(result.cols(), result.rows(), Bitmap.Config.ARGB_8888); // Utils.matToBitmap(result,Bmp); List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(result, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); //有了轮廓之后,为了方便我们先将轮廓给画出来,这里的resultMat其实就是srcMat,为了区分用了Mat resultMat = srcMat.clone();, // 下面代码的意思是,把contours轮廓列表中的所有轮廓(-1表示所有轮廓)在,resultMat上画用黑色(new Scalar(0, 0, 0))粗细为10的线条画出来。 if (contours.isEmpty()){ return null; } Mat resultMat = resultG.clone(); double arcLength=0; int index=0; for (int i = 0; i < contours.size(); i++) { MatOfPoint2f source = new MatOfPoint2f(); source.fromList(contours.get(i).toList()); if (Imgproc.arcLength(source, true)>arcLength){ arcLength=Imgproc.arcLength(source, true); index=i; } } MatOfPoint matOfPoint = contours.get(index); MatOfPoint2f tempMat=new MatOfPoint2f(); Imgproc.approxPolyDP(new MatOfPoint2f(matOfPoint.toArray()), tempMat, Imgproc.arcLength(new MatOfPoint2f(matOfPoint.toArray()), true)*0.04, true); Point[] points = tempMat.toArray(); if (points.length!=4){ return null; } List<MatOfPoint> matOfPoints = new ArrayList<>(); matOfPoints.add(new MatOfPoint(tempMat.toArray())); Imgproc.drawContours(resultMat, matOfPoints, -1, new Scalar(0, 0, 255), 4); Bitmap resultBmp = Bitmap.createBitmap(resultMat.cols(), resultMat.rows(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(resultMat,resultBmp); return resultBmp; }

三.效果

没办法传视频。https://github.com/jzqCode/OpenCvDemo




111.jpg

发布人:4979****    IP:117.173.23.***     举报/删稿
展会推荐
让朕来说2句
评论
收藏
点赞
转发