[编辑] 我设计了一些图片比较代码。匹配部分仍有些缺陷,希望得到一些帮助。该项目可以在GitHub上找到。
我有这两个图片 Img1 和 Img2:
当我在OpenCV中使用以下命令时:
Mat img1 = Highgui.imread("mnt/sdcard/IMG-20121228.jpg");
Mat img2 = Highgui.imread("mnt/sdcard/IMG-20121228-1.jpg");
try{
double l2_norm = Core.norm( img1, img2 );
tv.setText(l2_norm+"");
} catch(Exception e) {
//image is not a duplicate
}
对于 l2_norm,我得到了一个双精度浮点数。对于重复的图像对,此双精度浮点数会有所变化。但是,如果图像不同,则会抛出异常。这是我如何识别重复图像的方法吗?还是有更好的方法?我进行了广泛的谷歌搜索,但没能找到真正令人信服的答案。我想要代码和解释,以便比较两个图像并根据它们获得布尔值 true
或 false
。
编辑
Scalar blah= Core.sumElems(img2);
Scalar blah1=Core.sumElems(img1);
if(blah.equals(blah1))
{
tv.setText("same image");
}
}
我尝试过这个方法,但是if
条件从未满足。我猜可能存在一些差异,但是对于Scalar
没有compare
函数。我该怎么办?
编辑
try{
Scalar blah= Core.sumElems(img2);
Scalar blah1=Core.sumElems(img1);
String b=blah.toString();
String b1=blah1.toString();
System.out.println(b+" "+b1);
double comp=b.compareTo(b1);
tv.setText(""+comp);
}
这种方法存在缺陷。虽然可以用来比较图像并得到较高的准确性,但在图像大小不同时会失败。
当图像大小不同时,打印标量值如下:
[9768383.0, 1.0052889E7, 1.0381814E7, 0.0] [1.5897384E7, 1.6322252E7, 1.690251E7, 0.0]
第二个和第三个数字的变化虽然不大,但与比较同一大小的图像相比仍然很大。然而,第一个数字受到最大的变化。
有什么最好最快的方法来比较两个图像的内容?
[编辑]
我正在使用我在这里找到的代码。
我无法弄清楚如何初始化MatOfKeyPoint
变量keypoints
和logoKeypoints
。 这是我的代码片段:
FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
//FeatureDetector detector = FeatureDetector.create(FeatureDetector.FAST);
//Imgproc.cvtColor(img1, img1, Imgproc.COLOR_RGBA2RGB);
//Imgproc.cvtColor(img2, img2, Imgproc.COLOR_RGBA2RGB);
DescriptorExtractor SurfExtractor = DescriptorExtractor
.create(DescriptorExtractor.SURF);
//extract keypoints
MatOfKeyPoint keypoints, logoKeypoints;
long time= System.currentTimeMillis();
detector.detect(img1, keypoints);
Log.d("LOG!", "number of query Keypoints= " + keypoints.size());
detector.detect(img2, logoKeypoints);
Log.d("LOG!", "number of logo Keypoints= " + logoKeypoints.size());
Log.d("LOG!", "keypoint calculation time elapsed" + (System.currentTimeMillis() -time));
//Descript keypoints
long time2 = System.currentTimeMillis();
Mat descriptors = new Mat();
Mat logoDescriptors = new Mat();
Log.d("LOG!", "logo type" + img2.type() + " intype" + img1.type());
SurfExtractor.compute(img1, keypoints, descriptors);
SurfExtractor.compute(img2, logoKeypoints, logoDescriptors);
Log.d("LOG!", "Description time elapsed" + (System.currentTimeMillis()- time2));
显然我不能将变量keypoints
和logoKeypoints
初始化为null,因为我会收到一个空指针异常。那么我该如何初始化它们呢?