Advertisement
ILyaCyclone

Untitled

Jun 5th, 2023
1,111
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. public Mat bufferedImagetoMat(BufferedImage image, String format) throws IOException {
  2.         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  3.         ImageIO.write(image, format, byteArrayOutputStream);
  4.         byteArrayOutputStream.flush();
  5.         return Imgcodecs.imdecode(new MatOfByte(byteArrayOutputStream.toByteArray()),
  6.                 Imgcodecs.IMREAD_UNCHANGED);
  7.     }
  8.  
  9.  
  10. public int[] find(Mat template, Mat screenMat) {
  11.         // Create the result matrix
  12.         int result_cols = screenMat.cols() - template.cols() + 1;
  13.         int result_rows = screenMat.rows() - template.rows() + 1;
  14.         Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
  15.  
  16.         int templateMatchingMethod = Imgproc.TM_CCOEFF_NORMED;
  17.         // int templateMatchingMethod = Imgproc.TM_CCORR_NORMED;
  18.         // int templateMatchingMethod = Imgproc.TM_SQDIFF_NORMED;
  19.  
  20.         Imgproc.matchTemplate(screenMat, template, result, templateMatchingMethod);
  21.         Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
  22.         org.opencv.core.Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
  23.  
  24.         Point matchLoc;
  25.         if( templateMatchingMethod == Imgproc.TM_SQDIFF || templateMatchingMethod == Imgproc.TM_SQDIFF_NORMED ) {
  26.             matchLoc = mmr.minLoc;
  27.         } else {
  28.             matchLoc = mmr.maxLoc;
  29.         }
  30.  
  31.         Imgproc.rectangle(screenMat, matchLoc, new Point(matchLoc.x + template.cols(),
  32.                 matchLoc.y + template.rows()), new Scalar(0, 255, 0));
  33.        
  34.         return new int[] { (int) matchLoc.x, (int) matchLoc.y };
  35.     }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement