Java Code Examples for org.opencv.core.Core#minMaxLoc()
The following examples show how to use
org.opencv.core.Core#minMaxLoc() .
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: ALTMRetinex.java From OptimizedImageEnhance with MIT License | 6 votes |
private static List<Mat> globalAdaptation(Mat b, Mat g, Mat r, int rows, int cols) { // Calculate Lw & maximum of Lw Mat Lw = new Mat(rows, cols, r.type()); Core.multiply(r, new Scalar(rParam), r); Core.multiply(g, new Scalar(gParam), g); Core.multiply(b, new Scalar(bParam), b); Core.add(r, g, Lw); Core.add(Lw, b, Lw); double LwMax = Core.minMaxLoc(Lw).maxVal; // the maximum luminance value // Calculate log-average luminance and get global adaptation result Mat Lw_ = Lw.clone(); Core.add(Lw_, new Scalar(0.001), Lw_); Core.log(Lw_, Lw_); double LwAver = Math.exp(Core.sumElems(Lw_).val[0] / (rows * cols)); Mat Lg = Lw.clone(); Core.divide(Lg, new Scalar(LwAver), Lg); Core.add(Lg, new Scalar(1.0), Lg); Core.log(Lg, Lg); Core.divide(Lg, new Scalar(Math.log(LwMax / LwAver + 1.0)), Lg); // Lg is the global adaptation List<Mat> list = new ArrayList<>(); list.add(Lw); list.add(Lg); return list; }
Example 2
Source File: TemplateMatching.java From opencv-object-detection with MIT License | 6 votes |
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 3
Source File: Image.java From RobotHelper with GNU Affero General Public License v3.0 | 5 votes |
/** * 模板匹配 * * @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 4
Source File: Match.java From SikuliX1 with MIT License | 5 votes |
@Override public boolean hasNext() { resultMinMax = Core.minMaxLoc(result); currentScore = resultMinMax.maxVal; currentX = (int) resultMinMax.maxLoc.x; currentY = (int) resultMinMax.maxLoc.y; if (lastScore < 0) { lastScore = currentScore; targetScore = image.similarity(); targetW = image.w; targetH = image.h; marginX = (int) (targetW * 0.8); marginY = (int) (targetH * 0.8); matchCount = 0; } boolean isMatch = false; if (currentScore > targetScore) { if (matchCount == 0) { isMatch = true; } else if (matchCount == 1) { scoreMeanDiff = lastScore - currentScore; isMatch = true; } else { double scoreDiff = lastScore - currentScore; if (scoreDiff <= (scoreMeanDiff + 0.01)) { // 0.005 scoreMeanDiff = ((scoreMeanDiff * matchCount) + scoreDiff) / (matchCount + 1); isMatch = true; } } } return isMatch; }
Example 5
Source File: ALTMRetinex.java From OptimizedImageEnhance with MIT License | 5 votes |
@SuppressWarnings("unused") private static Mat adjustment(Mat alpha, double a) { double b = Core.minMaxLoc(alpha).maxVal; int rows = alpha.rows(); int cols = alpha.cols(); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { //double val = alpha.get(i, j)[0]; alpha.put(i, j, (2 * Math.atan(a * alpha.get(i, j)[0] / b) / Math.PI * b)); } } return alpha; }
Example 6
Source File: CVProcessor.java From CVScanner with GNU General Public License v3.0 | 5 votes |
public static Rect detectBorder(Mat original){ Mat src = original.clone(); Log.d(TAG, "1 original: " + src.toString()); Imgproc.GaussianBlur(src, src, new Size(3, 3), 0); Log.d(TAG, "2.1 --> Gaussian blur done\n blur: " + src.toString()); Imgproc.cvtColor(src, src, Imgproc.COLOR_RGBA2GRAY); Log.d(TAG, "2.2 --> Grayscaling done\n gray: " + src.toString()); Mat sobelX = new Mat(); Mat sobelY = new Mat(); Imgproc.Sobel(src, sobelX, CvType.CV_32FC1, 2, 0, 5, 1, 0); Log.d(TAG, "3.1 --> Sobel done.\n X: " + sobelX.toString()); Imgproc.Sobel(src, sobelY, CvType.CV_32FC1, 0, 2, 5, 1, 0); Log.d(TAG, "3.2 --> Sobel done.\n Y: " + sobelY.toString()); Mat sum_img = new Mat(); Core.addWeighted(sobelX, 0.5, sobelY, 0.5, 0.5, sum_img); //Core.add(sobelX, sobelY, sum_img); Log.d(TAG, "4 --> Addition done. sum: " + sum_img.toString()); sobelX.release(); sobelY.release(); Mat gray = new Mat(); Core.normalize(sum_img, gray, 0, 255, Core.NORM_MINMAX, CvType.CV_8UC1); Log.d(TAG, "5 --> Normalization done. gray: " + gray.toString()); sum_img.release(); Mat row_proj = new Mat(); Mat col_proj = new Mat(); Core.reduce(gray, row_proj, 1, Core.REDUCE_AVG, CvType.CV_8UC1); Log.d(TAG, "6.1 --> Reduce done. row: " + row_proj.toString()); Core.reduce(gray, col_proj, 0, Core.REDUCE_AVG, CvType.CV_8UC1); Log.d(TAG, "6.2 --> Reduce done. col: " + col_proj.toString()); gray.release(); Imgproc.Sobel(row_proj, row_proj, CvType.CV_8UC1, 0, 2); Log.d(TAG, "7.1 --> Sobel done. row: " + row_proj.toString()); Imgproc.Sobel(col_proj, col_proj, CvType.CV_8UC1, 2, 0); Log.d(TAG, "7.2 --> Sobel done. col: " + col_proj.toString()); Rect result = new Rect(); int half_pos = (int) (row_proj.total()/2); Mat row_sub = new Mat(row_proj, new Range(0, half_pos), new Range(0, 1)); Log.d(TAG, "8.1 --> Copy sub matrix done. row: " + row_sub.toString()); result.y = (int) Core.minMaxLoc(row_sub).maxLoc.y; Log.d(TAG, "8.2 --> Minmax done. Y: " + result.y); row_sub.release(); Mat row_sub2 = new Mat(row_proj, new Range(half_pos, (int) row_proj.total()), new Range(0, 1)); Log.d(TAG, "8.3 --> Copy sub matrix done. row: " + row_sub2.toString()); result.height = (int) (Core.minMaxLoc(row_sub2).maxLoc.y + half_pos - result.y); Log.d(TAG, "8.4 --> Minmax done. Height: " + result.height); row_sub2.release(); half_pos = (int) (col_proj.total()/2); Mat col_sub = new Mat(col_proj, new Range(0, 1), new Range(0, half_pos)); Log.d(TAG, "9.1 --> Copy sub matrix done. col: " + col_sub.toString()); result.x = (int) Core.minMaxLoc(col_sub).maxLoc.x; Log.d(TAG, "9.2 --> Minmax done. X: " + result.x); col_sub.release(); Mat col_sub2 = new Mat(col_proj, new Range(0, 1), new Range(half_pos, (int) col_proj.total())); Log.d(TAG, "9.3 --> Copy sub matrix done. col: " + col_sub2.toString()); result.width = (int) (Core.minMaxLoc(col_sub2).maxLoc.x + half_pos - result.x); Log.d(TAG, "9.4 --> Minmax done. Width: " + result.width); col_sub2.release(); row_proj.release(); col_proj.release(); src.release(); return result; }
Example 7
Source File: CVProcessor.java From CVScanner with GNU General Public License v3.0 | 5 votes |
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; }
Example 8
Source File: ImageMatchers.java From onetwo with Apache License 2.0 | 5 votes |
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 9
Source File: CameraActivity.java From AndroidObjectDetection-OpenCV with MIT License | 4 votes |
@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 10
Source File: EyesDetectionInteractorImpl.java From Image-Detection-Samples with Apache License 2.0 | 4 votes |
/** * <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 11
Source File: ImageFinder.java From opentest with MIT License | 4 votes |
/** * 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); }