Hello..
I am implementing a java program that highlights the difference between two images.
I used the following steps :
- turning the images into gray scale
- apply feature detection to the images in order to check if they are similar
- applying absdiff to them
- highlighting the difference in the original picture
My problem is : when I upload two images that are similar but one of them have outliers, or the position is slightly different , the whole image become different
the code is : ` package image;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.*;
import org.opencv.features2d.*;
import org.opencv.features2d.DMatch;
import org.opencv.features2d.KeyPoint;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
public class SURFDetector22 {
boolean answer;
//copy matrix
public static Mat imitate(Mat m){
return new Mat(m.height(), m.width(), m.type());
}
//draw rect
public static void drawRotatedRect(Mat image, RotatedRect rotatedRect, Scalar color, int thickness) {
Point[] vertices = new Point[4];
rotatedRect.points(vertices);
MatOfPoint points = new MatOfPoint(vertices);
Imgproc.drawContours(image, Arrays.asList(points), -1, color, thickness);
}
//diff btw matrices
public static Mat diff(Mat mat1, Mat mat2){
Mat dst = imitate(mat1);
Core.absdiff(mat1, mat2, dst);
return dst;}
public void imageComparison(File file1, File file2) throws IOException {
File lib = null;
String os = System.getProperty("os.name");
String bitness = System.getProperty("sun.arch.data.model");
if (os.toUpperCase().contains("WINDOWS")) {
if (bitness.endsWith("64")) {
lib = new File("libs//x64//" + System.mapLibraryName("opencv_java2411"));
} else {
lib = new File("libs//x86//" + System.mapLibraryName("opencv_java2411"));
}
}
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
///////step 1 : read images
///first image
String bookObject = file1.getAbsolutePath();
Mat objectImage = Highgui.imread(bookObject, Highgui.CV_LOAD_IMAGE_COLOR);
Mat mat1 = new Mat(objectImage.height(),objectImage.width(),CvType.CV_8UC1);
Imgproc.cvtColor(objectImage, mat1, Imgproc.COLOR_RGB2GRAY);
byte[] data1 = new byte[mat1.rows() * mat1.cols() * (int)(mat1.elemSize())];
mat1.get(0, 0, data1);
BufferedImage image1 = new BufferedImage(mat1.cols(),mat1.rows(), BufferedImage.TYPE_BYTE_GRAY);
image1.getRaster().setDataElements(0, 0, mat1.cols(), mat1.rows(), data1);
File ouptut = new File("gray//1.jpg");
ImageIO.write(image1, "jpg", ouptut);
//bade a3ml show la hyde el sora
//2nd image
String bookScene = file2.getAbsolutePath();
Mat sceneImage = Highgui.imread(bookScene, Highgui.CV_LOAD_IMAGE_COLOR);
Mat mat2 = new Mat(sceneImage.height(),sceneImage.width(),CvType.CV_8UC1);
Imgproc.cvtColor(sceneImage, mat2, Imgproc.COLOR_RGB2GRAY );
byte[] data2 = new byte[mat2.rows() * mat2.cols() * (int)(mat2.elemSize())];
mat2.get(0, 0, data2);
BufferedImage image2 = new BufferedImage(mat2.cols(),mat2.rows(), BufferedImage.TYPE_BYTE_GRAY);
image2.getRaster().setDataElements(0, 0, mat2.cols(), mat2.rows(), data2);
File ouptut2 = new File("gray//2.jpg");
ImageIO.write(image2, "jpg", ouptut2);
//////step 2 : Detect and extract features
MatOfKeyPoint objectKeyPoints = new MatOfKeyPoint();
FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.SURF);
System.out.println("Detecting key points...");
featureDetector.detect(mat1, objectKeyPoints);
System.out.println("----- key pointss image 1----" + objectKeyPoints);
//KeyPoint[] keypoints = objectKeyPoints.toArray();
System.out.println(objectKeyPoints ...
(more)