Alb*_*ert 186 opencv image-processing computer-vision
我需要一种简单快速的方法来比较两个图像的相似性.即如果它们包含完全相同的东西但是可能有一些稍微不同的背景并且可能被移动/调整大小几个像素,我想获得高值.
(更具体的是,如果重要的话:一张图片是一个图标,另一张图片是截图的子区域,我想知道该子区域是否恰好是图标.)
我手边有OpenCV,但我仍然不习惯它.
到目前为止我想到的一种可能性:将两张图片分成10x10个单元格,对于这100个单元格中的每一个,比较颜色直方图.然后我可以设置一些补偿阈值,如果我得到的值高于该阈值,我认为它们是相似的.
我还没有尝试过它的效果如何,但我想它会足够好.图像已经非常相似(在我的用例中),所以我可以使用相当高的阈值.
我想有很多其他可能的解决方案可以或多或少地工作(因为任务本身非常简单,因为我只想检测相似性,如果它们非常相似).你会建议什么?
关于从图像中获取签名/指纹/哈希,有一些非常相关/类似的问题:
另外,我偶然发现了这些具有获取指纹功能的实现:
关于感知图像哈希的一些讨论:这里
有点offtopic:有很多方法来创建音频指纹.MusicBrainz是一种为歌曲提供基于指纹的查找的网络服务,在他们的维基中有很好的概述.他们现在正在使用AcoustID.这是为了找到精确(或大部分精确)的匹配.要查找类似的匹配(或者如果您只有一些片段或高噪音),请查看Echoprint.一个相关的SO问题在这里.所以这似乎解决了音频问题.所有这些解决方案都非常有效.
Kar*_*nek 104
屏幕截图或图标可以转换(缩放,旋转,倾斜......)吗?我脑子里有很多方法可以帮到你:
其中大部分已经在OpenCV中实现 - 例如参见cvMatchTemplate方法(使用直方图匹配):http://dasl.mem.drexel.edu/~noahKuntz/openCVTut6.html .突出点/面积检测器也可用 - 请参阅OpenCV特征检测.
Ste*_*ing 40
我最近面临同样的问题,为了解决这个问题(简单快速的算法比较两个图像)一劳永逸,我将一个img_hash模块贡献给opencv_contrib,你可以从这个链接中找到细节.
img_hash模块提供了六种图像哈希算法,非常好用.
代码示例
#include <opencv2/core.hpp>
#include <opencv2/core/ocl.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/img_hash.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
void compute(cv::Ptr<cv::img_hash::ImgHashBase> algo)
{
auto input = cv::imread("lena.png");
cv::Mat similar_img;
//detect similiar image after blur attack
cv::GaussianBlur(input, similar_img, {7,7}, 2, 2);
cv::imwrite("lena_blur.png", similar_img);
cv::Mat hash_input, hash_similar;
algo->compute(input, hash_input);
algo->compute(similar_img, hash_similar);
std::cout<<"gaussian blur attack : "<<
algo->compare(hash_input, hash_similar)<<std::endl;
//detect similar image after shift attack
similar_img.setTo(0);
input(cv::Rect(0,10, input.cols,input.rows-10)).
copyTo(similar_img(cv::Rect(0,0,input.cols,input.rows-10)));
cv::imwrite("lena_shift.png", similar_img);
algo->compute(similar_img, hash_similar);
std::cout<<"shift attack : "<<
algo->compare(hash_input, hash_similar)<<std::endl;
//detect similar image after resize
cv::resize(input, similar_img, {120, 40});
cv::imwrite("lena_resize.png", similar_img);
algo->compute(similar_img, hash_similar);
std::cout<<"resize attack : "<<
algo->compare(hash_input, hash_similar)<<std::endl;
}
int main()
{
using namespace cv::img_hash;
//disable opencl acceleration may(or may not) boost up speed of img_hash
cv::ocl::setUseOpenCL(false);
//if the value after compare <= 8, that means the images
//very similar to each other
compute(ColorMomentHash::create());
//there are other algorithms you can try out
//every algorithms have their pros and cons
compute(AverageHash::create());
compute(PHash::create());
compute(MarrHildrethHash::create());
compute(RadialVarianceHash::create());
//BlockMeanHash support mode 0 and mode 1, they associate to
//mode 1 and mode 2 of PHash library
compute(BlockMeanHash::create(0));
compute(BlockMeanHash::create(1));
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,ColorMomentHash会给我们带来最好的结果
每种算法的优缺点
img_hash的性能也很好
与PHash库进行速度比较(来自ukbench的100张图片)

如果您想知道这些算法的推荐阈值,请查看这篇文章(http://qtandopencv.blogspot.my/2016/06/introduction-to-image-hash-module-of.html).如果您对如何衡量img_hash模块的性能(包括速度和不同的攻击)感兴趣,请查看此链接(http://qtandopencv.blogspot.my/2016/06/speed-up-image-hashing-of -opencvimghash.html).
car*_*sdc 10
屏幕截图是否仅包含图标?如果是这样,两个图像的L2距离可能就足够了.如果L2距离不起作用,下一步就是尝试一些简单而完善的东西,例如:Lucas-Kanade.我确信在OpenCV中可用.
如果您可以确保您的模板(图标)与测试区域精确对齐,那么任何旧的像素差异总和都将起作用。
如果对齐只会有一点点偏差,那么您可以在找到像素差异的总和之前使用cv::GaussianBlur对两个图像进行低通。
如果对齐的质量可能很差,那么我会推荐定向梯度直方图或 OpenCV 方便的关键点检测/描述符算法之一(例如SIFT或SURF)。
如果你想获得关于两张图片的相似性的索引,我建议你从SSIM索引的指标.它与人眼更加一致.这是一篇关于它的文章:结构相似性指数
它也是在OpenCV中实现的,它可以通过GPU加速:带GPU的OpenCV SSIM
| 归档时间: |
|
| 查看次数: |
121757 次 |
| 最近记录: |