이미지 2개를 서로 비교해서 유사도를 측정하는 앱 만드려고 갖고 잇는 자바코드를
안드로이드 스튜디오로 옮겨서 사용하려고 합니다.
일단 기기에 있는 사진2개를 가져와서 계산후에 단순히 텍스트로 유사도를 보여주는거 부터 시작하려 했는데
아무리 생각해도 문제없는 코드같은데 실행시 앱이 자꾸 꺼져버리네요..
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import android.widget.TextView;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.DMatch;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class MainActivity extends AppCompatActivity {
TextView outputBottom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
outputBottom = (TextView)findViewById(R.id.text1);
String filename1 = "data/data/com.example.timon.myapplication/files/hungry.jpg";
String filename2 = "data/data/com.example.timon.myapplication/files/hungry.jpg";
int ret;
ret = compareHistogram(filename1, filename2);
if (ret > 0) {
outputBottom.setText("Two images are same.");
} else {
outputBottom.setText("Two images are different.");
}
}
public int compareHistogram(String filename1, String filename2) {
int retVal = 0;
long startTime = System.currentTimeMillis();
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// Load images to compare
Mat img1 = Imgcodecs.imread(filename1, Imgcodecs.CV_LOAD_IMAGE_COLOR);
Mat img2 = Imgcodecs.imread(filename2, Imgcodecs.CV_LOAD_IMAGE_COLOR);
Mat hsvImg1 = new Mat();
Mat hsvImg2 = new Mat();
// Convert to HSV
Imgproc.cvtColor(img1, hsvImg1, Imgproc.COLOR_BGR2HSV);
Imgproc.cvtColor(img2, hsvImg2, Imgproc.COLOR_BGR2HSV);
// Set configuration for calchist()
List<Mat> listImg1 = new ArrayList<Mat>();
List<Mat> listImg2 = new ArrayList<Mat>();
listImg1.add(hsvImg1);
listImg2.add(hsvImg2);
MatOfFloat ranges = new MatOfFloat(0,255);
MatOfInt histSize = new MatOfInt(50);
MatOfInt channels = new MatOfInt(0);
// Histograms
Mat histImg1 = new Mat();
Mat histImg2 = new Mat();
// Calculate the histogram for the HSV imgaes
Imgproc.calcHist(listImg1, channels, new Mat(), histImg1, histSize, ranges);
Imgproc.calcHist(listImg2, channels, new Mat(), histImg2, histSize, ranges);
Core.normalize(histImg1, histImg1, 0, 1, Core.NORM_MINMAX, -1, new Mat());
Core.normalize(histImg2, histImg2, 0, 1, Core.NORM_MINMAX, -1, new Mat());
double result0, result1, result2, result3;
result0 = Imgproc.compareHist(histImg1, histImg2, 0);
result1 = Imgproc.compareHist(histImg1, histImg2, 1);
result2 = Imgproc.compareHist(histImg1, histImg2, 2);
result3 = Imgproc.compareHist(histImg1, histImg2, 3);
System.out.println("Method [0] " + result0);
System.out.println("Method [1] " + result1);
System.out.println("Method [2] " + result2);
System.out.println("Method [3] " + result3);
// If the count that it is satisfied with the condition is over 3, two images is same.
int count=0;
if (result0 > 0.9) count++;
if (result1 < 0.1) count++;
if (result2 > 1.5) count++;
if (result3 < 0.3) count++;
if (count >= 3) retVal = 1;
long estimatedTime = System.currentTimeMillis() - startTime;
System.out.println("estimatedTime=" + estimatedTime + "ms");
return retVal;
}
}
실행하면 오류나는 부분은 저렇게 나오는데 문제가 되는 코드는
05-20 12:08:11.762 28031-28031/com.example.timon.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.timon.myapplication, PID: 28031
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/base.apk", zip file "/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/split_lib_resources_apk.apk", zip file "/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.timon.myapplication-XgaOBZJEatCmxQ2Fj-xRfg==/lib/x86, /system/lib, /vendor/lib]]] couldn't find "libopencv_java341.so"
at java.lang.Runtime.loadLibrary0(Runtime.java:1011)
at java.lang.System.loadLibrary(System.java:1657)
at com.example.timon.myapplication.MainActivity.compareHistogram(MainActivity.java:53)
at com.example.timon.myapplication.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
에서 문제되는 코드부분은
ret = compareHistogram(filename1, filename2);
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
이거 2가지인데 아무리 생각해도 찾아봐도 뭐가 문제인지 모르겟네요