Java Code Examples for org.opencv.core.Core#MinMaxLocResult

The following examples show how to use org.opencv.core.Core#MinMaxLocResult . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * 模板匹配
 *
 * @param srcImg      //源图像
 * @param templateImg //模板图像
 * @param threshold   //相识度阈值,阈值调小可以一定程度解决不同手机分辨率的问题
 * @return //如果没有找到则返回(-1,-1)点
 */
public static Point matchTemplate(Bitmap srcImg, Bitmap templateImg, double threshold) {

    if (threshold <= 0) {
        threshold = 0.5;
    }


    Mat tpl = new Mat();
    Mat src = new Mat();
    Utils.bitmapToMat(srcImg, src);
    Utils.bitmapToMat(templateImg, tpl);


    int height = src.rows() - tpl.rows() + 1;
    int width = src.cols() - tpl.cols() + 1;
    Mat result = new Mat(height, width, CvType.CV_32FC1);
    int method = Imgproc.TM_CCOEFF_NORMED;
    Imgproc.matchTemplate(src, tpl, result, method);
    Core.MinMaxLocResult minMaxResult = Core.minMaxLoc(result);
    org.opencv.core.Point maxloc = minMaxResult.maxLoc;
    if (minMaxResult.maxVal < threshold) {
        return new Point(-1, -1);
    }
    org.opencv.core.Point minloc = minMaxResult.minLoc;
    org.opencv.core.Point matchloc = null;
    matchloc = maxloc;
    return new Point((int) matchloc.x, (int) matchloc.y);

}
 
Example 2
Source Project: onetwo   File: ImageMatchers.java    License: Apache License 2.0 5 votes vote down vote up
static  public void drawMatchedImage(String destImagePath, String srcImagePath) {
       //将文件读入为OpenCV的Mat格式
       Mat source = Imgcodecs.imread(srcImagePath);
       Mat destImage = Imgcodecs.imread(destImagePath);
	Core.MinMaxLocResult result = matchResult(source, destImage);
	Point matchLoc = result.minLoc;
       //在原图上的对应模板可能位置画一个绿色矩形
       Imgproc.rectangle(source, matchLoc, new Point(matchLoc.x + destImage.width(), matchLoc.y + destImage.height()), new Scalar(0, 255, 0));
       //将结果输出到对应位置
       String srcDir = new File(srcImagePath).getParent();
   	String srcImageName = FileUtils.getFileNameWithoutExt(srcImagePath);
   	String srcExt = FileUtils.getExtendName(srcImagePath);
   	String destImageName = FileUtils.getFileNameWithoutExt(destImagePath);
       Imgcodecs.imwrite(srcDir + "/" + srcImageName + "-matched-"+destImageName+"." + srcExt, source);
}
 
Example 3
Source Project: onetwo   File: ImageMatchers.java    License: Apache License 2.0 5 votes vote down vote up
static public Core.MinMaxLocResult matchResult(Mat source, Mat destImage) {
       //创建于原图相同的大小,储存匹配度
       Mat result = Mat.zeros(source.rows() - destImage.rows() + 1, source.cols() - destImage.cols() + 1, CvType.CV_32FC1);
       //调用模板匹配方法
       Imgproc.matchTemplate(source, destImage, result, Imgproc.TM_SQDIFF_NORMED);
       //规格化
       Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1);
       //获得最可能点,MinMaxLocResult是其数据格式,包括了最大、最小点的位置x、y
       Core.MinMaxLocResult mlr = Core.minMaxLoc(result);
       return mlr;
}
 
Example 4
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    Mat frame = inputFrame.rgba();
    Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2RGB);
    Size frame_size = new Size(416, 416);
    Scalar mean = new Scalar(127.5);

    Mat blob = Dnn.blobFromImage(frame, 1.0 / 255.0, frame_size, mean, true, false);
    //save_mat(blob);
    net.setInput(blob);

    List<Mat> result = new ArrayList<>();
    List<String> outBlobNames = net.getUnconnectedOutLayersNames();

    net.forward(result, outBlobNames);
    float confThreshold = 0.5f;

    for (int i = 0; i < result.size(); ++i) {
        // each row is a candidate detection, the 1st 4 numbers are
        // [center_x, center_y, width, height], followed by (N-4) class probabilities
        Mat level = result.get(i);
        for (int j = 0; j < level.rows(); ++j) {
            Mat row = level.row(j);
            Mat scores = row.colRange(5, level.cols());
            Core.MinMaxLocResult mm = Core.minMaxLoc(scores);
            float confidence = (float) mm.maxVal;
            Point classIdPoint = mm.maxLoc;
            if (confidence > confThreshold) {

                int centerX = (int) (row.get(0, 0)[0] * frame.cols());
                int centerY = (int) (row.get(0, 1)[0] * frame.rows());
                int width = (int) (row.get(0, 2)[0] * frame.cols());
                int height = (int) (row.get(0, 3)[0] * frame.rows());

                int left = (int) (centerX - width * 0.5);
                int top =(int)(centerY - height * 0.5);
                int right =(int)(centerX + width * 0.5);
                int bottom =(int)(centerY + height * 0.5);

                Point left_top = new Point(left, top);
                Point right_bottom=new Point(right, bottom);
                Point label_left_top = new Point(left, top-5);
                DecimalFormat df = new DecimalFormat("#.##");

                int class_id = (int) classIdPoint.x;
                String label= classNames.get(class_id) + ": " + df.format(confidence);
                Scalar color= colors.get(class_id);

                Imgproc.rectangle(frame, left_top,right_bottom , color, 3, 2);
                Imgproc.putText(frame, label, label_left_top, Imgproc.FONT_HERSHEY_SIMPLEX, 1, new Scalar(0, 0, 0), 4);
                Imgproc.putText(frame, label, label_left_top, Imgproc.FONT_HERSHEY_SIMPLEX, 1, new Scalar(255, 255, 255), 2);
            }
        }
    }
    return frame;
}
 
Example 5
public static void drawIrisCircle(Mat matrixRgba, Core.MinMaxLocResult minMaxLocResult) {
    Imgproc.circle(matrixRgba, minMaxLocResult.minLoc, 2, new Scalar(255, 255, 255, 255), 2);
}
 
Example 6
/**
 * <p>Build a template from a specific eye area previously substracted
 * uses detectMultiScale for this area, then uses minMaxLoc method to
 * detect iris from the detected eye</p>
 *
 * @param area Preformatted Area
 * @param size minimum iris size
 * @param grayMat image in gray
 * @param rgbaMat image in color
 * @param detectorEye Haar Cascade classifier
 * @return built template
 */
@NonNull
private static Mat buildTemplate(Rect area, final int size,
                                 @NonNull Mat grayMat,
                                 @NonNull Mat rgbaMat,
                                 CascadeClassifier detectorEye) {
    Mat template = new Mat();
    Mat graySubMatEye = grayMat.submat(area);
    MatOfRect eyes = new MatOfRect();
    Rect eyeTemplate;
    detectorEye.detectMultiScale(graySubMatEye, eyes, 1.15, 2,
            Objdetect.CASCADE_FIND_BIGGEST_OBJECT
                    | Objdetect.CASCADE_SCALE_IMAGE, new Size(EYE_MIN_SIZE, EYE_MIN_SIZE),
            new Size());

    Rect[] eyesArray = eyes.toArray();
    if (eyesArray.length > 0) {
        Rect e = eyesArray[0];
        e.x = area.x + e.x;
        e.y = area.y + e.y;
        Rect eyeRectangle = getEyeArea((int) e.tl().x,
                (int) (e.tl().y + e.height * 0.4),
                e.width,
                (int) (e.height * 0.6));
        graySubMatEye = grayMat.submat(eyeRectangle);
        Mat rgbaMatEye = rgbaMat.submat(eyeRectangle);


        Core.MinMaxLocResult minMaxLoc = Core.minMaxLoc(graySubMatEye);

        FaceDrawerOpenCV.drawIrisCircle(rgbaMatEye, minMaxLoc);
        Point iris = new Point();
        iris.x = minMaxLoc.minLoc.x + eyeRectangle.x;
        iris.y = minMaxLoc.minLoc.y + eyeRectangle.y;
        eyeTemplate = getEyeArea((int) iris.x - size / 2,
                (int) iris.y
                        - size / 2, size, size);
        FaceDrawerOpenCV.drawEyeRectangle(eyeTemplate, rgbaMat);
        template = (grayMat.submat(eyeTemplate)).clone();
    }
    return template;
}
 
Example 7
public static List<MatOfPoint> findContoursForMRZ(Mat src){
    Mat img = src.clone();
    src.release();
    double ratio = getScaleRatio(img.size());
    int width = (int) (img.size().width / ratio);
    int height = (int) (img.size().height / ratio);
    Size newSize = new Size(width, height);
    Mat resizedImg = new Mat(newSize, CvType.CV_8UC4);
    Imgproc.resize(img, resizedImg, newSize);

    Mat gray = new Mat();
    Imgproc.cvtColor(resizedImg, gray, Imgproc.COLOR_BGR2GRAY);
    Imgproc.medianBlur(gray, gray, 3);
    //Imgproc.blur(gray, gray, new Size(3, 3));

    Mat morph = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(13, 5));
    Mat dilatedImg = new Mat();
    Imgproc.morphologyEx(gray, dilatedImg, Imgproc.MORPH_BLACKHAT, morph);
    gray.release();

    Mat gradX = new Mat();
    Imgproc.Sobel(dilatedImg, gradX, CvType.CV_32F, 1, 0);
    dilatedImg.release();
    Core.convertScaleAbs(gradX, gradX, 1, 0);
    Core.MinMaxLocResult minMax = Core.minMaxLoc(gradX);
    Core.convertScaleAbs(gradX, gradX, (255/(minMax.maxVal - minMax.minVal)),
            - ((minMax.minVal * 255) / (minMax.maxVal - minMax.minVal)));
    Imgproc.morphologyEx(gradX, gradX, Imgproc.MORPH_CLOSE, morph);

    Mat thresh = new Mat();
    Imgproc.threshold(gradX, thresh, 0, 255, Imgproc.THRESH_OTSU);
    gradX.release();
    morph.release();

    morph = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(21, 21));
    Imgproc.morphologyEx(thresh, thresh, Imgproc.MORPH_CLOSE, morph);
    Imgproc.erode(thresh, thresh, new Mat(), new Point(-1, -1), 4);
    morph.release();

    int col = (int) resizedImg.size().width;
    int p = (int) (resizedImg.size().width * 0.05);
    int row = (int) resizedImg.size().height;
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < p; j++){
            thresh.put(i, j, 0);
            thresh.put(i, col-j, 0);
        }
    }

    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(thresh, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    hierarchy.release();

    Log.d(TAG, "contours found: " + contours.size());

    Collections.sort(contours, new Comparator<MatOfPoint>() {
        @Override
        public int compare(MatOfPoint o1, MatOfPoint o2) {
            return Double.valueOf(Imgproc.contourArea(o2)).compareTo(Imgproc.contourArea(o1));
        }
    });

    return contours;
}