import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
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.MatOfKeyPoint;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.RotatedRect;
import org.opencv.core.Scalar;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.imgproc.Imgproc;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class UJI extends AppCompatActivity {
int jumMM=0;
int jumJM;
String gab="";
int jd=20;
Button btnCek;
String[]arID;
String kategori,mypath,huruf;
private static final String TAG = "OCVSample::Activity";
private static final int REQUEST_PERMISSION = 100;
TextView tvName;
Scalar RED = new Scalar(255, 0, 0);
Scalar GREEN = new Scalar(0, 255, 0);
FeatureDetector detector;
DescriptorExtractor descriptor;
DescriptorMatcher matcher;
Mat descriptors2;
MatOfKeyPoint keypoints2;
MatOfKeyPoint [] key1;
Mat desc1[],citra1[];
static {
if (!OpenCVLoader.initDebug())
Log.d("ERROR", "Unable to load OpenCV");
else
Log.d("SUCCESS", "OpenCV loaded");
}
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS: {
Log.i(TAG, "OpenCV loaded successfully");
try {
// initializeOpenCVDependencies();
} catch (Exception e) {
e.printStackTrace();
}
}
break;
default: {
super.onManagerConnected(status);
}
break;
}
}
};
private void initializeOpenCVDependencies() throws IOException {
// mOpenCvCameraView.enableView();
detector = FeatureDetector.create(FeatureDetector.ORB);
descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);
matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
mypath=arID[0];
AssetManager assetManager = getAssets();
Mat aInputFrame=new Mat();
//Bitmap bitmap1 = BitmapFactory.decodeFile(path);
InputStream istr2 = null;//a.jpeg
try {
istr2 = assetManager.open(mypath);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap2 = BitmapFactory.decodeStream(istr2);
Utils.bitmapToMat(bitmap2, aInputFrame );//aInputFrame=citra1[k]
Imgproc.cvtColor(aInputFrame , aInputFrame , Imgproc.COLOR_RGB2GRAY);
aInputFrame.convertTo(aInputFrame , 0);
descriptors2= new Mat();//descriptors2=desc1[k]
keypoints2 = new MatOfKeyPoint();//keypoints2= key1[k]
detector.detect(aInputFrame ,keypoints2);
descriptor.compute(aInputFrame , keypoints2, descriptors2);
for(int k=0;k<jd;k++) {
citra1[k] = new Mat();
InputStream istr1 = assetManager.open(arID[k]);//a.jpeg
Bitmap bitmap1 = BitmapFactory.decodeStream(istr1);
Utils.bitmapToMat(bitmap1, citra1[k] );
Imgproc.cvtColor(citra1[k] , citra1[k] , Imgproc.COLOR_RGB2GRAY);
citra1[k] .convertTo(citra1[k] , 0);
desc1[k] = new Mat();
key1[k] = new MatOfKeyPoint();
detector.detect(citra1[k] , key1[k]);
descriptor.compute(citra1[k] , key1[k], desc1[k]);
compareBitmaps(bitmap1, bitmap2);
//compareMats(descriptors2, desc1[k]);
}
//============================================1
// Log.d("path",kategori+"#"+huruf+"#"+mypath);
// cekCompare(mypath);
}
public UJI() {
Log.i(TAG, "Instantiated new " + this.getClass());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "called onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// setContentView(R.layout.activity_deteksi);
// Intent io=this.getIntent();
// kategori=io.getStringExtra("kategori");
// mypath=io.getStringExtra("mypath");
// huruf=io.getStringExtra("huruf");
//btnCek.setText(mypath);
huruf="A";
if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISSION);
}
jd=3;
arID=new String[jd];
desc1=new Mat[jd];
citra1=new Mat[jd];
key1=new MatOfKeyPoint[jd];
if(huruf.equalsIgnoreCase("A")) {
arID[0] = "a1.png";
arID[1] = "a2.png";
arID[2] = "a3.png";
}
else if(huruf.equalsIgnoreCase("I")) {
arID[0] = "i1.png";
arID[1] = "i2.png";
arID[2] = "i3.png";
}
else if(huruf.equalsIgnoreCase("U")) {
arID[0] = "u1.png";
arID[1] = "u2.png";
arID[2] = "u3.png";
}
else if(huruf.equalsIgnoreCase("E")) {
arID[0] = "e1.png";
arID[1] = "e2.png";
arID[2] = "e3.png";
}
else if(huruf.equalsIgnoreCase("KA")) {
arID[0] = "ka1.png";
arID[1] = "ka2.png";
arID[2] = "ka3.png";
}
else if(huruf.equalsIgnoreCase("KI")) {
arID[0] = "ki1.png";
arID[1] = "ki2.png";
arID[2] = "ki3.png";
}
else if(huruf.equalsIgnoreCase("KU")) {
arID[0] = "ku1.png";
arID[1] = "ku2.png";
arID[2] = "ku3.png";
}
else if(huruf.equalsIgnoreCase("KE")) {
arID[0] = "ke1.png";
arID[1] = "ke2.png";
arID[2] = "ke3.png";
}
else if(huruf.equalsIgnoreCase("KO")) {
arID[0] = "ko1.png";
arID[1] = "ko2.png";
arID[2] = "ko3.png";
}
else if(huruf.equalsIgnoreCase("SA")) {
arID[0] = "sa1.png";
arID[1] = "sa2.png";
arID[2] = "sa3.png";
}
else if(huruf.equalsIgnoreCase("SHI")) {
arID[0] = "shi1.png";
arID[1] = "shi2.png";
arID[2] = "shi3.png";
}
else if(huruf.equalsIgnoreCase("SU")) {
arID[0] = "su1.png";
arID[1] = "su2.png";
arID[2] = "su3.png";
}
else if(huruf.equalsIgnoreCase("SE")) {
arID[0] = "se1.png";
arID[1] = "se2.png";
arID[2] = "se3.png";
}
else if(huruf.equalsIgnoreCase("SO")) {
arID[0] = "so1.png";
arID[1] = "so2.png";
arID[2] = "so3.png";
}
else if(huruf.equalsIgnoreCase("TA")) {
arID[0] = "ta1.png";
arID[1] = "ta2.png";
arID[2] = "ta3.png";
}
else if(huruf.equalsIgnoreCase("CHI")) {
arID[0] = "chi1.png";
arID[1] = "chi2.png";
arID[2] = "chi3.png";
}
else if(huruf.equalsIgnoreCase("TSU")) {
arID[0] = "tsu1.png";
arID[1] = "tsu2.png";
arID[2] = "tsu3.png";
}
else if(huruf.equalsIgnoreCase("TE")) {
arID[0] = "te1.png";
arID[1] = "te2.png";
arID[2] = "te3.png";
}
else if(huruf.equalsIgnoreCase("TO")) {
arID[0] = "to1.png";
arID[1] = "to2.png";
arID[2] = "to3.png";
}
else if(huruf.equalsIgnoreCase("NA")) {
arID[0] = "na1.png";
arID[1] = "na2.png";
arID[2] = "na3.png";
}
else if(huruf.equalsIgnoreCase("NI")) {
arID[0] = "ni1.png";
arID[1] = "ni2.png";
arID[2] = "ni3.png";
}
else if(huruf.equalsIgnoreCase("NU")) {
arID[0] = "nu1.png";
arID[1] = "nu2.png";
arID[2] = "nu3.png";
}
else if(huruf.equalsIgnoreCase("NE")) {
arID[0] = "ne1.png";
arID[1] = "ne2.png";
arID[2] = "ne3.png";
}
else if(huruf.equalsIgnoreCase("NO")) {
arID[0] = "no1.png";
arID[1] = "no2.png";
arID[2] = "no3.png";
}
else if(huruf.equalsIgnoreCase("HA")) {
arID[0] = "ha1.png";
arID[1] = "ha2.png";
arID[2] = "ha3.png";
}
else if(huruf.equalsIgnoreCase("HI")) {
arID[0] = "hi1.png";
arID[1] = "hi2.png";
arID[2] = "hi3.png";
}
else if(huruf.equalsIgnoreCase("FU")) {
arID[0] = "fu1.png";
arID[1] = "fu2.png";
arID[2] = "fu3.png";
}
else if(huruf.equalsIgnoreCase("HE")) {
arID[0] = "he1.png";
arID[1] = "he2.png";
arID[2] = "he3.png";
}
else if(huruf.equalsIgnoreCase("HO")) {
arID[0] = "ho1.png";
arID[1] = "ho2.png";
arID[2] = "ho3.png";
}
else if(huruf.equalsIgnoreCase("MA")) {
arID[0] = "ma1.png";
arID[1] = "ma2.png";
arID[2] = "ma3.png";
}
else if(huruf.equalsIgnoreCase("MI")) {
arID[0] = "mi1.png";
arID[1] = "mi2.png";
arID[2] = "mi3.png";
}
else if(huruf.equalsIgnoreCase("MU")) {
arID[0] = "mu1.png";
arID[1] = "mu2.png";
arID[2] = "mu3.png";
}
else if(huruf.equalsIgnoreCase("ME")) {
arID[0] = "me1.png";
arID[1] = "me2.png";
arID[2] = "me3.png";
}
else if(huruf.equalsIgnoreCase("MO")) {
arID[0] = "mo1.png";
arID[1] = "mo2.png";
arID[2] = "mo3.png";
}
else if(huruf.equalsIgnoreCase("YA")) {
arID[0] = "ya1.png";
arID[1] = "ya2.png";
arID[2] = "ya3.png";
}
else if(huruf.equalsIgnoreCase("YU")) {
arID[0] = "yu1.png";
arID[1] = "yu2.png";
arID[2] = "yu3.png";
}
else if(huruf.equalsIgnoreCase("YO")) {
arID[0] = "yo1.png";
arID[1] = "yo2.png";
arID[2] = "yo3.png";
}
else if(huruf.equalsIgnoreCase("RA")) {
arID[0] = "ra1.png";
arID[1] = "ra2.png";
arID[2] = "ra3.png";
}
else if(huruf.equalsIgnoreCase("RI")) {
arID[0] = "ri1.png";
arID[1] = "ri2.png";
arID[2] = "ri3.png";
}
else if(huruf.equalsIgnoreCase("RU")) {
arID[0] = "ru1.png";
arID[1] = "ru2.png";
arID[2] = "ru3.png";
}
else if(huruf.equalsIgnoreCase("RO")) {
arID[0] = "re1.png";
arID[1] = "re2.png";
arID[2] = "re3.png";
}
else if(huruf.equalsIgnoreCase("RO")) {
arID[0] = "ro1.png";
arID[1] = "ro2.png";
arID[2] = "ro3.png";
}
else if(huruf.equalsIgnoreCase("WA")) {
arID[0] = "wa1.png";
arID[1] = "wa2.png";
arID[2] = "wa3.png";
}
else if(huruf.equalsIgnoreCase("WO")) {
arID[0] = "wo1.png";
arID[1] = "wo2.png";
arID[2] = "wo3.png";
}
else if(huruf.equalsIgnoreCase("N")) {
arID[0] = "n1.png";
arID[1] = "n2.png";
arID[2] = "n3.png";
}
try {
initializeOpenCVDependencies();
} catch (IOException e) {
e.printStackTrace();
}
}
public void cekCompare(String path) {//aInputFrame=objek dari Kamera
Mat aInputFrame=new Mat();
AssetManager assetManager = getAssets();
//Bitmap bitmap1 = BitmapFactory.decodeFile(path);
InputStream istr1 = null;//a.jpeg
try {
istr1 = assetManager.open(path);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap1 = BitmapFactory.decodeStream(istr1);
Utils.bitmapToMat(bitmap1, aInputFrame );//aInputFrame=citra1[k]
Imgproc.cvtColor(aInputFrame , aInputFrame , Imgproc.COLOR_RGB2GRAY);
//cekCompare2(aInputFrame);
// String sgab="";
// aInputFrame.convertTo(aInputFrame , 0);
// descriptors2= new Mat();//descriptors2=desc1[k]
// keypoints2 = new MatOfKeyPoint();//keypoints2= key1[k]
// detector.detect(aInputFrame ,keypoints2);
// descriptor.compute(aInputFrame , keypoints2, descriptors2);
//
//
//
//
// compareBitmaps(bmTemplate, bmLogo);
}
//======================================
public void cekCompare2(Mat aInputFrame) {
String sgab="";
aInputFrame.convertTo(aInputFrame , 0);
descriptors2= new Mat();//descriptors2=desc1[k]
keypoints2 = new MatOfKeyPoint();//keypoints2= key1[k]
detector.detect(aInputFrame ,keypoints2);
descriptor.compute(aInputFrame , keypoints2, descriptors2);
double nmin = 10000000;
int indexKe=0;
MatOfDMatch matches = null;
int JM = 0;
Double max_dist = 0.0;
Double min_dist = 100.0;
matches = new MatOfDMatch();
for(int k=0;k<jd;k++){
matcher.match(desc1[k],descriptors2,matches);
MatOfDMatch filtered = filterMatchesByDistance(matches);
int total = (int) matches.size().height;
int Match= (int) filtered.size().height;
Log.d("HITUNG_MM",k+"."+arID[k]+"=Total Bit:" + total + " Match Bit:"+Match);
if(JM<Match){JM=Match;indexKe=k;}
// try {
// matcher.match(desc1[k], descriptors2, matches);
// List<DMatch> matchesList1 = matches.toList();
//
// JM = matchesList1.size();
// for (int i = 0; i < JM; i++) {
// Double dist = (double) matchesList1.get(i).distance;
// if (dist < min_dist)
// min_dist = dist;
// if (dist > max_dist)
// max_dist = dist;
// }
//
// if (min_dist < nmin) {
// indexKe = k;
// nmin = min_dist;
// }
// Log.d("MAXMIN", k+"." + max_dist + "@" + min_dist + "@" + (1.2 * min_dist)+"#"+indexKe);
// } catch (Exception ee) {
// }
//
//
// matches = new MatOfDMatch();
// try {
// matcher.match(desc1[k], descriptors2, matches);
// } catch (Exception ee) {
// }
//
//
// List<DMatch> matchesListOut = matches.toList();
// JM = matchesListOut.size();
// List<DMatch> listBest=null;
// listBest=matchesListOut;
//
// //=================================================================================================LAST
// int mm=0;
// LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
// for (int i = 0; i < JM; i++) {
// if (listBest.get(i).distance <= (1.2 * min_dist)) {//1.5 * min_dist
// good_matches.addLast(listBest.get(i));
// mm++;
// }
// }
//
// if(jumMM<mm){
// jumMM=mm;
// }
//
// if(jumJM<JM){
// jumJM=JM;
// }
//
// Log.d("HITUNG_MM",k+"."+mm+"#"+jumMM+"#JM="+JM+"#"+jumJM);
//
// sgab+="GB"+(k+1)+"."+mm+"#"+jumMM+"#JM="+JM+"#"+jumJM+"\n";
}//loop
hasil(JM+"#"+indexKe);
}
public void hasil(String hs){
new AlertDialog.Builder(this)
.setTitle("Kategori:"+kategori+":"+huruf)
.setMessage(hs)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
finish();
}})
.show();
}
// static MatOfDMatch filterMatchesByDistance(MatOfDMatch matches){
// List<DMatch> matches_original = matches.toList();
// List<DMatch> matches_filtered = new ArrayList<DMatch>();
//
// int DIST_LIMIT = 30;
// // Check all the matches distance and if it passes add to list of filtered matches
// Log.d("DISTFILTER", "ORG SIZE:" + matches_original.size() + "");
// for (int i = 0; i < matches_original.size(); i++) {
// DMatch d = matches_original.get(i);
// if (Math.abs(d.distance) <= DIST_LIMIT) {
// matches_filtered.add(d);
// }
// }
// Log.d("DISTFILTER", "FIL SIZE:" + matches_filtered.size() + "");
//
// MatOfDMatch mat = new MatOfDMatch();
// mat.fromList(matches_filtered);
// return mat;
// }
//=======================
private Bitmap findLogo(Bitmap sourceBitmap) {
Bitmap roiBitmap = null;
Mat sourceMat = new Mat(sourceBitmap.getWidth(), sourceBitmap.getHeight(), CvType.CV_8UC3);
Utils.bitmapToMat(sourceBitmap, sourceMat);
Mat roiTmp = sourceMat.clone();
final Mat hsvMat = new Mat();
sourceMat.copyTo(hsvMat);
// convert mat to HSV format for Core.inRange()
Imgproc.cvtColor(hsvMat, hsvMat, Imgproc.COLOR_RGB2HSV);
Scalar lowerb = new Scalar(85, 50, 40); // lower color border for BLUE
Scalar upperb = new Scalar(135, 255, 255); // upper color border for BLUE
Core.inRange(hsvMat, lowerb, upperb, roiTmp); // select only blue pixels
// find contours
List<MatOfPoint> contours = new ArrayList<>();
List<Rect> squares = new ArrayList<>();
Imgproc.findContours(roiTmp, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
// find appropriate bounding rectangles
for (MatOfPoint contour : contours) {
MatOfPoint2f areaPoints = new MatOfPoint2f(contour.toArray());
RotatedRect boundingRect = Imgproc.minAreaRect(areaPoints);
double rectangleArea = boundingRect.size.area();
// test min ROI area in pixels
if (rectangleArea > 400) {
Point rotated_rect_points[] = new Point[4];
boundingRect.points(rotated_rect_points);
Rect rect = Imgproc.boundingRect(new MatOfPoint(rotated_rect_points));
double aspectRatio = rect.width > rect.height ?
(double) rect.height / (double) rect.width : (double) rect.width / (double) rect.height;
if (aspectRatio >= 0.9) {
squares.add(rect);
}
}
}
Mat logoMat = extractSquareMat(roiTmp, getBiggestSquare(squares));
roiBitmap = Bitmap.createBitmap(logoMat.cols(), logoMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(logoMat, roiBitmap);
return roiBitmap;
}
public static Mat extractSquareMat(Mat sourceMat, Rect rect) {
Mat squareMat = null;
int padding = 50;
if (rect != null) {
Rect truncatedRect = new Rect((int) rect.tl().x + padding, (int) rect.tl().y + padding,
rect.width - 2 * padding, rect.height - 2 * padding);
squareMat = new Mat(sourceMat, truncatedRect);
}
return squareMat ;
}
private void compareBitmaps(Bitmap bitmap1, Bitmap bitmap2) {
Mat mat1 = new Mat(bitmap1.getWidth(), bitmap1.getHeight(), CvType.CV_8UC3);
Utils.bitmapToMat(bitmap1, mat1);
Mat mat2 = new Mat(bitmap2.getWidth(), bitmap2.getHeight(), CvType.CV_8UC3);
Utils.bitmapToMat(bitmap2, mat2);
Log.d("OKDEH","90sukses");
compareMats(mat1, mat2);
}
private void compareMats(Mat img1, Mat img2) {
FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.BRIEF);
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
Mat descriptors1 = new Mat();
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
detector.detect(img1, keypoints1);
extractor.compute(img1, keypoints1, descriptors1);
//second image
// Mat img2 = Imgcodecs.imread(path2);
Mat descriptors2 = new Mat();
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
detector.detect(img2, keypoints2);
extractor.compute(img2, keypoints2, descriptors2);
Log.d("OKDEH","91sukses");
//matcher image descriptors
MatOfDMatch matches = new MatOfDMatch();
matcher.match(descriptors1,descriptors2,matches);
// Filter matches by distance
MatOfDMatch filtered = filterMatchesByDistance(matches);
int total = (int) matches.size().height;
int Match= (int) filtered.size().height;
Log.d("LOGOKDEH", "total:" + total + " Match:" + Match);
}
static MatOfDMatch filterMatchesByDistance(MatOfDMatch matches){
List<DMatch> matches_original = matches.toList();
List<DMatch> matches_filtered = new ArrayList<DMatch>();
int DIST_LIMIT = 30;
// Check all the matches distance and if it passes add to list of filtered matches
Log.d("DISTFILTER", "ORG SIZE:" + matches_original.size() + "");
for (int i = 0; i < matches_original.size(); i++) {
DMatch d = matches_original.get(i);
if (Math.abs(d.distance) <= DIST_LIMIT) {
matches_filtered.add(d);
}
}
Log.d("DISTFILTER", "FIL SIZE:" + matches_filtered.size() + "");
MatOfDMatch mat = new MatOfDMatch();
mat.fromList(matches_filtered);
return mat;
}
public static Rect getBiggestSquare(List<Rect> squares) {
Rect biggestSquare = null;
if (squares != null && squares.size() >= 1) {
Rect square;
double maxArea;
int ixMaxArea = 0;
square = squares.get(ixMaxArea);
maxArea = square.area();
for (int ix = 1; ix < squares.size(); ix++) {
square = squares.get(ix);
if (square.area() > maxArea) {
maxArea = square.area();
ixMaxArea = ix;
}
}
biggestSquare = squares.get(ixMaxArea);
}
return biggestSquare;
}
}
Tidak ada komentar:
Posting Komentar