Skip to content

Commit

Permalink
feat: init_new_package
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouYixun committed Sep 30, 2022
1 parent d553e75 commit caff805
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 72 deletions.
60 changes: 49 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,59 @@
<version>1.5.7</version>
<exclusions>
<exclusion>
<groupId>org.openifx</groupId>
<groupId>org.bytedeco</groupId>
<artifactId>artoolkitplus</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>flandmark</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>leptonica</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>tesseract</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>librealsense2</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>librealsense</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>libfreenect2</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>libfreenect</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>libdc1394</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>flycapture</artifactId>
</exclusion>
<exclusion>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
</exclusion>
<exclusion>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
</exclusion>
<exclusion>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>openblas</artifactId>
<version>0.3.19-1.5.7</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv</artifactId>
<version>4.5.5-1.5.7</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/org/cloud/sonic/vision/cv/AKAZEFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.cloud.sonic.vision.tool.Logger;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
Expand All @@ -38,7 +37,8 @@
import static org.bytedeco.opencv.global.opencv_calib3d.findHomography;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_flann.FLANN_DIST_HAMMING;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;
import static org.bytedeco.opencv.global.opencv_imgcodecs.IMREAD_COLOR;
import static org.bytedeco.opencv.global.opencv_imgcodecs.IMREAD_GRAYSCALE;
import static org.bytedeco.opencv.global.opencv_imgproc.*;
import static org.bytedeco.opencv.helper.opencv_imgcodecs.cvLoadImage;
import static org.bytedeco.opencv.helper.opencv_imgcodecs.cvSaveImage;
Expand Down Expand Up @@ -235,15 +235,17 @@ public static Scalar randColor() {
return new Scalar(b, g, r, 0);
}

public FindResult getAKAZEFindResult(File temFile, File beforeFile) throws IOException {
public FindResult getAKAZEFindResult(File temFile, File beforeFile, boolean isDelete) {
IplImage object = cvLoadImage(temFile.getAbsolutePath(), IMREAD_GRAYSCALE);
IplImage image = cvLoadImage(beforeFile.getAbsolutePath(), IMREAD_GRAYSCALE);
logger.info("img width:" + image.width());
logger.info("img height:" + image.height());
if (object == null || image == null) {
logger.error("read image failed!");
temFile.delete();
beforeFile.delete();
if (isDelete) {
temFile.delete();
beforeFile.delete();
}
return null;
}

Expand Down Expand Up @@ -337,8 +339,10 @@ public FindResult getAKAZEFindResult(File temFile, File beforeFile) throws IOExc
String fileName = "test-output" + File.separator + time + ".jpg";
cvSaveImage(fileName, correspondColor);
findResult.setFile(new File(fileName));
temFile.delete();
beforeFile.delete();
if (isDelete) {
temFile.delete();
beforeFile.delete();
}
return findResult;
}
}
8 changes: 5 additions & 3 deletions src/main/java/org/cloud/sonic/vision/cv/SIFTFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
public class SIFTFinder {
private Logger logger = new Logger();

public FindResult getSIFTFindResult(File temFile, File beforeFile) throws Exception {
public FindResult getSIFTFindResult(File temFile, File beforeFile, boolean isDelete) throws Exception {
Mat image01 = imread(beforeFile.getAbsolutePath());
Mat image02 = imread(temFile.getAbsolutePath());

Expand Down Expand Up @@ -102,8 +102,10 @@ public FindResult getSIFTFindResult(File temFile, File beforeFile) throws Except
String fileName = "test-output" + File.separator + time + ".jpg";
imwrite(fileName, result);
findResult.setFile(new File(fileName));
temFile.delete();
beforeFile.delete();
if (isDelete) {
temFile.delete();
beforeFile.delete();
}
return findResult;
}

Expand Down
94 changes: 47 additions & 47 deletions src/main/java/org/cloud/sonic/vision/cv/SimilarityChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,53 +33,53 @@
public class SimilarityChecker {
private Logger logger = new Logger();

public static double getSimilarMSSIMScore(File file1, File file2, Boolean isDelete) {
synchronized (SimilarityChecker.class) {
Mat i1 = imread(file1.getAbsolutePath());
Mat i2 = imread(file2.getAbsolutePath());
if (i1.size().get() != i2.size().get()) {
return 0;
}
double C1 = 6.5025, C2 = 58.5225;
int d = opencv_core.CV_32F;
Mat I1 = new Mat();
Mat I2 = new Mat();
i1.convertTo(I1, d);
i2.convertTo(I2, d);
Mat I2_2 = I2.mul(I2).asMat();
Mat I1_2 = I1.mul(I1).asMat();
Mat I1_I2 = I1.mul(I2).asMat();
Mat mu1 = new Mat();
Mat mu2 = new Mat();
GaussianBlur(I1, mu1, new Size(11, 11), 1.5);
GaussianBlur(I2, mu2, new Size(11, 11), 1.5);
Mat mu1_2 = mu1.mul(mu1).asMat();
Mat mu2_2 = mu2.mul(mu2).asMat();
Mat mu1_mu2 = mu1.mul(mu2).asMat();
Mat sigma1_2 = new Mat();
Mat sigma2_2 = new Mat();
Mat sigma12 = new Mat();
GaussianBlur(I1_2, sigma1_2, new Size(11, 11), 1.5);
sigma1_2 = subtract(sigma1_2, mu1_2).asMat();
GaussianBlur(I2_2, sigma2_2, new Size(11, 11), 1.5);
sigma2_2 = subtract(sigma2_2, mu2_2).asMat();
GaussianBlur(I1_I2, sigma12, new Size(11, 11), 1.5);
sigma12 = subtract(sigma12, mu1_mu2).asMat();
Mat t1, t2, t3;
t1 = add(multiply(2, mu1_mu2), Scalar.all(C1)).asMat();
t2 = add(multiply(2, sigma12), Scalar.all(C2)).asMat();
t3 = t1.mul(t2).asMat();
t1 = add(add(mu1_2, mu2_2), Scalar.all(C1)).asMat();
t2 = add(add(sigma1_2, sigma2_2), Scalar.all(C2)).asMat();
t1 = t1.mul(t2).asMat();
Mat ssim_map = new Mat();
divide(t3, t1, ssim_map);
Scalar mSsim = mean(ssim_map);
if (isDelete) {
file1.delete();
file2.delete();
}
return new BigDecimal((mSsim.get(0) + mSsim.get(1) + mSsim.get(2)) / 3).setScale(2, RoundingMode.DOWN).doubleValue();
public double getSimilarMSSIMScore(File file1, File file2, Boolean isDelete) {
Mat i1 = imread(file1.getAbsolutePath());
Mat i2 = imread(file2.getAbsolutePath());
if (i1.size().get() != i2.size().get()) {
return 0;
}
double C1 = 6.5025, C2 = 58.5225;
int d = opencv_core.CV_32F;
Mat I1 = new Mat();
Mat I2 = new Mat();
i1.convertTo(I1, d);
i2.convertTo(I2, d);
Mat I2_2 = I2.mul(I2).asMat();
Mat I1_2 = I1.mul(I1).asMat();
Mat I1_I2 = I1.mul(I2).asMat();
Mat mu1 = new Mat();
Mat mu2 = new Mat();
GaussianBlur(I1, mu1, new Size(11, 11), 1.5);
GaussianBlur(I2, mu2, new Size(11, 11), 1.5);
Mat mu1_2 = mu1.mul(mu1).asMat();
Mat mu2_2 = mu2.mul(mu2).asMat();
Mat mu1_mu2 = mu1.mul(mu2).asMat();
Mat sigma1_2 = new Mat();
Mat sigma2_2 = new Mat();
Mat sigma12 = new Mat();
GaussianBlur(I1_2, sigma1_2, new Size(11, 11), 1.5);
sigma1_2 = subtract(sigma1_2, mu1_2).asMat();
GaussianBlur(I2_2, sigma2_2, new Size(11, 11), 1.5);
sigma2_2 = subtract(sigma2_2, mu2_2).asMat();
GaussianBlur(I1_I2, sigma12, new Size(11, 11), 1.5);
sigma12 = subtract(sigma12, mu1_mu2).asMat();
Mat t1, t2, t3;
t1 = add(multiply(2, mu1_mu2), Scalar.all(C1)).asMat();
t2 = add(multiply(2, sigma12), Scalar.all(C2)).asMat();
t3 = t1.mul(t2).asMat();
t1 = add(add(mu1_2, mu2_2), Scalar.all(C1)).asMat();
t2 = add(add(sigma1_2, sigma2_2), Scalar.all(C2)).asMat();
t1 = t1.mul(t2).asMat();
Mat ssim_map = new Mat();
divide(t3, t1, ssim_map);
Scalar mSsim = mean(ssim_map);
if (isDelete) {
file1.delete();
file2.delete();
}
double re = new BigDecimal((mSsim.get(0) + mSsim.get(1) + mSsim.get(2)) / 3).setScale(2, RoundingMode.DOWN).doubleValue();
logger.info("similar rate: " + re);
return re;
}
}
9 changes: 6 additions & 3 deletions src/main/java/org/cloud/sonic/vision/cv/TemMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public class TemMatcher {
private Logger logger = new Logger();

public FindResult getTemMatchResult(File temFile, File beforeFile) throws IOException {
public FindResult getTemMatchResult(File temFile, File beforeFile, boolean isDelete) throws IOException {
try {
Mat sourceColor = imread(beforeFile.getAbsolutePath());
Mat sourceGrey = new Mat(sourceColor.size(), CV_8UC1);
Expand Down Expand Up @@ -62,10 +62,13 @@ public FindResult getTemMatchResult(File temFile, File beforeFile) throws IOExce
findResult.setX(max.x() + template.cols() / 2);
findResult.setY(max.y() + template.rows() / 2);
findResult.setFile(new File(fileName));
logger.info(findResult.toString());
return findResult;
} finally {
temFile.delete();
beforeFile.delete();
if (isDelete) {
temFile.delete();
beforeFile.delete();
}
}
}

Expand Down
26 changes: 25 additions & 1 deletion src/test/java/org/cloud/sonic/vision/cv/CVTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,34 @@
import java.io.File;

public class CVTest {
File tem = new File("tem.png");
File before = new File("test.png");
File s = new File("testS.png");

@Test
public void testSift() throws Exception {
SIFTFinder siftFinder = new SIFTFinder();
FindResult findResult = siftFinder.getSIFTFindResult(new File("tem.jpg"), new File("test.jpg"));
FindResult findResult = siftFinder.getSIFTFindResult(tem, before, false);
Assert.assertTrue(findResult != null);
}

@Test
public void testAkaze() {
AKAZEFinder akazeFinder = new AKAZEFinder();
FindResult findResult = akazeFinder.getAKAZEFindResult(tem, before, false);
Assert.assertTrue(findResult != null);
}

@Test
public void testTem() throws Exception {
TemMatcher temMatcher = new TemMatcher();
FindResult findResult = temMatcher.getTemMatchResult(tem, before, false);
Assert.assertTrue(findResult != null);
}

@Test
public void testSimi() {
SimilarityChecker similarityChecker = new SimilarityChecker();
Assert.assertTrue(similarityChecker.getSimilarMSSIMScore(before, s, false) > 0);
}
}
Binary file added tem.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit caff805

Please sign in to comment.