当前位置:网站首页>Interesting opencv - record image binarization and similarity
Interesting opencv - record image binarization and similarity
2022-08-03 20:45:00 【Public number: arigeweixin】
一、背景:
前面的文章提到,要实现两个功能
实现数字识别 Implement icon recognition
The previous article has already realized the recognition of numbers,However, the recognition rate was found to be relatively low,And the recognition error rate is also relatively high.It is considered because the background is more complicated and affects the recognition effect,This paper mainly addresses the simplification of complex backgrounds,and comparison of pictures
二、目标:
通过opencv对图片处理,Make the background and content more distinct,即二值化 通过opencvCompare the similarity of the binarized images,to identify the icon
三、实现过程:
opencv的集成
1. 下载opencv对应的版本 https://opencv.org/releases/
我使用了最新的4.6.0 的版本
2. 解压下载的zip得到demoand needs to be integratedmodule
3. Access your own projects
3.1、将2中得到的sdkas an independentmodule放到项目中
3.2、在项目的setting.gradle中引入sdk module
3.3、 在使用opencv的module中引入sdk module
4. 编译使用
The integration is now complete,But because of this usendk相关的功能,如果本地没有ndk环境的话,Environmental issues may need to be addressed,这里不再赘述
二值化处理
1. 先上代码
public static Bitmap createBitmap(Bitmap bitmap) {
Mat src = new Mat();
Utils.bitmapToMat(bitmap, src); //将bitmap转换为Mat
Mat thresholdImage = new Mat(src.size(), src.type()); //This binary image is used to find out the key information of the image
//将图像转换为灰度图像
Imgproc.cvtColor(src, thresholdImage, Imgproc.COLOR_RGBA2GRAY);
//Convert the image to an edge binary image
Imgproc.threshold(thresholdImage,thresholdImage,10.0,255.0, Imgproc.THRESH_BINARY_INV|Imgproc.THRESH_OTSU);
Bitmap binaryBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
Utils.matToBitmap(thresholdImage, binaryBitmap);
return binaryBitmap;
}
2. 步骤拆解
2.1、先将bitmap转为 Mat方便opencv进行操作
2.2、 调用 Imgproc.cvtColor(src, thresholdImage, Imgproc.COLOR_RGBA2GRAY);
将图像转为灰度图像
2.3、 调用 ` Imgproc.threshold(thresholdImage,thresholdImage,10.0,255.0, Imgproc.THRESH_BINARY_INV|Imgproc.THRESH_OTSU);
` Convert the image to a binary image. 这个方法非常的重要,Specifically speaking here
1. The first parameter is the image source
2. 第二个参数是输出的图像
3. 是一个标准.Each pixel is compared against this criterion.This will be used in conjunction with the last parameter.
4. In cases where the criteria for the third argument are met,要赋予的值
5. The fifth parameter controls the third、The usage of the fourth parameter
3. 效果展示
Similarity processing
1. 上代码
public static Double similarity(Bitmap bitmap1, Bitmap bitmap2){
// 计算每张图片的特征点
MatOfKeyPoint descriptors1 = computeDescriptors(bitmap1);
MatOfKeyPoint descriptors2 = computeDescriptors(bitmap2);
// 比较两张图片的特征点
DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
List<MatOfDMatch> matches =new ArrayList();
// 计算大图中包含多少小图的特征点.
// 如果计算小图中包含多少大图的特征点,结果会不准确.
// 比如:若小图中的 50 个点都包含在大图中的 100 个特征点中,则计算出的相似度为 100%,显然不符合我们的预期
if (bitmap1.getByteCount() > bitmap2.getByteCount() ) {
descriptorMatcher.knnMatch(descriptors1, descriptors2, matches, 2);
} else {
descriptorMatcher.knnMatch(descriptors2, descriptors1, matches, 2);
}
Log.i("~~~", "matches.size: ${matches.size}");
if (matches.isEmpty()) return 0.00;
// 获取匹配的特征点数量
int matchCount = 0;
// 邻近距离阀值,这里设置为 0.7,该值可自行调整
float nndrRatio = 0.7f;
for (MatOfDMatch match:matches) {
DMatch[] array = match.toArray();
// 用邻近距离比值法(NNDR)计算匹配点数
if (array[0].distance <= array[1].distance * nndrRatio) {
matchCount++;
}
}
Log.i("~~~", "matchCount: $matchCount");
return Double.valueOf(matchCount/ matches.size());
}
2. 步骤拆解
2.1、 Find the feature points of the two images
private static MatOfKeyPoint computeDescriptors(Bitmap bitmap){
Mat mat = new Mat();
Utils.bitmapToMat(bitmap, mat);
MatOfKeyPoint keyPoints = new MatOfKeyPoint();
siftDetector.detect(mat, keyPoints);
MatOfKeyPoint descriptors = new MatOfKeyPoint();
// 计算图片的特征点
siftDetector.compute(mat, keyPoints, descriptors);
return descriptors;
}
2.2、 Choose the appropriate match mode
2.3、避免 Scenarios where the inclusion of a small image in a large image leads to the belief that the small image exactly matches the large image,So judge the size,Compare large and small images.
2.4、 Choose a proximity distance threshold
2.5、 Compare the difference in the eigenvalues of the two plots,,Record the number of differences that meet the threshold
2.6、 符合阈值的个数,than the total number of features,Get match rate
总结
opencvProvides a complete set of very completeapi,It can solve most of the scenarios we encounter,You can read more documents,学习起来.
关注公众号: arigeweixin ,取得更多联系
边栏推荐
猜你喜欢
汉源高科8光口12电口交换机千兆8光8电12电16电网管型工业以太网交换机
ESP8266-Arduino编程实例-BH1750FVI环境光传感器驱动
云服务器如何安全使用本地的AD/LDAP?
2022年强网杯rcefile wp
15年软件架构师经验总结:在ML领域,初学者踩过的五个坑
Advantages and Disadvantages of Blind and Buried Via PCB Stacked Via Design
检测和控制影子IT的五个步骤
leetcode 231. Powers of 2
tRNA修饰2-甲基胞嘧啶(m2C)|tRNA修饰m2G (N2-methylguanosine)
敏捷交付的工程效能治理
随机推荐
太香了! 阿里 Redis 速成笔记, 从头到尾全是精华!
详解虚拟机!京东大佬出品 HotSpot VM 源码剖析笔记(附完整源码)
RNA-ATTO 390|RNA-ATTO 425|RNA-ATTO 465|RNA-ATTO 488|RNA-ATTO 495|RNA-ATTO 520近红外荧光染料标记核糖核酸RNA
染料修饰核酸RNA|[email protected] 610/[email protected] 594/Alexa 56
leetcode 1837. The sum of the digits in the K-base representation
Leetcode 16. Numerical integral power (power + fast recursive/iteration)
系统运维系列 之CSV文件读取时内容中包含逗号的处理方法
canvas螺旋动画js特效
Android build error: Plugin with id ‘kotlin-android‘ not found.
Auto.js脚本程序打包
2022年1~7月语音合成(TTS)和语音识别(ASR)论文月报
ESP8266-Arduino编程实例-MCP4725数模转换器驱动
Alexa染料标记RNA核糖核酸|RNA-Alexa 514|RNA-Alexa 488|RNA-Alexa 430
力扣203-移除链表元素——链表
华为设备配置VRRP负载分担
有趣的opencv-记录图片二值化和相似度实现
wordpress建立数据库连接时出错
Markdown语法
Go语言类型与接口的关系
leetcode 461. 汉明距离