Android opencv(四)轮廓检测
来源:互联网 发布:在手机上怎么注册淘宝 编辑:程序博客网 时间:2024/06/05 14:50
轮廓检测函数:
Imgproc. findContours(image, contours, hierarchy, mode, method)
通过轮廓检测找到面积最大的轮廓并绘制轮廓:
List<MatOfPoint> contours=new ArrayList<>(); Imgproc.findContours(blurredImage,contours,new Mat(),Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE); double maxVal = 0; int maxValIdx = 0; for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) { double contourArea = Imgproc.contourArea(contours.get(contourIdx)); if (maxVal < contourArea) { maxVal = contourArea; maxValIdx = contourIdx; } } Mat mRgba=new Mat(); mRgba.create(blurredImage.rows(), blurredImage.cols(), CvType.CV_8UC3); //绘制检测到的轮廓 Imgproc.drawContours(mRgba, contours, maxAreaIdx, new Scalar(0,255,0), 5);
//通过轮廓检测获取4个交点
public static List<Point> getCornersByContour(Mat imgsource){ List<MatOfPoint> contours=new ArrayList<>(); //轮廓检测 Imgproc.findContours(imgsource,contours,new Mat(),Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE); double maxArea=-1; int maxAreaIdx=-1; MatOfPoint temp_contour=contours.get(0);//假设最大的轮廓在index=0处 MatOfPoint2f approxCurve=new MatOfPoint2f(); for (int idx=0;idx<contours.size();idx++){ temp_contour=contours.get(idx); double contourarea=Imgproc.contourArea(temp_contour); //当前轮廓面积比最大的区域面积大就检测是否为四边形 if (contourarea>maxArea){ //检测contour是否是四边形 MatOfPoint2f new_mat=new MatOfPoint2f(temp_contour.toArray()); int contourSize= (int) temp_contour.total(); MatOfPoint2f approxCurve_temp=new MatOfPoint2f(); //对图像轮廓点进行多边形拟合 Imgproc.approxPolyDP(new_mat,approxCurve_temp,contourSize*0.05,true); if (approxCurve_temp.total()==4){ maxArea=contourarea; maxAreaIdx=idx; approxCurve=approxCurve_temp; } } } double[] temp_double=approxCurve.get(0,0); Point point1=new Point(temp_double[0],temp_double[1]); temp_double=approxCurve.get(1,0); Point point2=new Point(temp_double[0],temp_double[1]); temp_double=approxCurve.get(2,0); Point point3=new Point(temp_double[0],temp_double[1]); temp_double=approxCurve.get(3,0); Point point4=new Point(temp_double[0],temp_double[1]); List<Point> source=new ArrayList<>(); source.add(point1); source.add(point2); source.add(point3); source.add(point4); //对4个点进行排序 Point centerPoint=new Point(0,0);//质心 for (Point corner:source){ centerPoint.x+=corner.x; centerPoint.y+=corner.y; } centerPoint.x=centerPoint.x/source.size(); centerPoint.y=centerPoint.y/source.size(); Point lefttop=new Point(); Point righttop=new Point(); Point leftbottom=new Point(); Point rightbottom=new Point(); for (int i=0;i<source.size();i++){ if (source.get(i).x<centerPoint.x&&source.get(i).y<centerPoint.y){ lefttop=source.get(i); }else if (source.get(i).x>centerPoint.x&&source.get(i).y<centerPoint.y){ righttop=source.get(i); }else if (source.get(i).x<centerPoint.x&& source.get(i).y>centerPoint.y){ leftbottom=source.get(i); }else if (source.get(i).x>centerPoint.x&&source.get(i).y>centerPoint.y){ rightbottom=source.get(i); } } source.clear(); source.add(lefttop); source.add(righttop); source.add(leftbottom); source.add(rightbottom); return source; }
阅读全文
0 0
- Android opencv(四)轮廓检测
- OpenCV 轮廓检测
- OpenCV轮廓检测
- OpenCV轮廓检测
- OpenCV轮廓检测
- OpenCV 轮廓检测
- Opencv轮廓检测
- Opencv 轮廓检测 直线
- OpenCV 轮廓检测
- OpenCV 矩形轮廓检测
- OpenCV 闭合轮廓检测
- OpenCV 轮廓检测
- Opencv轮廓查找检测
- OpenCV检测图像轮廓
- python-opencv-轮廓检测
- OpenCV之轮廓检测
- Python-OpenCV轮廓检测
- OpenCV例子学习轮廓检测
- 通过mvn dependency:tree 查看依赖树,解决依赖jar冲突问题
- hadoop学习(七)----mapReduce原理以及操作过程
- python发送邮件学习
- RecyclerView添加虚线分割线
- Eclipse中关闭html或jsp页面的视图窗口
- Android opencv(四)轮廓检测
- WEB-INF和META-INF目录详解
- 用eclipse开发工具,maven进行包管理,整合SSH框架
- JSP、Servlet中的相对路径和绝对路径 页面跳转问题
- Leetcode | Delete and Earn
- 二叉树的简介
- Qt 5.0.2 (32 bit) 编译错误
- 【2017-12-17】 线程笔记
- 【TensorFlow】数据处理(队列和多线程)