Python 官方文档:入门教程 => 点击学习
实现步骤: 图像灰度化 边缘检测 根据Canny检测得出来的Mat寻找轮廓 算出最大轮廓周长or面积 根据获取到的最大轮廓下标进行轮廓绘制
基于Canny算法的边缘检测主要有5个步骤,依次是
高斯滤波、像素梯度计算、非极大值像素梯度抑制、滞后阈值处理和孤立弱边缘抑制
。Canny在有噪声的情况下表现好不好,取决于前面的降噪过程,可以手动做高斯处理提高识别率。
public static void Canny(Mat image, Mat edges, double threshold1, double threshold2, int apertureSize, boolean L2gradient)
使用
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;
}
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;
}
}
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;
}
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();
}
最终效果图
获得矩形坐标点以后,后期可以做裁剪,旋转之类操作,可以自行研究。
到此这篇关于Android+OpenCV4实现边缘检测及轮廓绘制出图像最大边缘的文章就介绍到这了,更多相关Android OpenCv4边缘检测内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Android+OpenCv4实现边缘检测及轮廓绘制出图像最大边缘
本文链接: https://www.lsjlt.com/news/126219.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0