Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 1117

OpenCV compareHist and matchTemplate not working properly on Android Studio

$
0
0
I've been trying to compare two images and get an output showing the differences among the images. The code seemed to work perfectly on python environment with same logic. But It does not seem to work on Android Studio. The Value of compareHist returns 1 and the variable 'accuracy' becomes 1 during the process. In summary, any different two images are said to be matched by this code. How do I solve this? **compare.java** private void compare_image(){ Mat image_1 = Imgcodecs.imread(getResources().getDrawable(R.drawable.real10tk).toString()); Mat image_2 = Imgcodecs.imread(getResources().getDrawable(R.drawable.noteicon).toString()); double commutativeImageDiff = this.get_image_difference(image_1, image_2); TextView t = findViewById(R.id.textView10); int minImgDiff = 1; if (commutativeImageDiff < minImgDiff) { TextView text = findViewById(R.id.textView11); text.setText("M"); t.setText("" + commutativeImageDiff); } else t.setText("" + 10000); } @SuppressLint("SetTextI18n") private double get_image_difference(Mat image_1, Mat image_2) { MatOfFloat ranges = new MatOfFloat(0f, 256f); MatOfInt histsize = new MatOfInt(256); calcHist(Arrays.asList(image_1), new MatOfInt(0), new Mat(), image_1, histsize, ranges); calcHist(Arrays.asList(image_2), new MatOfInt(0), new Mat(), image_2, histsize, ranges); double img_hist_diff = compareHist(image_1, image_2, HISTCMP_BHATTACHARYYA);// img_hist_diff = 0 int result_cols = image_1.cols() - image_2.cols() + 1; int result_rows = image_1.rows() - image_2.rows() + 1; Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1); Imgproc.matchTemplate(image_1, image_2, result, TM_CCOEFF_NORMED); Core.MinMaxLocResult minMaxLocRes = Core.minMaxLoc(result); double accuracy = minMaxLocRes.maxVal; Point location = minMaxLocRes.maxLoc; double img_template_diff = 1 - accuracy; //accuracy = 1 return (img_hist_diff / 10) + img_template_diff; }

Viewing all articles
Browse latest Browse all 1117

Trending Articles