private double match_eye(Rect area, Mat mTemplate, int type) { Point matchLoc; Mat mROI = mGray.submat(area); int result_cols = mGray.cols() - mTemplate.cols() + 1; int result_rows = mGray.rows() - mTemplate.rows() + 1; if (mTemplate.cols() == 0 || mTemplate.rows() == 0) { return 0.0; } mResult = new Mat(result_cols, result_rows, CvType.CV_32FC1); switch (type) { case TM_SQDIFF: Imgproc.matchTemplate(mROI, mTemplate, mResult, Imgproc.TM_SQDIFF); break; case TM_SQDIFF_NORMED: Imgproc.matchTemplate(mROI, mTemplate, mResult, Imgproc.TM_SQDIFF_NORMED); break; case TM_CCOEFF: Imgproc.matchTemplate(mROI, mTemplate, mResult, Imgproc.TM_CCOEFF); break; case TM_CCOEFF_NORMED: Imgproc.matchTemplate(mROI, mTemplate, mResult, Imgproc.TM_CCOEFF_NORMED); break; case TM_CCORR: Imgproc.matchTemplate(mROI, mTemplate, mResult, Imgproc.TM_CCORR); break; case TM_CCORR_NORMED: Imgproc.matchTemplate(mROI, mTemplate, mResult, Imgproc.TM_CCORR_NORMED); break; } Core.MinMaxLocResult mmres = Core.minMaxLoc(mResult); if (type == TM_SQDIFF || type == TM_SQDIFF_NORMED) { matchLoc = mmres.minLoc; } else { matchLoc = mmres.maxLoc; } Point matchLoc_tx = new Point(matchLoc.x + area.x, matchLoc.y + area.y); Point matchLoc_ty = new Point(matchLoc.x + mTemplate.cols() + area.x, matchLoc.y + mTemplate.rows() + area.y); Core.rectangle(mRgba, matchLoc_tx, matchLoc_ty, new Scalar(255, 255, 0, 255)); if (type == TM_SQDIFF || type == TM_SQDIFF_NORMED) { return mmres.maxVal; } else { return mmres.minVal; } }
/** * @param source * @param delta * @return Mat */ public static Mat Sobel(Mat source, int delta) { Mat grey = new Mat(); Imgproc.cvtColor(source, grey, Imgproc.COLOR_BGR2GRAY); Mat sobelx = new Mat(); Imgproc.Sobel(grey, sobelx, CvType.CV_32F, 1, delta); double minVal, maxVal; Core.MinMaxLocResult minMaxLocResult = Core.minMaxLoc(sobelx); minVal = minMaxLocResult.minVal; maxVal = minMaxLocResult.maxVal; Mat draw = new Mat(); sobelx.convertTo( draw, CvType.CV_8U, 255.0 / (maxVal - minVal), -minVal * 255.0 / (maxVal - minVal)); return draw; }
private Mat get_template(CascadeClassifier clasificator, Rect area, int size) { Mat template = new Mat(); Mat mROI = mGray.submat(area); MatOfRect eyes = new MatOfRect(); Point iris = new Point(); Rect eye_template = new Rect(); clasificator.detectMultiScale( mROI, eyes, 1.15, 2, Objdetect.CASCADE_FIND_BIGGEST_OBJECT | Objdetect.CASCADE_SCALE_IMAGE, new Size(30, 30), new Size()); Rect[] eyesArray = eyes.toArray(); for (int i = 0; i < eyesArray.length; ) { Rect e = eyesArray[i]; e.x = area.x + e.x; e.y = area.y + e.y; Rect eye_only_rectangle = new Rect( (int) e.tl().x, (int) (e.tl().y + e.height * 0.4), (int) e.width, (int) (e.height * 0.6)); mROI = mGray.submat(eye_only_rectangle); Mat vyrez = mRgba.submat(eye_only_rectangle); Core.MinMaxLocResult mmG = Core.minMaxLoc(mROI); Imgproc.circle(vyrez, mmG.minLoc, 2, new Scalar(255, 255, 255, 255), 2); iris.x = mmG.minLoc.x + eye_only_rectangle.x; iris.y = mmG.minLoc.y + eye_only_rectangle.y; eye_template = new Rect((int) iris.x - size / 2, (int) iris.y - size / 2, size, size); Imgproc.rectangle(mRgba, eye_template.tl(), eye_template.br(), new Scalar(255, 0, 0, 255), 2); template = (mGray.submat(eye_template)).clone(); return template; } return template; }
public void templateMatching() { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); int match_method = 5; int max_Trackbar = 5; Mat data = Highgui.imread("images/training_data/1" + "/data (" + 1 + ").jpg"); Mat temp = Highgui.imread("images/template.jpg"); Mat img = data.clone(); int result_cols = img.cols() - temp.cols() + 1; int result_rows = img.rows() - temp.rows() + 1; Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1); Imgproc.matchTemplate(img, temp, result, match_method); Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat()); double minVal; double maxVal; Point minLoc; Point maxLoc; Point matchLoc; // minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() ); Core.MinMaxLocResult res = Core.minMaxLoc(result); if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) { matchLoc = res.minLoc; } else { matchLoc = res.maxLoc; } // / Show me what you got Core.rectangle( img, matchLoc, new Point(matchLoc.x + temp.cols(), matchLoc.y + temp.rows()), new Scalar(0, 255, 0)); // Save the visualized detection. Highgui.imwrite("images/samp.jpg", img); }
private void match_eye(Rect area, Mat mTemplate, int type) { Point matchLoc; Mat mROI = mGray.submat(area); int result_cols = mROI.cols() - mTemplate.cols() + 1; int result_rows = mROI.rows() - mTemplate.rows() + 1; // Check for bad template size if (mTemplate.cols() == 0 || mTemplate.rows() == 0) { return; } Mat mResult = new Mat(result_cols, result_rows, CvType.CV_8U); long nbPixels = (mResult.rows() * mResult.cols()) - getBlackPixels(mResult); if (Math.abs(nbPixels) < 2000) { final MediaPlayer mp = new MediaPlayer(); try { mp.reset(); AssetFileDescriptor afd; afd = getAssets().openFd("wakeUp.mp3"); mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); mp.prepare(); mp.start(); Thread.sleep(60000); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } Log.i("You are sleeping", "YOU SLEPT"); } else Log.i("M_match_eye: else ", "nbPixels = " + nbPixels); switch (type) { case TM_SQDIFF: Imgproc.matchTemplate(mROI, mTemplate, mResult, Imgproc.TM_SQDIFF); break; case TM_SQDIFF_NORMED: Imgproc.matchTemplate(mROI, mTemplate, mResult, Imgproc.TM_SQDIFF_NORMED); break; case TM_CCOEFF: Imgproc.matchTemplate(mROI, mTemplate, mResult, Imgproc.TM_CCOEFF); break; case TM_CCOEFF_NORMED: Imgproc.matchTemplate(mROI, mTemplate, mResult, Imgproc.TM_CCOEFF_NORMED); break; case TM_CCORR: Imgproc.matchTemplate(mROI, mTemplate, mResult, Imgproc.TM_CCORR); break; case TM_CCORR_NORMED: Imgproc.matchTemplate(mROI, mTemplate, mResult, Imgproc.TM_CCORR_NORMED); break; } Core.MinMaxLocResult mmres = Core.minMaxLoc(mResult); // there is difference in matching methods - best match is max/min value if (type == TM_SQDIFF || type == TM_SQDIFF_NORMED) { matchLoc = mmres.minLoc; } else { matchLoc = mmres.maxLoc; } Point matchLoc_tx = new Point(matchLoc.x + area.x, matchLoc.y + area.y); Point matchLoc_ty = new Point(matchLoc.x + mTemplate.cols() + area.x, matchLoc.y + mTemplate.rows() + area.y); Imgproc.rectangle(mRgba, matchLoc_tx, matchLoc_ty, new Scalar(255, 255, 0, 255)); Rect rec = new Rect(matchLoc_tx, matchLoc_ty); }