Android+OpenCv4实现边缘检测及轮廓绘制出图像最大边缘
作者:Best-dadong 时间:2022-04-08 18:05:44
实现步骤:
图像灰度化
边缘检测
根据Canny检测得出来的Mat寻找轮廓
算出最大轮廓周长or面积
根据获取到的最大轮廓下标进行轮廓绘制
画出最大矩形,并返回Rect
Canny边缘检测
基于Canny算法的边缘检测主要有5个步骤,依次是
高斯滤波、像素梯度计算、非极大值像素梯度抑制、滞后阈值处理和孤立弱边缘抑制
。Canny在有噪声的情况下表现好不好,取决于前面的降噪过程,可以手动做高斯处理提高识别率。
/**
image 输入图像,必须是CV_8U的单通道或者三通道图像。
edges 输出图像,与输入图像具有相同尺寸的单通道图像,且数据类型为CV_8U。
threshold1 第一个滞后阈值。
threshold2 第二个滞后阈值。
apertureSize Sobel算子的直径。
L2gradient 计算图像梯度幅值方法的标志。默认为false
**/
public static void Canny(Mat image, Mat edges, double threshold1, double threshold2, int apertureSize, boolean L2gradient)
使用
/**
* canny算法,边缘检测
*/
public static Mat canny(Bitmap bitmap) {
Mat mSource = new Mat();
Utils.bitmapToMat(bitmap, mSource);
Mat grayMat = new Mat();
Imgproc.cvtColor(mSource,grayMat,Imgproc.COLOR_BGR2GRAY);//转换成灰度图
Mat mat = mSource.clone();
Imgproc.Canny(mSource, mat, 75, 200);
return mat;
}
获取图像最大矩形
/**
* 返回边缘检测之后的最大矩形,并返回
*
* @param cannyMat
* Canny之后的mat矩阵
* @return
*/
public Rect findMaxRect(Mat cannyMat) {
Mat tmp = mSource.clone();
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
// 寻找轮廓
Imgproc.findContours(cannyMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
int index = 0;
double perimeter = 0;
// 找出匹配到的最大轮廓
for (int i = 0; i < contours.size(); i++) {
// 最大面积
// double area = Imgproc.contourArea(contours.get(i));
//最大周长
MatOfPoint2f source = new MatOfPoint2f();
source.fromList(contours.get(i).toList());
double length = Imgproc.arcLength(source,true);
if(length>perimeter){
perimeter = length;
index = i;
}
}
/**
* 参数一:image,待绘制轮廓的图像。
*
* 参数二:contours,待绘制的轮廓集合。
*
* 参数三:contourIdx,要绘制的轮廓在contours中的索引,若为负数,表示绘制全部轮廓。
*
* 参数四:color,绘制轮廓的颜色。
*
* 参数五:thickness,绘制轮廓的线条粗细。若为负数,那么绘制轮廓的内部。
*
* 参数六:lineType,线条类型。FILLED LINE_4 4连通 LINE_8 8连通 LINE_AA 抗锯齿
*/
Imgproc.drawContours(
tmp,
contours,
index,
new Scalar(0.0, 0.0, 255.0),
9,
Imgproc.LINE_AA
);
Rect rect = Imgproc.boundingRect(contours.get(index));
// Imgproc.rectangle(tmp, rect, new Scalar(0.0, 0.0, 255.0), 4, Imgproc.LINE_8);
showImg(tmp);
return rect;
}
/**
* 显示图像
* @param mat
*/
private void showImg(Mat mat){
Bitmap bitmap = Bitmap.createBitmap(mat.width(), mat.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mat, bitmap);
mIvSrc.setImageBitmap(bitmap);
mat.release();
}
最终效果图
获得矩形坐标点以后,后期可以做裁剪,旋转之类操作,可以自行研究。
来源:https://blog.csdn.net/TLuffy/article/details/116602265
标签:Android,OpenCv4,边缘检测,最大边缘
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Oracle 10G:PL/SQL正规表达式(正则表达式)手册
2009-07-02 12:33:00
python anaconda 安装 环境变量 升级 以及特殊库安装的方法
2022-11-05 01:56:24
Asp DatePart 函数的语法详解(用于计算日期并返回指定的时间间隔)
2012-12-04 20:04:29
30个出色的分页设计
2009-05-12 17:49:00
![](https://img.aspxhome.com/file/UploadPic/20095/12/220023a63-30s.jpg)
python+rsync精确同步指定格式文件
2023-09-18 06:51:26
SQL Server 压缩日志及数据库文件大小
2009-05-13 10:29:00
从SQL Server2000升级到2005的过程解析
2009-01-13 14:07:00
如何在SQL Server数据库中加密数据
2008-12-18 14:27:00
基于标准的web项目开发模式探索
2013-12-12 15:05:25
![](https://img.aspxhome.com/file/UploadPic/20071/200713110544641s.gif)
asp 过滤尖括号内所有内容的正则代码
2011-04-03 10:40:00
增加 javascript 的 trim 函数
2008-04-20 16:54:00
关于MySQL编码问题的经验总结
2007-08-23 16:10:00
python特性语法之遍历、公共方法、引用
2023-05-17 16:21:29
php随机取mysql记录方法小结
2023-11-22 22:27:22
完美解决ARIMA模型中plot_acf画不出图的问题
2023-07-13 14:17:34
![](https://img.aspxhome.com/file/2023/1/61761_0s.jpg)
Go 值传递与引用传递的方法
2023-06-25 03:11:11
![](https://img.aspxhome.com/file/2023/3/89023_0s.png)
支付宝lab logo设计创意发想
2009-11-12 12:44:00
![](https://img.aspxhome.com/file/UploadPic/200911/12/blog1-38s.jpg)
Oracle 10g的DBA无法登录解决方案
2009-05-24 19:38:00
如何获知用户的IP?
2009-11-24 20:52:00
十万条Access数据表分页的两个解决方法
2008-05-23 18:24:00