public Mat bufferedImagetoMat(BufferedImage image, String format) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, format, byteArrayOutputStream);
byteArrayOutputStream.flush();
return Imgcodecs.imdecode(new MatOfByte(byteArrayOutputStream.toByteArray()),
Imgcodecs.IMREAD_UNCHANGED);
}
public int[] find(Mat template, Mat screenMat) {
// Create the result matrix
int result_cols = screenMat.cols() - template.cols() + 1;
int result_rows = screenMat.rows() - template.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
int templateMatchingMethod = Imgproc.TM_CCOEFF_NORMED;
// int templateMatchingMethod = Imgproc.TM_CCORR_NORMED;
// int templateMatchingMethod = Imgproc.TM_SQDIFF_NORMED;
Imgproc.matchTemplate(screenMat, template, result, templateMatchingMethod);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
org.opencv.core.Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
Point matchLoc;
if( templateMatchingMethod == Imgproc.TM_SQDIFF || templateMatchingMethod == Imgproc.TM_SQDIFF_NORMED ) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
Imgproc.rectangle(screenMat, matchLoc, new Point(matchLoc.x + template.cols(),
matchLoc.y + template.rows()), new Scalar(0, 255, 0));
return new int[] { (int) matchLoc.x, (int) matchLoc.y };
}
Comments
0 B
|0 👍
/1 👎
0 B
|👍
/👎