org.opencv.core.Core.MinMaxLocResult Java Examples

The following examples show how to use org.opencv.core.Core.MinMaxLocResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TemplateMatching.java    From opencv-object-detection with MIT License 6 votes vote down vote up
public static void main(String[] args) {
	
	System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
	Mat source=null;
	Mat template=null;
	String filePath="Sample Image\\";
	source=Imgcodecs.imread(filePath+"kapadokya.jpg");
	template=Imgcodecs.imread(filePath+"balon.jpg");

	Mat outputImage=new Mat();	
	int machMethod=Imgproc.TM_CCOEFF;
  
       Imgproc.matchTemplate(source, template, outputImage, machMethod);

   
       MinMaxLocResult mmr = Core.minMaxLoc(outputImage);
       Point matchLoc=mmr.maxLoc;

       Imgproc.rectangle(source, matchLoc, new Point(matchLoc.x + template.cols(),
               matchLoc.y + template.rows()), new Scalar(255, 255, 255));

       Imgcodecs.imwrite(filePath+"sonuc.jpg", source);
       System.out.println("��lem tamamland�.");
}
 
Example #2
Source File: ImageFinder.java    From opentest with MIT License 5 votes vote down vote up
/**
 * Checks whether an OpenCV MinMaxLocResult object is valid. This object is
 * used for storing the location of the minimum and maximum values for an
 * image find operation, along with the actual values themselves.
 */
private boolean minMaxLocResultIsValid(MinMaxLocResult minMaxLocRes) {
    if (minMaxLocRes.minVal == 1
            && minMaxLocRes.maxVal == 1
            && minMaxLocRes.maxLoc.x == 0
            && minMaxLocRes.maxLoc.y == 0
            && minMaxLocRes.minLoc.x == 0
            && minMaxLocRes.minLoc.y == 0) {

        return false;
    } else {
        return true;
    }
}
 
Example #3
Source File: ImageFinder.java    From opentest with MIT License 4 votes vote down vote up
/**
 * Finds a template image in a source image. Throws an exception when the
 * image wasn't found or the desired accuracy couldn't be met.
 *
 * @param sourceMat The source image.
 * @param templateMat The template image to find in the source image.
 * @param desiredAccuracy The desired accuracy of the find operation as a
 * number between 0 and 1.
 * @return An ImageFinderResult object that stores the rectangle of the
 * found image and desired accuracy.
 */
private ImageFinderResult findImage(Mat sourceMat, Mat templateMat, double desiredAccuracy) {
    if (sourceMat.width() < templateMat.width() || sourceMat.height() < templateMat.height()) {
        throw new UnsupportedOperationException("The template image is larger than the source image. Ensure that the width and/or height of the image you are trying to find do not exceed the dimensions of the source image.");
    }

    Mat result = new Mat(sourceMat.rows() - templateMat.rows() + 1, sourceMat.rows() - templateMat.rows() + 1, CvType.CV_32FC1);
    int intMatchingMethod;

    switch (this.matchingMethod) {
        case MM_CORELLATION_COEFF:
            intMatchingMethod = Imgproc.TM_CCOEFF_NORMED;
            break;
        case MM_CROSS_CORELLATION:
            intMatchingMethod = Imgproc.TM_CCORR_NORMED;
            break;
        default:
            intMatchingMethod = Imgproc.TM_SQDIFF_NORMED;
    }

    Imgproc.matchTemplate(sourceMat, templateMat, result, intMatchingMethod);
    MinMaxLocResult minMaxLocRes = Core.minMaxLoc(result);

    double accuracy = 0;
    Point location = null;

    if (this.matchingMethod == MatchingMethod.MM_SQUARE_DIFFERENCE) {
        accuracy = 1 - minMaxLocRes.minVal;
        location = minMaxLocRes.minLoc;
    } else {
        accuracy = minMaxLocRes.maxVal;
        location = minMaxLocRes.maxLoc;
    }

    if (accuracy < desiredAccuracy) {
        throw new ImageNotFoundException(
                String.format(
                        "Failed to find template image in the source image. The accuracy was %.2f and the desired accuracy was %.2f",
                        accuracy,
                        desiredAccuracy),
                new Rectangle((int) location.x, (int) location.y, templateMat.width(), templateMat.height()),
                accuracy);
    }

    if (!minMaxLocResultIsValid(minMaxLocRes)) {
        throw new ImageNotFoundException(
                "Image find result (MinMaxLocResult) was invalid. This usually happens when the source image is covered in one solid color.",
                null,
                null);
    }

    Rectangle foundRect = new Rectangle(
            (int) location.x,
            (int) location.y,
            templateMat.width(),
            templateMat.height());

    return new ImageFinderResult(foundRect, accuracy);
}