I'm trying to use template matching with a mask but I'm getting significantly lower scores than I expect.
class Main {
public static void main(String args[]){
String inputFile = "D:\\Projects\\Smileywork\\p.bmp";
String templateFile = "D:\\Projects\\Smileywork\\p2.bmp";
String externalMaskFile = "D:\\Projects\\Smileywork\\p_mask.bmp";
System.loadLibrary("opencv_java310");
int match_method = Imgproc.TM_CCORR_NORMED;
Mat img = Imgcodecs.imread(inputFile, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
Mat templ = Imgcodecs.imread(templateFile, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
Mat mask = Imgcodecs.imread(externalMaskFile, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_8U);
MinMaxLocResult mmr = match(match_method, img, templ, result, mask);
System.out.println("Score:" + mmr.maxVal);
}
private static MinMaxLocResult match(int match_method, Mat img, Mat templ, Mat result) {
Imgproc.matchTemplate(img, templ, result, match_method);
MinMaxLocResult mmr = Core.minMaxLoc(result);
return mmr;
}
private static MinMaxLocResult match(int match_method, Mat img, Mat templ, Mat result, Mat mask) {
Imgproc.matchTemplate(img, templ, result, match_method, mask);
MinMaxLocResult mmr = Core.minMaxLoc(result);
return mmr;
}
}
Here are p, p2 and p_mask:

All of them have the same size, they are 24-bit bmp's, but contain only black and white pixels
I get a score of 0.059. Without a mask I get 0.853. I'd understand some lower score since some white areas are not here to match, but I feel I should've got somewhat closer value.
All of them have the same size, they are 24-bit bmp's, but contain only black and white pixels
I get a score of 0.059. Without a mask I get 0.853. I'd understand some lower score since some white areas are not here to match, but I feel I should've got somewhat closer value.